MySQL 主從同步容易出現的問題

MySQL主從同步:

有三臺mysql A B C 
其中A 和 B 已經做了主從,又把B做成了C的主,然後問題是,B上說什麼也不記錄bin-log,所以C就一直不能從B上同步到數據,B能從A上順利同步到數據。

解決辦法: 
在B的配置文件my.cnf 加上一句 
log-slave-updates

AB主從已設置好,問題出在BC之間

B上:

mysql>grant replication slave on *.* to 'repl2'@'192.168.1.118' identified by 'mysqlslave'mysql> flush table with read clock;mysql> show master status;
+-----------------+----------+--------------+------------------+| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-----------------+----------+--------------+------------------+| B_Master.000002 |   524565 | db1,db2      | mysql,db1        |
+-----------------+----------+--------------+------------------+1 row in set (0.00 sec)123456789123456789

C上:

vim /usr/local/mysql_slave/my.cnf
修改如下:
server-id       = 2  //區別於主的id相同在主上已經配置過,註釋下面的配置#指定要同步的庫 #replicate-do-db=db2 #忽略不同步的庫#replicate-ignore-db=mysql1234567812345678

重啓生效:/etc/init.d/mysqldslave restart

mysql> change master to master_host='192.168.1.119',master_port=3307,master_user='repl2',master_password='mysqlslave',master_log_file='B_Master.000002',master_log_pos=524565;11

C連接B時報錯:

這樣配置了之後,B能同步A,但是C連接不到B,提示:

Last_IO_Error: error connecting to master '[email protected]:3307' - retry-time: 60  retries: 8640011

查看C錯誤日誌:

Version: '5.1.73'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)160512  8:40:29 [ERROR] Slave I/O: error connecting to master '[email protected]:3307' - retry-time: 60  retries: 86400, Error_code: 1130160512  8:40:53 [Note] Error reading relay log event: slave SQL thread was killed160512  8:40:53 [Note] Slave I/O thread killed while connecting to master160512  8:40:53 [Note] Slave I/O thread exiting, read up to log 'B_Master.000003', position 340160512  8:42:14 [Note] 'CHANGE MASTER TO executed'. Previous state master_host='192.168.1.119', master_port='3307', master_log_file='B_Master.000003', master_log_pos='340'. New state master_host='192.168.1.119', master_port='3307', master_log_file='B_Master.000004', master_log_pos='338'.160512  8:42:29 [Note] Slave SQL thread initialized, starting replication in log 'B_Master.000004' at position 338, relay log './localCoohx-relay-bin.000001' position: 4160512  8:42:29 [ERROR] Slave I/O: error connecting to master '[email protected]:3307' - retry-time: 60  retries: 86400, Error_code: 1130160512  8:48:09 [Note] /usr/local/mysql/bin/mysqld: Normal shutdown123456789101112123456789101112

C連接不到B,再試着直接C登錄B:

[root@localCoohx ~]# mysql -u repl2 -h192.168.1.119 -P 3307 -pmysqlslaveERROR 1130 (HY000): Host '192.168.1.118' is not allowed to connect to this MySQL server1212

問題就出在這了,突然想起來B沒有給C授權遠程登錄,於是在B上:

mysql> grant all on *.* to 'repl2'@'192.168.1.118' identified by 'mysqlslave';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)12341234

之後就連接成功了。

————————————————–

由於之前mysql 從機的IP發生變化,所以需要BC之間從新設置主從:

B 192.168.1.112 C:192.168.1.118

B上: 重新給作爲Slave的C授權

mysql> grant replication slave on *.* to 'repl2'@192.168.1.118 identified by 'mysqlslave';                                                        
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
mysql> flush tables with read lock; //讓事務show master status鎖死表的讀mysql> show master status;
+--------------+----------+--------------+------------------+| File         | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------+----------+--------------+------------------+| coohx.000002 |      333 | db1          | mysql            |
+--------------+----------+--------------+------------------+1 row in set (0.00 sec)12345678910111234567891011

C上:

mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host='192.168.1.112',master_port=3307,master_user='repl2',master_password='mysqlslave',master_log_file='B_Master.000028',master_log_pos=106;ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MySQL error log   C上重新設置主信息失敗.




  查看錯誤日誌:    160529 13:15:52 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: '5.1.73'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)160529 13:40:50 [ERROR] Failed to open the relay log './localCoohx-relay-bin.000004' (relay_log_pos 549)160529 13:40:50 [ERROR] Could not find target log during relay log initialization160529 13:45:38 [ERROR] Failed to open the relay log './localCoohx-relay-bin.000004' (relay_log_pos 549)160529 13:45:38 [ERROR] Could not find target log during relay log initialization160529 13:52:06 [ERROR] Failed to open the relay log './localCoohx-relay-bin.000004' (relay_log_pos 549)160529 13:52:06 [ERROR] Could not find target log during relay log initialization160529 13:52:18 [ERROR] Failed to open the relay log './localCoohx-relay-bin.000004' (relay_log_pos 549)160529 13:52:18 [ERROR] Could not find target log during relay log initialization160529 14:00:21 [ERROR] Failed to open the relay log './localCoohx-relay-bin.000004' (relay_log_pos 549)160529 14:00:21 [ERROR] Could not find target log during relay log initialization123456789101112131415161718192021222324123456789101112131415161718192021222324

localCoohx-relay-bin.000004 這是上一次C同步B後生成的relay重演文件,後來由於改過主機名,所以作廢,於是刪掉這些同步文件,繼續執行:

mysql> change master to master_host='192.168.1.112',master_port=3307,master_user='repl2',master_password='mysqlslave',master_log_file='B_Master.000028',master_log_pos=106;
ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MySQL error log1212

還是出錯,查看slave狀態:

mysql> show slave status\G;*************************** 1. row ***************************               Slave_IO_State:                  Master_Host: 192.168.1.119                  Master_User: repl2                  Master_Port: 3307                Connect_Retry: 60              Master_Log_File: B_Master.000005          Read_Master_Log_Pos: 405               Relay_Log_File: localCoohx-relay-bin.000004                Relay_Log_Pos: 549        Relay_Master_Log_File: B_Master.000005             Slave_IO_Running: No            Slave_SQL_Running: No              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: 405              Relay_Log_Space: 0              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: NULLMaster_SSL_Verify_Server_Cert: No                Last_IO_Errno: 0                Last_IO_Error:               Last_SQL_Errno: 0               Last_SQL_Error:1 row in set (0.00 sec)12345678910111213141516171819202122232425262728293031323334353637383940411234567891011121314151617181920212223242526272829303132333435363738394041

顯示爲上一次主從同步時主的信息,主B的IP已經改變,所以重新設置slave-C,但是失敗, 
解決辦法:

mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> reset slave;  //這是關鍵一步!!!Query OK, 0 rows affected (0.03 sec)1234512345
mysql> change master to master_host='192.168.1.112',master_port=3307,master_user='repl2',master_password='mysqlslave',master_log_file='B_Master.000028',master_log_pos=106;
Query OK, 0 rows affected (0.09 sec)
mysql> slave start;
Query OK, 0 rows affected (0.05 sec)12341234
mysql> show slave status\G;*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 192.168.1.112                  Master_User: repl2                  Master_Port: 3307                Connect_Retry: 60              Master_Log_File: B_Master.000028          Read_Master_Log_Pos: 106               Relay_Log_File: Coohx_slave-relay-bin.000002                Relay_Log_Pos: 250        Relay_Master_Log_File: B_Master.000028             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: 106              Relay_Log_Space: 411              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: 0Master_SSL_Verify_Server_Cert: No                Last_IO_Errno: 0                Last_IO_Error:               Last_SQL_Errno: 0               Last_SQL_Error:1 row in set (0.00 sec)12345678910111213141516171819202122232425262728293031323334353637383940411234567891011121314151617181920212223242526272829303132333435363738394041

成功連接到主,進行同步。

補充: 
查看某一賬戶權限 
mysql> show grants for ‘repl2’@’192.168.1.119’; 
+——————————————————————————————————————————+ 
| Grants for [email protected]
+——————————————————————————————————————————+ 
| GRANT REPLICATION SLAVE ON . TO ‘repl2’@’192.168.1.119’ IDENTIFIED BY PASSWORD ‘*EBB378AC232302FF0D4565FFF9F42577B1C04625’ | 
+——————————————————————————————————————————+ 
1 row in set (0.00 sec)

總結:在同一臺mysql服務器上第二次設置從服務器時,應先關掉上一次的主從連接,reset清空以前的master信息,然後重新設置新的slave。 
即:

 stop
 reset
 change master123123


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章