docker + mysql 雙主互備及常見異常處理
docker + mysql 雙主互備
環境 centos7、docker
10.200.132.168 | master | 3307 |
10.200.132.168 | slave | 3308 |
我使用的是以前自帶的docker環境,mysql是docker images裡面存在的映象,也可以自行下載
1、指令碼
vim /opt/bin/start-mysql-master.sh
docker run -d --restart=always --privileged=true --name=mysql-master -p 3307:3306 -e MYSQL_ROOT_PASSWORD=Pioneer@2017 -v /opt/data/mysql/data:/var/lib/mysql:rw -v /etc/localtime:/etc/localtime -v /opt/data/mysql/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf mysql
vim /opt/bin/start-mysql-slave.sh
docker run -d --restart=always --privileged=true --name=mysql-slave -p 3308:3306 -e MYSQL_ROOT_PASSWORD=Pioneer@2017 -v /opt/data/mysql/data-slave:/var/lib/mysql:rw -v /etc/localtime:/etc/localtime -v /opt/data/mysql/mysqld-slave.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf mysql
配置可執行許可權
chmod +x /opt/bin/start-mysql-master.sh
chmod +x /opt/bin/start-mysql-slave.sh
2、mysql data映射出來(/opt/data/mysql)
其中:
master data 目錄:/opt/data/mysql/data
slave data 目錄:/opt/data/mysql/data-slave
3、配置
master mysqld.cnf配置
[mysql] [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql #log-error = /var/log/mysql/error.log # By default we only accept connections from localhost #bind-address = 127.0.0.1 # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 lower_case_table_names = 1 #不區分大小寫 character_set_server = utf8 #字元編碼 log-bin=mysql-bin # 開啟bin-log 日誌,MySQL主從配置,必須開啟 log-bin-index=mysql-bin server_id=1 # 唯一的標識,與slave不同 log-slave-updates = true # 雙主互備必須開啟,否則只是主從關係 relay-log= relaylog relay-log-index=relaylog relay-log-purge=on binlog-do-db=dcmp #開啟同步的資料庫 binlog-do-db=test
slave mysqld-slave.cnf
[mysql]
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
lower_case_table_names = 1 #不區分大小寫
character_set_server = utf8 #字元編碼
log-bin=mysql-bin # 開啟bin-log 日誌,MySQL主從配置,必須開啟
log-bin-index=mysql-bin
server_id=2 # 唯一的標識,與master不同
log-slave-updates = true # 雙主互備必須開啟,否則只是主從關係
relay-log= relaylog
relay-log-index=relaylog
relay-log-purge=on
binlog-do-db=dcmp #開啟同步的資料庫
binlog-do-db=test
4、啟動MySQL
執行第一步的指令碼。
5、開啟slave
①登陸master
docker exec -it mysql-master mysql -hlocalhost -uroot -p123456
檢視master status
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000007 | 154 | dcmp,test | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
②登陸slave
docker exec -it mysql-slave mysql -hlocalhost -uroot -p123456
開啟slave
mysql> change master to master_host='10.200.132.168',master_user='root',master_password='123456',master_port=3307, master_log_file='mysql-bin.000007',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.200.132.168
Master_User: root
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 154
Relay_Log_File: relaylog.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000007
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: 154
Relay_Log_Space: 520
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: 1
Master_UUID: 7beb7135-3ec4-11e7-b73a-0242ac110003
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:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
change master to master_host='10.200.132.168',master_user='root',master_password='123456',master_port=3307, master_log_file='mysql-bin.000007',master_log_pos=154;
配置主庫的資訊,日誌檔案同步起始位。
start slave : 開啟slave
show slave status\G; 檢視slave的同步情況,重點關注兩個指標 Slave_IO_Running: 、 Slave_SQL_Running,如果都是yes,代表同步ok,否則任意一個為No,都是異常。
修改master 資料庫test表,看看slave是否同步。
6、互備
以上只是開啟單向同步的,即把master的資料同步到slave。適合讀寫分離機制
把slave 的資料同步到master跟上面配置基本一樣。
①登陸slave
檢視slave 的master
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000006 | 1948 | dcmp,test | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
②登陸master,開啟master的slave
mysql> change master to master_host='10.200.132.168',master_user='root',master_password='123456',master_port=3308, master_log_file='mysql-bin.000006',master_log_pos=1948;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.200.132.168
Master_User: root
Master_Port: 3308
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 1948
Relay_Log_File: relaylog.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000006
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: 1948
Relay_Log_Space: 520
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: 2
Master_UUID: 0e1fd2f7-a02e-11e8-a7ca-0242ac110007
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:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
修改slave 的test庫表資料,看看master是否有更新。
7、常見異常處理
①Fatal error : The slave I/O thread stops because master and slave have equal MySql server UUIDs
通過上面的報錯資訊我們其實就可以看出問題出在哪裡。上面報錯說的是replication架構中需要保證每個mysql例項UUID(UUID儲存在datadir目錄下的auto.cnf檔案中)唯一,就跟server_id一樣需要保證主從架構中所有mysql例項server_id唯一。
之所以出現會出現這樣的問題,是因為我的從庫主機是克隆的主庫所在的主機,所以auto.cnf檔案中儲存的UUID會出現重複(server_id已經在my.cnf檔案中修改過)
解決方案:
關於該問題的解決方案其實很簡單,停掉備庫例項,刪除備庫的auto.cnf檔案,啟動備庫例項,此時備庫就會產生一個新的auto.cnf檔案(產生新的UUID)。
②ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository
通過上面的報錯以及relay log介紹,很容易知道由於mysql.slave_relay_log_info表中保留了以前的複製資訊,導致新從庫啟動時無法找到對應檔案,那麼我們清理掉該表中的記錄不就可以了。再次提醒,不要手動刪該表資料,MySQL已經提供工具給我們了:reset slave:
reset slave乾的那些事:
1、刪除slave_master_info ,slave_relay_log_info兩個表中資料;
2、刪除所有relay log檔案,並重新建立新的relay log檔案;
3、不會改變gtid_executed 或者 gtid_purged的值
下面解決問題:
1 2 |
|
1 |
|
1 2 |
|
到這裡問題解決了。
③Got fatal error 1236 from master when reading data from binary log: 'Could not open log file'
在master 方 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 | 154 | dcmp,test | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
在slave執行
mysql> reset slave;
Query OK, 0 rows affected (0.01 sec)
mysql> change master to master_host='10.200.132.168',master_user='root',master_password='123456',master_port=3308, master_log_file='mysql-bin.000003',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)