一、mysql主從配置過程
主節(jié)點(diǎn):
?? ?1) 啟動(dòng)二進(jìn)制日志;
?? ??? ?vim /etc/my.cnf | vim /etc/my.cnf.d/server.cnf
?? ??? ??? ?[mysqld]
?? ??? ??? ?log-bin=master-bin
?? ??? ??? ?server-id=1
?? ??? ??? ?innodb_file_per_table=ON
?? ??? ??? ?skip_name_resolve=ON
?? ??? ?systemctl restart mysqld
?? ?2) 為當(dāng)前節(jié)點(diǎn)設(shè)置一個(gè)全局唯一的id號(hào);
?? ?3) 創(chuàng)建有復(fù)制權(quán)限REPLICATION SLAVE, REPLICATION CLIENT的用戶賬號(hào);
?? ??? ?grant replication slave,replication client on *.* to 'repluser'@'172.18.%.%' identified by 'replpass';
?? ??? ?flush privileges;
從節(jié)點(diǎn):
?? ?1) 啟動(dòng)中繼日志;
?? ??? ?vim /etc/my.cnf
?? ??? ??? ?relay-log=relay-log
?? ??? ??? ?server-id=2
?? ??? ??? ?innodb_file_per_table=ON
?? ??? ??? ?skip_name_resolve=ON
?? ??? ?systemctl restart mysqld
?? ?2) 為當(dāng)前節(jié)點(diǎn)設(shè)置一個(gè)全局唯一的id號(hào)并且和主節(jié)點(diǎn)不同;
?? ?3) 使用有復(fù)制權(quán)限的賬號(hào)連接至主服務(wù)器,并啟動(dòng)復(fù)制線程;
?? ??? ?change master to master_host='$ip',master_user='repluser',master_password='replpass',master_log_file='master-bin.000003',master_log_pos=245;
?? ??? ?start slave;
二、mysql主從同步故障定位
1.the slave i/o thread stops because master and slave have equal mysql server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it
問題原因:mysql主庫和從庫server_id相同
更改從庫id既可
2.error executing row event: 'table doesn't exist'
問題原因:主庫中有數(shù)據(jù),從庫是空庫無法直接同步
需先導(dǎo)入一次主庫備份的全量備份數(shù)據(jù),再進(jìn)行主從同步
3.could not execute update_rows event on table super_pms.issue_cli_queue; can't find record in 'issue_cli_queue', error_code: 1032; handler error ha_err_key_not_found; the event's master log mysql-bin-log.001793, end_log_pos 336631
解決辦法:
mysql> stop slave ;
mysql> set global sql_slave_skip_counter=1;
mysql> start slave;
?4.ERROR 1858 (HY000): sql_slave_skip_counter can not be set when the server is running with @@GLOBAL.GTID_MODE = ON. Instead, for each transaction that you want to skip, generate an empty transaction with the same GTID as the transaction
解決辦法:
mysql> stop slave;
mysql> set global gtid_mode=ON_PERMISSIVE;
mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;
mysql> START SLAVE;
mysql> SHOW SLAVE STATUS\G;
mysql> set global gtid_mode=ON;
mysql> show variables like 'gtid_mode';
5.error 1236 報(bào)錯(cuò)
5.1 logevent超過max_allowed_packet 大小
- Got fatal error 1236 from master when reading data from binary log:?'log event entry exceeded max_allowed_packet; Increase max_allowed_packet on master; the start event position from 'mysql-bin.006730' at 290066246, the last event was read from '/u01/my3309/log/mysql-bin.006730
【原因】
? ?此類報(bào)錯(cuò)和max_allowed_packet相關(guān)。首先max_allowed_packet控制著主從復(fù)制過程中,一個(gè)語句產(chǎn)生的二進(jìn)制binlog event大小,它的值必須是1024的倍數(shù) 。出現(xiàn)此類錯(cuò)誤的常見原因是
?1 該參數(shù)在主備庫的配置大小不一樣,主庫的配置值大于從庫的配置值。 從主庫傳遞到備庫的binlog event大小超過了主庫或者備庫的max_allowed_packet大小。
?2 主庫有大量數(shù)據(jù)寫入時(shí),比如在主庫上執(zhí)行?laod data,insert into .... select 語句,產(chǎn)生大事務(wù)。
當(dāng)主庫向從庫傳遞一個(gè)比從庫的max_allowed_packet 大的packet ,從庫接收該packet失敗,并報(bào) “l(fā)og event entry exceeded max_allowed_packet“。
【如何解決】
?需要確保主備配置一樣,然后嘗試調(diào)大該參數(shù)的值。
mysql> set global max_allowed_packet?=1*1024*1024*1024;
mysql> stop slave;
mysql> start slave
另外,5.6 版本中的?slave_max_allowed_packet_size?參數(shù)控制slave 可以接收的最大的packet 大小,該值通常大于而且可以覆蓋 max_allowed_packet 的配置, 進(jìn)而減少由于上面的問題導(dǎo)致主從復(fù)制中斷。
5.2 slave 在主庫找不到binlog文件?
- Got fatal error 1236 from master when reading data from binary log:
【原因】
?該錯(cuò)誤發(fā)生在從庫的io進(jìn)程從主庫拉取日志時(shí),發(fā)現(xiàn)主庫的mysql_bin.index文件中第一個(gè)文件不存在。出現(xiàn)此類報(bào)錯(cuò)可能是由于你的slave 由于某種原因停止了好長(zhǎng)一段是時(shí)間,當(dāng)你重啟slave 復(fù)制的時(shí)候,在主庫上找不到相應(yīng)的binlog ,會(huì)報(bào)此類錯(cuò)誤?;蛘呤怯捎谀承┰O(shè)置主庫上的binlog被刪除了,導(dǎo)致從庫獲取不到對(duì)應(yīng)的binglog file。
【如何解決】
?1 為了避免數(shù)據(jù)丟失,需要重新搭建slave 。
?2 注意主庫binlog的清理策略,選擇基于時(shí)間過期的刪除方式還是基于空間利用率的刪除方式。
? 不要使用rm -fr 命令刪除binlog file,這樣不會(huì)同步修改mysql_bin.index 記錄的binlog 條目。在刪除binlog的時(shí)候確保主庫保留了從庫 show slave status 的Relay_Master_Log_File對(duì)應(yīng)的binlog file。
5.3 主庫空間問題,日志被截?cái)?/strong>
- Got fatal error 1236 from master when reading data from binary log:?'binlog truncated in the middle of event; consider out of disk space on master; the start event position from 'mysql-bin.006730' at 290066434, the last event was read from '/u01/my3309/log/mysql-bin.006730
【原因】
?該錯(cuò)誤和主庫的空間問題和sync_binlog配置有關(guān),當(dāng)主庫 sync_binlog=N不等于1且磁盤空間滿時(shí),MySQL每寫N次binary log,系統(tǒng)才會(huì)同步到磁盤,但是由于存儲(chǔ)日志的磁盤空間滿而導(dǎo)致MySQL 沒有將日志完全寫入磁盤,binlog event被截?cái)?。slave 讀取該binlog file時(shí)就會(huì)報(bào)錯(cuò)"binlog truncated in the middle of event;"
?當(dāng)sync_binlog 的默認(rèn)值是0,像操作系統(tǒng)刷其他文件的機(jī)制一樣,MySQL不會(huì)同步到磁盤中去而是依賴操作系統(tǒng)來刷新binary log。
?當(dāng)sync_binlog =N (N>0) ,MySQL 在每寫 N次 二進(jìn)制日志binary log時(shí),會(huì)使用fdatasync()函數(shù)將它的寫二進(jìn)制日志binary log同步到磁盤中去。
【如何解決】
?在從庫重新指向到主庫下一個(gè)可用的binlog file 并且從binlog file初始化的位置開始
mysql> stop slave;
mysql> change master to master_log_file='mysql-bin.006731',?master_log_pos=4;
mysql> start slave;
5.4 主庫異常斷電,從庫讀取錯(cuò)誤的position
- 120611 20:39:38?[ERROR]?Error reading packet from server:?Client requested master to start replication from impossible position?(?server_errno=1236)?
- 120611 20:39:38?[ERROR]?Slave I/O:?Got fatal error 1236 from master when reading data from binary log:?'Client requested master to start replication from impossible position',?Error_code:?1236
- 120611 20:39:38?[Note]?Slave I/O thread exiting,?read up to log?'mysql-bin.000143',?position 664526789
【原因】
?該問題也是和sync_binlog=N不等于1有關(guān),多出現(xiàn)在主機(jī)異常crash ,比如磁盤損壞,raid 卡損壞,或者主機(jī)異常掉電導(dǎo)致binlog 未及時(shí)同步到磁盤。從庫讀取了主庫binlog file中的不存在的binlog position ,一般比binlogfile 的end position 的值還要大。
【如何解決】
1 在從庫重新指向到主庫下一個(gè)可用的binlog file 并且從binlog file初始化的位置開始文章來源:http://www.zghlxwxcb.cn/news/detail-512944.html
mysql> stop slave;
mysql> change master to master_log_file='mysql-bin.000144',?master_log_pos=4;
mysql> start slave;
2 主備庫設(shè)置 sync_binlog=1,但是設(shè)置為1的時(shí)候,會(huì)帶來性能下降。?文章來源地址http://www.zghlxwxcb.cn/news/detail-512944.html
到了這里,關(guān)于mysql主從同步配置及故障定位的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!