當(dāng)前位置:首頁 > IT技術(shù) > 數(shù)據(jù)庫 > 正文

docker 構(gòu)建mysql主主
2021-10-12 14:20:09

db01

docker load -i mysql57.tar
db01
docker run --net=host --name db01 -v /data1:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7


docker cp db01:/etc/mysql/mysql.conf.d/mysqld.cnf /tmp/mysqld_1.cnf


vi /tmp/mysqld_1.cnf #編輯添加以下
server-id = 101
log-bin = mysql-bin
max_connections = 1000
gtid_mode = on
enforce_gtid_consistency = 1
auto_increment_offset = 1
auto_increment_increment = 2




docker cp /tmp/mysqld_1.cnf db01:/etc/mysql/mysql.conf.d/mysqld.cnf


docker restart db01


db02

docker load -i mysql57.tar


docker run --net=host --name db02 -v /data2:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
docker cp db02:/etc/mysql/mysql.conf.d/mysqld.cnf /tmp/mysqld_2.cnf


vi /tmp/mysqld_2.cnf


server-id = 102log-bin = mysql-bin
max_connections = 1000gtid_mode = on
enforce_gtid_consistency = 1
auto_increment_offset = 2
auto_increment_increment =2


docker cp /tmp/mysqld_2.cnf db02:/etc/mysql/mysql.conf.d/mysqld.cnf


docker restart db02


連接db01

docker exec -it db01 bash
mysql -uroot -P 3301 -p123456
grant replication slave on *.* to 'rep'@'%' identified by '123456';


連接db02


docker exec -it db02 bash
mysql -uroot -P 3301 -p123456
grant replication slave on *.* to 'rep'@'%' identified by '123456';


在db01上

change master to master_host='192.168.40.217',master_port=3306,master_user='rep',master_password='123456',MASTER_AUTO_POSITION=1;
start slave;
show slave statusG


在db02上

change master to master_host='192.168.40.216',master_port=3306,master_user='rep',master_password='123456',MASTER_AUTO_POSITION=1;
start slave;
show slave statusG


檢查狀態(tài)

檢查狀態(tài)
Slave_IO_Running和Slave_SQL_Running 兩個值都為YES,說明復(fù)制狀態(tài)正常
Seconds_Behind_Master 為0,說明沒有同步延遲
Slave_IO_Running: Yes
Slave_SQL_Running: Yes


在db01 db02 創(chuàng)建遠程連接用戶

dacs

grant all on *.* to dacs@'%' identified by '123456' with grant option;

導(dǎo)入數(shù)據(jù)


mysql -h127.0.0.1 -udacs -p123456 < server.sql

mysql -h127.0.0.1 -udacs -p123456 < server_V1.0.9.sql

mysql -h127.0.0.1 -udacs -p123456 < server_V1.1.0.sql

mysql -h127.0.0.1 -udacs -p123456 < data.sql

mysql -h127.0.0.1 -udacs -p123456 < data_V1.0.9.sql


本文摘自 :https://blog.51cto.com/u

開通會員,享受整站包年服務(wù)立即開通 >