How to re-sync the Mysql DB if Master and slave have different database incase of Mysql replication?

前端 未结 14 917
小蘑菇
小蘑菇 2020-11-29 14:20

Mysql Server1 is running as MASTER.
Mysql Server2 is running as SLAVE.

Now DB replication is happeni

相关标签:
14条回答
  • 2020-11-29 14:57

    Master:

    mysqldump -u root -p --all-databases --master-data | gzip > /tmp/dump.sql.gz  
    

    scp master:/tmp/dump.sql.gz slave:/tmp/ Move dump file to slave server

    Slave:

    STOP SLAVE;
    

    zcat /tmp/dump.sql.gz | mysql -u root -p

    START SLAVE;
    SHOW SLAVE STATUS;  
    

    NOTE:
    On master you can run SET GLOBAL expire_logs_days = 3 to keep binlogs for 3 days in case of slave issues.

    0 讨论(0)
  • 2020-11-29 15:01

    Here is a complete answer that will hopefully help others...


    I want to setup mysql replication using master and slave, and since the only thing I knew was that it uses log file(s) to synchronize, if the slave goes offline and gets out of sync, in theory it should only need to connect back to its master and keep reading the log file from where it left off, as user malonso mentioned.

    So here are the test result after configuring the master and slave as mentioned by: http://dev.mysql.com/doc/refman/5.0/en/replication-howto.html ...

    Provided you use the recommended master/slave configuration and don't write to the slave, he and I where right (as far as mysql-server 5.x is concerned). I didn't even need to use "START SLAVE;", it just caught up to its master. But there is a default 88000 something retries every 60 second so I guess if you exhaust that you might have to start or restart the slave. Anyways, for those like me who wanted to know if having a slave going offline and back up again requires manual intervention.. no, it doesn't.

    Maybe the original poster had corruption in the log-file(s)? But most probably not just a server going off-line for a day.


    pulled from /usr/share/doc/mysql-server-5.1/README.Debian.gz which probably makes sense to non debian servers as well:

    * FURTHER NOTES ON REPLICATION
    ===============================
    If the MySQL server is acting as a replication slave, you should not
    set --tmpdir to point to a directory on a memory-based filesystem or to
    a directory that is cleared when the server host restarts. A replication
    slave needs some of its temporary files to survive a machine restart so
    that it can replicate temporary tables or LOAD DATA INFILE operations. If
    files in the temporary file directory are lost when the server restarts,
    replication fails.
    

    you can use something sql like: show variables like 'tmpdir'; to find out.

    0 讨论(0)
  • 2020-11-29 15:02

    Adding to the popular answer to include this error:

    "ERROR 1200 (HY000): The server is not configured as slave; fix in config file or with CHANGE MASTER TO",
    

    Replication from slave in one shot:

    In one terminal window:

    mysql -h <Master_IP_Address> -uroot -p
    

    After connecting,

    RESET MASTER;
    FLUSH TABLES WITH READ LOCK;
    SHOW MASTER STATUS;
    

    The status appears as below: Note that position number varies!

    +------------------+----------+--------------+------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +------------------+----------+--------------+------------------+
    | mysql-bin.000001 |      98  | your_DB      |                  |
    +------------------+----------+--------------+------------------+
    

    Export the dump similar to how he described "using another terminal"!

    Exit and connect to your own DB(which is the slave):

    mysql -u root -p
    

    The type the below commands:

    STOP SLAVE;
    

    Import the Dump as mentioned (in another terminal, of course!) and type the below commands:

    RESET SLAVE;
    CHANGE MASTER TO 
      MASTER_HOST = 'Master_IP_Address', 
      MASTER_USER = 'your_Master_user', // usually the "root" user
      MASTER_PASSWORD = 'Your_MasterDB_Password', 
      MASTER_PORT = 3306, 
      MASTER_LOG_FILE = 'mysql-bin.000001', 
      MASTER_LOG_POS = 98; // In this case
    

    Once logged, set the server_id parameter (usually, for new / non-replicated DBs, this is not set by default),

    set global server_id=4000;
    

    Now, start the slave.

    START SLAVE;
    SHOW SLAVE STATUS\G;
    

    The output should be the same as he described.

      Slave_IO_Running: Yes
      Slave_SQL_Running: Yes
    

    Note: Once replicated, the master and slave share the same password!

    0 讨论(0)
  • 2020-11-29 15:07

    sometimes you just need to give the slave a kick too

    try

    stop slave;    
    reset slave;    
    start slave;    
    show slave status;
    

    quite often, slaves, they just get stuck guys :)

    0 讨论(0)
  • 2020-11-29 15:07

    I created a GitHub repo with an script to solve this problem quickly. Just change a couple variables and run it (First, the script creates a backup of your database).

    I hope this help you (and others people too).

    How to Reset (Re-Sync) MySQL Master-Slave Replication

    0 讨论(0)
  • 2020-11-29 15:10

    This is the full step-by-step procedure to resync a master-slave replication from scratch:

    At the master:

    RESET MASTER;
    FLUSH TABLES WITH READ LOCK;
    SHOW MASTER STATUS;
    

    And copy the values of the result of the last command somewhere.

    Without closing the connection to the client (because it would release the read lock) issue the command to get a dump of the master:

    mysqldump -u root -p --all-databases > /a/path/mysqldump.sql
    

    Now you can release the lock, even if the dump hasn't ended yet. To do it, perform the following command in the MySQL client:

    UNLOCK TABLES;
    

    Now copy the dump file to the slave using scp or your preferred tool.

    At the slave:

    Open a connection to mysql and type:

    STOP SLAVE;
    

    Load master's data dump with this console command:

    mysql -uroot -p < mysqldump.sql
    

    Sync slave and master logs:

    RESET SLAVE;
    CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=98;
    

    Where the values of the above fields are the ones you copied before.

    Finally, type:

    START SLAVE;
    

    To check that everything is working again, after typing:

    SHOW SLAVE STATUS;
    

    you should see:

    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes
    

    That's it!

    0 讨论(0)
提交回复
热议问题