Docker安裝mysql8主從結構

準備兩臺服務器,假設IP地址爲mysql

172.31.27.67    主服務器sql

172.31.27.69    從服務器docker

由於我用的鏡像是docker.io/cytopia/mysql-8.0,因此咱們須要先把該鏡像給pull下來。數據庫

docker pull docker.io/cytopia/mysql-8.0服務器

在/home下建一個文件夾socket

mkdir /home/wen工具

在主服務器建一個master.my.cnfserver

內容以下blog

[client]
socket = /var/sock/mysqld/mysqld.sock
[mysql]
socket = /var/sock/mysqld/mysqld.sock
[mysqld]
skip-host-cache
skip-name-resolve
datadir = /var/lib/mysql
user = mysql
port = 3306
bind-address = 0.0.0.0
socket = /var/sock/mysqld/mysqld.sock
pid-file = /var/run/mysqld/mysqld.pid
general_log_file = /var/log/mysql/query.log
slow_query_log_file = /var/log/mysql/slow.log
log-error = /var/log/mysql/error.log
log-bin=mysql-bin
server-id=1
!includedir /etc/my.cnf.d/
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/docker-default.d/ip

在從服務器建一個slave.my.cnf,內容以下

[client]
socket = /var/sock/mysqld/mysqld.sock
[mysql]
socket = /var/sock/mysqld/mysqld.sock
[mysqld]
skip-host-cache
skip-name-resolve
datadir = /var/lib/mysql
user = mysql
port = 3306
bind-address = 0.0.0.0
socket = /var/sock/mysqld/mysqld.sock
pid-file = /var/run/mysqld/mysqld.pid
general_log_file = /var/log/mysql/query.log
slow_query_log_file = /var/log/mysql/slow.log
log-error = /var/log/mysql/error.log
log-bin=mysql-bin
server-id=2
!includedir /etc/my.cnf.d/
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/docker-default.d/

這裏他們的server-id要區分開

在主服務器啓動(密碼由你本身設定)

docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=abcd123 -p 3307:3306 -v /etc/localtime:/etc/localtime:ro -v /home/wen/master.my.cnf:/etc/my.cnf docker.io/cytopia/mysql-8.0

在從服務器啓動

docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=abcd123 -p 3306:3306 -v /etc/localtime:/etc/localtime:ro -v /home/wen/slave.my.cnf:/etc/my.cnf docker.io/cytopia/mysql-8.0

使用數據庫鏈接工具進行鏈接,先鏈接主服務器的數據庫

依次執行

GRANT REPLICATION SLAVE ON *.* TO 'root'@'%';

flush privileges;

show master status;

能夠看到主數據庫的狀態

切換到從數據庫,依次執行

CHANGE MASTER TO
MASTER_HOST='172.31.27.67',
MASTER_PORT=3307,
MASTER_USER='root',
MASTER_PASSWORD='abcd123',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=6143;

start slave;

show slave status;

若是咱們看到

其中Slave_IO_Running,Slave_SQL_Running必須爲Yes,表示同步成功,不然執行

stop slave;

將以前的動做從新執行一遍。以後咱們在主庫作的SQL語句執行,會同步到從庫中來。

相關文章
相關標籤/搜索