mysql 主從不同步

來源:互聯網
上載者:User

標籤:資料   show   今天   錯誤   統一   mysql 主從   介紹   global   tab   

今天發現Mysql的主從資料庫沒有同步 
先上Master庫: 
mysql>show processlist; 查看下進程是否Sleep太多。發現很正常。 
show master status; 也正常。 
mysql> show master status; 
+-------------------+----------+--------------+-------------------------------+ 
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | 
+-------------------+----------+--------------+-------------------------------+ 
| mysqld-bin.000001 | 3260 | | mysql,test,information_schema | 
+-------------------+----------+--------------+-------------------------------+ 
1 row in set (0.00 sec) 
再到Slave上查看 
mysql> show slave status\G 
Slave_IO_Running: Yes 
Slave_SQL_Running: No 
可見是Slave不同步 

下面介紹兩種解決方案: 
方法一:忽略錯誤後,繼續同步 
該方法適用於主從庫資料相差不大,或者要求資料可以不完全統一的情況,資料要求不嚴格的情況 
解決: 
stop slave; 
#表示跳過一步錯誤,後面的數字可變 
set global sql_slave_skip_counter =1; 
start slave; 
之後再用mysql> show slave status\G 查看: 
Slave_IO_Running: Yes 
Slave_SQL_Running: Yes 
ok,現在主從同步狀態正常了。。。 

方式二:重新做主從,完全同步 
該方法適用於主從庫資料相差較大,或者要求資料完全統一的情況 
解決步驟如下: 
1.先進入主庫,進行鎖表,防止資料寫入 
使用命令: 
mysql> flush tables with read lock; 
注意:該處是鎖定為唯讀狀態,語句不區分大小寫 
2.進行資料備份 
#把資料備份到mysql.bak.sql檔案 
[[email protected] mysql]#mysqldump -uroot -p -hlocalhost > mysql.bak.sql 
這裡注意一點:Database Backup一定要定期進行,可以用shell指令碼或者python指令碼,都比較方便,確保資料萬無一失 
3.查看master 狀態 
mysql> show master status; 
+-------------------+----------+--------------+-------------------------------+ 
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | 
+-------------------+----------+--------------+-------------------------------+ 
| mysqld-bin.000001 | 3260 | | mysql,test,information_schema | 
+-------------------+----------+--------------+-------------------------------+ 
1 row in set (0.00 sec) 
4.把mysql備份檔案傳到從庫機器,進行資料恢複 
#使用scp命令 
[[email protected] mysql]# scp mysql.bak.sql [email protected]:/tmp/ 
5.停止從庫的狀態 
mysql> stop slave; 
6.然後到從庫執行mysql命令,匯入資料備份 
mysql> source /tmp/mysql.bak.sql 
7.設定從庫同步,注意該處的同步點,就是主庫show master status資訊裡的| File| Position兩項 
change master to master_host = ‘192.168.128.100‘, master_user = ‘rsync‘, master_port=3306, master_password=‘‘, master_log_file = ‘mysqld-bin.000001‘, master_log_pos=3260; 
8.重新開啟從同步 
mysql> start slave; 
9.查看同步狀態 
mysql> show slave status\G 查看: 
Slave_IO_Running: Yes 
Slave_SQL_Running: Yes 
好了,同步完成啦。

別忘了unlock tables;

轉載的

mysql 主從不同步

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.