環(huán)境
centos7
Docker version 1.12.6
mysql鏡像5.7.5
主 10.43.124.88 master 3307
備 10.43.124.89 slave 3307
一笆凌、mysql容器啟動腳本準備
vim /opt/bin/start-mysql-master.sh
docker run --privileged=true -d -p 3307:3306 --name mysql3307 --network wp-net --network-alias mysql -e MYSQL_ROOT_PASSWORD=sxbctv3306 -v /home/slave/data/mysql:/var/lib/mysql -v /home/slave/conf/my.cnf:/etc/mysql/my.cnf -v /etc/localtime:/etc/localtime:ro 6448ea6ff3b5
vim /opt/bin/start-mysql-slave.sh
docker run --privileged=true -d -p 3307:3306 --name mysql3307 --network wp-net --network-alias mysql -e MYSQL_ROOT_PASSWORD=sxbctv3306 -v /home/slave/data/mysql:/var/lib/mysql -v /home/slave/conf/my.cnf:/etc/mysql/my.cnf -v /etc/localtime:/etc/localtime:ro 6448ea6ff3b5
【注意】 需要用到掛載的數(shù)據(jù)庫以及mysql的配置文件需要自己提前啟動一個mysql容器滔迈,然后將數(shù)據(jù)庫和配置文件拷貝到宿主機后再操作下面的步驟
二、賦予可執(zhí)行權限
chmod +x /opt/bin/start-mysql-master.sh
chmod +x /opt/bin/start-mysql-slave.sh
三换况、配置
1、在master 配置
進入容器
docker exec -it mysql3307 bash
在master 的 my.conf 的[mysqld] 下增加
vi /etc/mysql/my.cnf
#開啟二進制日志
log-bin=mysql-bin
#2到232–1之間的一個正整數(shù)值鲫趁,唯一穷蛹,默認是1丹莲,建議使用ip的最后一段
server-id=90
#自增id起始值為1
auto_increment_offset=1
#自增基數(shù)為2,保證id為奇數(shù)
auto_increment_increment=2
#手動啟動同步服務朗涩,避免突然宕機導致的數(shù)據(jù)日志不同步
skip-slave-start
#互為主從需要加入這一行
log-slave-updates
2忽孽、啟動master的mysql
在master服務器執(zhí)行
./opt/bin/start-mysql-master.sh
3、slave配置
進入容器
docker exec -it mysql3307 bash
在slave的 my.conf 的[mysqld] 下增加
vi /etc/mysql/my.cnf
#開啟二進制日志
log-bin=mysql-bin
#2到232–1之間的一個正整數(shù)值谢床,唯一兄一,默認是1,建議使用ip的最后一段
server-id=90
#自增id起始值為1
auto_increment_offset=1
#自增基數(shù)為2识腿,保證id為奇數(shù)
auto_increment_increment=2
#手動啟動同步服務出革,避免突然宕機導致的數(shù)據(jù)日志不同步
skip-slave-start
#互為主從需要加入這一行
log-slave-updates
4、啟動slave的mysql
在slave服務器執(zhí)行
./opt/bin/start-mysql-slave.sh
5渡讼、查看master status
執(zhí)行mysql命令 清空所有的bin-log日志
mysql> reset master;
然后執(zhí)行mysql命令 show master status;查看最新的bin日志骂束。
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 592 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
上圖中的mysql-bin.000004 就是最新的日志文件名稱,Position是bin日志結束的位置
配置主從會使用到
6成箫、 登陸slave(備)
進入容器
docker exec -it mysql3307 bash
登陸mysql
mysql -uroot -p123456
# 主從配置
#master_host是指主機A的ip地址展箱,
#master_port 主機A的端口,
#master_uesr是執(zhí)行同步操作的用戶名蹬昌,
#master_password是執(zhí)行同步的用戶密碼混驰,
#master_log_file是主機A的bin-log日志,
#master_log_pos是從bin-log日志開始同步的位置
mysql> change master to master_host='10.43.124.88',
master_port=3307,
master_user='backup',
master_password='backup888',
master_log_file='mysql-bin.000004',
master_log_pos=592 ;
Query OK, 0 rows affected, 9 warnings (0.01 sec)
#開啟slave
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
#查看slave的同步情況,重點關注兩個指標 Slave_IO_Running: 栖榨、 Slave_SQL_Running竞慢,如果都是yes,代表同步ok治泥,否則任意一個為No,都是異常筹煮。
#若出現(xiàn)異常,則根據(jù)錯誤日志去排查問題
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.43.124.88
Master_User: backup
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 120
Relay_Log_File: d8134c324dd3-relay-bin.000002
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 120
Relay_Log_Space: 463
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 90
Master_UUID: 918c8f0b-94f5-11e9-ae31-0242ac110002
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
1 row in set (0.00 sec)
ERROR:
No query specified
此時配置工作已經(jīng)完成居夹,可以在master新建數(shù)據(jù)庫败潦,然后在slave去查看
遇到的問題1
The slave I/O thread stops because master and slave have equal MySQL server UUIDs
問題是出在主從的auto.cnf 的uuid一樣
[root@localhost ~]# docker exec -it mysql3307 bash
root@6be3bff282a5:/# cat /var/lib/mysql/auto.cnf
[auto]
server-uuid=918c8f0b-94f5-11e9-ae31-0242ac110002
解決:在slave 中進容器 刪除auto.cnf 然后重啟slave 的mysql容器
[root@localhost ~]# docker exec -it mysql3307 bash
root@d8134c324dd3:/# cat /var/lib/mysql/auto.cnf
[auto]
server-uuid=918c8f0b-94f5-11e9-ae31-0242ac110002
遇到的問題2
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
在master 刷新binlog flush logs
mysql> flush logs;
Query OK, 0 rows affected (0.01 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 151 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
在slave執(zhí)行
mysql> reset slave;
Query OK, 0 rows affected (0.01 sec)
mysql> change master to master_host='10.43.124.88',
master_port=3307,
master_user='backup',
master_password='backup888',
master_log_file='mysql-bin.000004',
master_log_pos=151;
Query OK, 0 rows affected, 9 warnings (0.01 sec)
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)