天天看点

关于mysql主从的数据同步不一致的解决方法

对于已经做完mysql主从发现show slave status\G;查看mysql-bin-0000XX,slave等这些信息一致时候很开心,但当你在主上删除一个库里面一个表再到从上查看才发现信息不能同步一致的结果令人很郁闷...

<a href="http://s2.51cto.com/wyfs02/M00/7D/15/wKiom1bfxojzFT-mAABKXPq0Oj0475.png" target="_blank"></a>

<a href="http://s2.51cto.com/wyfs02/M00/7D/13/wKioL1bfxwvDZysyAABM4D8p3ec591.png" target="_blank"></a>

我就是前晚经历了这一次的宝贵教训(两个从的库居然是多了一两个,还要都有不同的库存在),正苦恼于自己当时的忘记,我就唯有将主从重组,首先在从上stop slave后将和主的不一样的库删除掉,分别再进入主从里面确认两边的库信息一样(这个要对照清楚,不然你又再做好主从才发现有错漏时候又要将主从重组),确认无误后再做主从各自的操作,今天,我终于将主从重组成功后查看发现主从的数据库信息终于可以一致了

关于mysql主从的数据同步不一致的解决方法

(当时心里很紧张会不会成功重新同步了)

主:directory   从1:slave1     从2:slave2

[root@directory ~]# mysql -S /tmp/mysql.sock -p123456

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.1.72-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql&gt; show databases;

+--------------------+

| Database           |

| information_schema |

| db2                |

| discuz             |

| mysql              |

4 rows in set (0.06 sec)

mysql&gt; use db2;

Database changed

mysql&gt; show tables;

+---------------------------+

| Tables_in_db2             |

| columns_priv              |

| db                        |

| event                     |

| func                      |

| general_log               |

| help_keyword              |

| help_relation             |

| help_topic                |

| host                      |

| ndb_binlog_index          |

| plugin                    |

| proc                      |

| procs_priv                |

| servers                   |

| slow_log                  |

| tables_priv               |

| tb2                       |

| time_zone                 |

| time_zone_leap_second     |

| time_zone_name            |

| time_zone_transition      |

| time_zone_transition_type |

| user                      |

23 rows in set (0.01 sec)

mysql&gt; show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

| mysql-bin.000007 |      106 | db2          |                  |

1 row in set (0.00 sec)

mysql&gt; flush tables with read lock;

Query OK, 0 rows affected (0.00 sec)

mysql&gt; unlock tables;

mysql&gt; show processlist;

+----+------+---------------------+------+-------------+------+-----------------                                             -----------------------------------------------+------------------+

| Id | User | Host                | db   | Command     | Time | State                                                                                                       | Info             |

|  1 | root | localhost           | db2  | Query       |    0 | NULL                                                                                                        | show processlist |

|  2 | repl | 192.168.0.141:54535 | NULL | Binlog Dump |  262 | Has sent all bin                                             log to slave; waiting for binlog to be updated | NULL             |

|  3 | repl | 192.168.0.140:34429 | NULL | Binlog Dump |  142 | Has sent all bin                                             log to slave; waiting for binlog to be updated | NULL             |

3 rows in set (0.00 sec)

mysql&gt; drop table help_keyword;

Query OK, 0 rows affected (0.01 sec)

22 rows in set (0.01 sec)

mysql&gt; drop table help_relation;

[root@slave1 ~]#  mysql -S /tmp/mysql.sock -p123456

Your MySQL connection id is 4

Server version: 5.1.51-log Source distribution

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

This software comes with ABSOLUTELY NO WARRANTY. This is free software,

and you are welcome to modify and redistribute it under the GPL v2 license

mysql&gt; stop slave;

mysql&gt; reset slave;

Query OK, 0 rows affected (0.04 sec)

mysql&gt; change master to master_host='192.168.0.8',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000007',master_log_pos=106;

Query OK, 0 rows affected (0.03 sec)

mysql&gt; flush privileges;

mysql&gt; start slave;

mysql&gt; show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.0.8

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000007

          Read_Master_Log_Pos: 106

               Relay_Log_File: relay-bin.000002

                Relay_Log_Pos: 251

        Relay_Master_Log_File: mysql-bin.000007

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: db2

          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: 400

              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:

1 row in set (0.01 sec)

ERROR:

No query specified

mysql&gt; show tables

    -&gt; ;

| help_category             |

23 rows in set (0.00 sec)

| help_category             |

22 rows in set (0.00 sec)

PS:从库多出一个help_category 的表,是因为之前主从数据库不一致时候,在主删除了一个表来尝试导致的.现在其余的表再删除就可以同步了

     本文转自wsw26 51CTO博客,原文链接:http://blog.51cto.com/wsw26/1749125,如需转载请自行联系原作者