MySQL5.6配置同步複製的新方法以及常見問題的解決方案

來源:互聯網
上載者:User

MySQL5.6配置同步複製的新方法以及常見問題的解決方案

MySQL5.6新增一種不用設定log_bin檔案和log位置的方法,之前我們都需要查看master機器資訊(show master status),然後在slave設定

master_host='10.163.213.228',master_user='repl',master_password='sangfordb',master_log_file='mysql-bin.000015',master_log_pos=193952;

使用mysql5.6後可以不用這種方法配置了,只要設定my.cnf檔案

log-bin=mysql-bin

binlog_format=row

log_slave_updates

gtid-mode=ON

enforce-gtid-consistency=ON

然後在slave中設定:

change master to

master_host='masterip',master_user='replicationuser',master_password='password',master_AUTO_POSITION=1;

其中masterip、replicationuser和password分別對應你master機的資訊

常見問題:

1、show slave status中Slave_IO_State: Waiting to reconnect after a failed registration on master

解決方案:

在master上執行

grant replication slave on *.* to 'repl'@'%' identified by 'password';

FLUSH PRIVILEGES;

然後重新stop slave 和start slave就可以

2、show slave status中Slave_IO_Running: No

Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

解決辦法:每個庫的uuid應該是不一樣的,修改auto.cnf檔案(在mysql 的data目錄下)的uuid:

[auto]

server-uuid=6dcee5be-8cdb-11e2-9408-90e2ba2e2ea6

按照這個16進位格式,隨便改下,重啟mysql即可。

3、從庫中slave_sql_running為NO

一般在 Last_SQL_Error:項中可以看到報錯的語句

Slave_IO_Running:串連到主庫,並讀取主庫的日誌到本地,產生本地記錄檔

Slave_SQL_Running:讀取本地記錄檔,並執行日誌裡的SQL命令。

這時,網上很多會教使用SET global sql_slave_skip_counter=n; 設定,但其實執行這個語句都會報錯,因為啟動了gtid-mode=ON

解決方案:跳過執行報錯的語句

查看show slave status的最後兩行:

Retrieved_Gtid_Set表示已經從master中拉取過來的事務;

Executed_Gtid_Set表示已經執行的事務

找到Executed_Gtid_Set中與Retrieved_Gtid_Set  ID一致的記錄,如ca83d308-2ea5-11e4-b85f-00163e042f50:1-5,表示只執行到第五個事務,就是這個事務報錯了,因此跳過這個事務即可。依次執行以下語句:

stop slave;

set gtid_next='ca83d308-2ea5-11e4-b85f-00163e042f50:6';

begin;commit;

set gtid_next="AUTOMATIC";

start slave;

然後再查看show slave status\G

如果看到以下狀態即可以了

PS:啟動主從備份時,一定要確保主從的資料一致,因為從庫遇到任何報錯,即使執行update語句時,主庫中的一條記錄在從庫中找不到,都會報導致問題2的出現,從而停止執行slave更新

--------------------------------------分割線 --------------------------------------

Ubuntu 14.04下安裝MySQL

《MySQL權威指南(原書第2版)》清晰中文掃描版 PDF

Ubuntu 14.04 LTS 安裝 LNMP Nginx\PHP5 (PHP-FPM)\MySQL

Ubuntu 14.04下搭建MySQL主從伺服器

Ubuntu 12.04 LTS 構建高可用分布式 MySQL 叢集

Ubuntu 12.04下原始碼安裝MySQL5.6以及Python-MySQLdb

MySQL-5.5.38通用二進位安裝

--------------------------------------分割線 --------------------------------------

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.