docker mysql8.0 主從複製
docker mysql8.0 主從複製
基於mysql8.0 配置的主從複製,排除一些坑。
安裝docker
ubuntu18 安裝最新版docker
如果能訪問此鏈接,就按教程走,否則自行百度:訪問docker連接
成功後繼續
- 更新索引包 :
sudo apt update
- 通過HTTPS使用倉庫(repository)安裝:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
- 添加Docker官方的GPG密鑰:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- 添加源:
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
- 安裝:
sudo apt install docker-ce
- 查看是否啓動:
systemctl status docker
- 需要配置鏡像加速,否則拉取鏡像會很慢,本人用的阿里鏡像加速,自己去阿里雲申請配置即可,免費的:
sudo vim /etc/docker/daemon.json
{ "registry-mirrors": ["××××××××××.mirror.aliyuncs.com"] }
配置主從複製
- 拉取鏡像(目前我拉取的最新版本是8.0.19):
$ sudo docker pull mysql
- 查看進行:
sudo docker images
- 安裝mysql-client,用於訪問mysql容器數據庫:
$ sudo apt install mysql-client-core-5.7
- 創建工作目錄,保存mysql數據到本地:
cd ~/
mkdir docker-mysql
cd docker-mysql
mkdir -p mysql-master/data mysql-master/cnf mysql-slave1/data mysql-slave1/cnf
- 創建主從mysql配置文件:
vim mysql-master/cnf /my.cnf
~ 主mysql配置
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# 主從配置服務期id
server_id = 1
# 開啓日誌文件
log-bin = mysql-bin
# 不參與主從的數據庫名
binlog-ignore-db= mysql
# 需要進行 binlog 日誌記錄的數據庫 `test`爲你自己的數據庫名稱
binlog-do-db = test
# Custom config should go here
!includedir /etc/mysql/conf.d/
~ 從mysql 配置
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# 主從配置服務期id
server_id = 2
# 開啓日誌文件
log-bin = mysql-bin
# 不參與主從的數據庫名
binlog-ignore-db= mysql
# 需要進行 binlog 日誌記錄的數據庫 `test`爲你自己的數據庫名稱
binlog-do-db = test
# Custom config should go here
!includedir /etc/mysql/conf.d/
- 運行主myqsl容器(目錄記得改成自己的):
run 運行一個容器
–name 後面是這個鏡像的名稱
-p 3307:3306 3307表示映射到本機的端口號,306表示在這個容器中使用的端口
-d 表示使用守護進程運行,即服務掛在後臺
sudo docker run -di -e MYSQL_ROOT_PASSWORD=123456 --name mysql-master -v /home/xiyeming/docker-mysql-data/mysql-master/data:/var/lib/mysql -v /home/xiyeming/docker-mysql-data/mysql-master/cnf/my.cnf:/etc/mysql/my.cnf -p 3307:3306 -d mysql
- 運行從myqsl容器(目錄記得改成自己的):
sudo docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name mysql-slave1 -v /home/xiyeming/docker-mysql-data/mysql-slave1/data:/var/lib/mysql -v /home/xiyeming/docker-mysql-data/mysql-slave1/cnf/my.cnf:/etc/mysql/my.cnf -p 3308:3306 mysql
- 連接容器數據庫:
-h 連接的ip
–port 連接的端口
~ 主:
mysql -uroot -p123456 -h127.0.0.1 --port 3307
~ 從:
mysql -uroot -p123456 -h127.0.0.1 --port 3308
- 數據庫庫配置:
- 主庫配置:
- 創建賬號:
create user 'slave1'@'%' identified by 'slave1';
- 授權:
grant all privileges ON *.* TO 'slave1'@'%';
- 驗證方式修改爲上一版的:
ALTER USER slave1@'%' IDENTIFIED WITH mysql_native_password BY 'slave1';
- 刷新:
flush privileges;
- 查看狀態
mysql> show master status;
- 從庫配置
- master_log_file 對應File
master_log_pos對應Position change master to master_host='192.168.1.102',master_user='slave1',master_password='slave1',master_log_file='mysql-bin.000003',master_log_pos=1298,master_port=3307;
start slave;
show slave status \G
- 主從配置已完成,可以自己測試,在主庫中寫入數據,到從庫中查看數據。
- 主庫配置: