简体   繁体   中英

How to restore a slave from a mysql backup?

I'm running MySql 5.1. I have Master and a Slave on 2 machines and I set up replication.

I do periodic backup on my slave server. I stop mysql, I copy all the files and I restart mysql.

In case I lose the Master, I can set up a new one from the last backup.

What If I lose the Slave? Can I restart the slave from the last backup? Am I supposed to keep track of the position of the replication every time I to a backup?

The way I solved this problem was:
Take monthly (or weekly/daily) backup, reset master so it restarts the log files. Pipe backup to the master so it refills the database one table at a time. Restart the slaves.
I had many more slaves and the table reloads didn't take too long. If your backup takes too long, you may want to do this a different way.
If you lose a slave, you can just have it restart through the log files as long as you start them with a table reload. If you backup from one of the slaves, it's critical to insure it's in sync with the master first.

There may be other ways to do this, but having a log file fresh start periodically became very useful.
Master Code to go in cron (This is from a way back, you should verify it works for you):

#!/usr/bin/ksh
date=`date +%y%m%d`
mysql -u root db_name -e "flush tables with read lock;"
mysqldump -u root -pYrPass --add-drop-table --add-locks natl_inv > /path/to/backup/db_name$date
mysql -u root -e "reset master;"
mysql -u root db_name -e "unlock tables;"
mysql -u root –pYrPass db_name < /path/to/backup/db_name$date
mysql -u root -e "flush logs;"

On the slaves: use show slave status command to verify you are in sync with the master. If you want to resync to the master, run:

slave stop;
reset slave;
slave start;

You may need to stop mysql, delete the slave bin log files then restart and run the above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM