resync a master-slave replication

Source: https://stackoverflow.com/questions/2366018/how-to-re-sync-the-mysql-db-if-master-and-slave-have-different-database-incase-o, David Espart 2010

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!

MySQL replication CHANGE MASTER TO master_log_file

master Server

show master status;

+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000177 | 31692464 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.000 sec)

File = mysql-bin.000177, Position = 31692464

slave Server

stop slave;
CHANGE MASTER TO master_log_file = 'mysql-bin.000177', master_log_pos =31692464;
start slave;
show slave status \G;

Backup script example, Auto Make directory, Prepare date file name, dump mySQL, Auto rename old backup files with index code, rsync data

#!/bin/bash
basedir=/var/backup/store
sqldir=$basedir/sql
PATH=/bin:/usr/bin:/sbin:/usr/sbin; export PATH
export LANG=C

for dirs in $basedir
do
        [ ! -d "$dirs" ] && mkdir -p $dirs
done

for dirs in $sqldir
do
        [ ! -d "$dirs" ] && mkdir -p $dirs
done



param1=$(date +%Y-%m-%d-%H-%M-%S)

# Database Backup
sqlusername=root
sqlpassword=Z2PG82SMtQzD
mysqldump=/usr/bin/mysqldump
bwlimit=--bwlimit=100K
#bwlimit=

sqldbname=dbispconfig
sqlsavetarget=$sqldir/$sqldbname.sql
rm -rf $sqlsavetarget.7
mv $sqlsavetarget.6 $sqlsavetarget.7
mv $sqlsavetarget.5 $sqlsavetarget.6
mv $sqlsavetarget.4 $sqlsavetarget.5
mv $sqlsavetarget.3 $sqlsavetarget.4
mv $sqlsavetarget.2 $sqlsavetarget.3
mv $sqlsavetarget.1 $sqlsavetarget.2
mv $sqlsavetarget $sqlsavetarget.1
$mysqldump -u $sqlusername -p$sqlpassword --skip-lock-tables -R $sqldbname > $sqlsavetarget

/usr/bin/rsync -ah $bwlimit --delete /etc $basedir
/usr/bin/rsync -ah $bwlimit --delete /usr/local/ispconfig $basedir

Fixed MySQL replication slave got Last_SQL_Errno: 1062

If you don’t want REDO the complete procedure, a good fix would be to use

STOP SLAVE;    
SET GLOBAL sql_slave_skip_counter=1;
START SLAVE;

If there are too many such errors, a good idea would be to automate it using a bash script.

Reference : Ut xD(2017), https://serverfault.com/questions/336888/what-causes-the-mysql-error-1062-duplicate-entry-when-starting-slave, Last visit at Jan 2022