User Tools

Site Tools


useful:mysql-replication

Contents Page

Resync MySQL or MariaDB replication

This works just the same for MariaDB and MySQL. Replication means there is a master server and a slave server. I’m assuming that you have command line access to both. It is handy to set up two terminal sessions on each server. If you use Terminator, you can do that as four panes within a single tab. On each server, one terminal session is logged in to the MySQL client and the other is ready to issue commands. The first step is on the slave SQL session:

  STOP SLAVE;

Then in the master SQL session:

  FLUSH TABLES WITH READ LOCK; SHOW MASTER STATUS;

The master server is now effectively stopped. Make a note of the information on the master’s status and then go to the master’s command line and run:

  mysqldump -u root -p --single-transaction --replace --opt --all-databases > backup-YYYY-MM-DD.sql

Note that the option –replace is important, and is often not mentioned in instructions for resync. What it does is avoid conflicts that can arise with MyISAM tables, resulting in a failure of the restore because of duplicate keys. With the –replace option, later inserts will overwrite earlier ones.

If you are running as root and the database server is MariaDB, there is no need to specify user and password, so the command can be:

   mysqldump --single-transaction --replace --opt --all-databases > backup-YYYY-MM-DD.sql
   

I find that this is liable to hang, and currently have no solution for it, except perhaps to rely on being able to unlock when the back has stalled - see next paragraph.

People say the next command on the master’s SQL session can be done as soon as the mysqldump is started, although I have had problems doing that and I usually wait for it to complete just to be certain nothing is out of line:

  UNLOCK TABLES;

The master server is operational again after that. Now the backup can be transferred from the master to slave server. Then, at the slave command line, install the backup into the slave database:

  mysql -u root -p  < /path/to/backup-YYY-MM-YY.sql

When that is completed, the slave needs to be reset with a SQL command something like:

  CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000341', MASTER_LOG_POS=3478636, MASTER_USER='replicator',   MASTER_PASSWORD='??????', MASTER_HOST='127.0.0.1', MASTER_PORT=4002;
  START SLAVE;

That may need a fair amount of tweaking, according to your situation. The name of the log file has to be taken from the MASTER STATUS obtained earlier, as does the master log position. The command shown assumes that an SSH tunnel has been created that uses local port 4002 on the slave server and connects to the master database at the other end.

With those commands executed, the slave server should be running and replicating the master.

useful/mysql-replication.txt · Last modified: 2022/11/26 08:43 by admin