且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果主机和从机具有不同的数据库的Mysql复制,如何重新同步Mysql数据库?

更新时间:2023-02-02 20:39:49

这是从头重新同步主从复制的完整的分步过程:



在主机:

  RESET MASTER; 
带锁定的冲压表;
SHOW MASTER STATUS;

并且将最后一个命令的结果复制到某处。 / p>

不关闭与客户端的连接(因为它会释放读锁)发出命令以获取主服务器的转储:

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



现在你可以释放锁,即使转储hasn' t结束了。为此,请在MySQL客户端中执行以下命令:

  UNLOCK TABLES; 

现在使用scp或首选工具将转储文件复制到从属。



在从属:



打开与mysql的连接并输入:

 停止奴隶; 

使用此控制台命令加载主数据转储:

  mysql -uroot -p< mysqldump.sql 

同步从属和主日志:

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

其中上述字段的值是您之前复制的值。



最后,输入:

  START SLAVE; 

键入以下内容后,要检查一切是否正常工作:

  SHOW从属状态; 

您应该会看到:

  Slave_IO_Running:是
Slave_SQL_Running:是

就是这样!


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

Now DB replication is happening from MASTER to SLAVE.

Server2 is removed from network and re-connect it back after 1 day. After this there is mismatch in database in master and slave.

How to re-sync the DB again as after restoring DB taken from Master to Slave also doesn't solve the problem ?

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!