├── README.md └── Re-SyncMysqlMasterSlaveReplication.sh /README.md: -------------------------------------------------------------------------------- 1 | # Re-SyncMysqlMasterSlaveReplication 2 | How to Reset (Re-Sync) MySQL Master-Slave Replication 3 | 4 | Prerequisites: 5 | 6 | 1) this tool must be run on master server 7 | 8 | 2) ssh access to both servers (master & slave) 9 | 10 | 2) ssh access from master to slave using ssh without pass (google: ssh without password) 11 | 12 | Some help at this point. 13 | Use ssh to login to your server under the account name you want to use. 14 | When prompting for passphrase hit enter. 15 | 16 | Steps: 17 | 18 | On master server: 19 | 20 | root@master-server$ cd ~/.ssh 21 | root@master-server$ ssh-keygen 22 | root@master-server$ ssh-copy-id -i ~/.ssh/id_rsa.pub slave-server 23 | # test connection 24 | root@master-server$ ssh slave-server 25 | 26 | If do not work, please google for some tutorial 27 | 28 | -------------------------------------------------------------------------------- /Re-SyncMysqlMasterSlaveReplication.sh: -------------------------------------------------------------------------------- 1 | ####################################################################### 2 | # Developer: Maykel Curbelo Pruna # 3 | # Email: mcurbelop@gmail.com # 4 | # Date: 03/02/2015, Version: 1.0 # 5 | # Important: # 6 | # 1) Use this code at your own risk # 7 | # 1) Test first on development environment # 8 | # 2) Make a backup of your database before using this code # 9 | ####################################################################### 10 | 11 | ############ Configuration section, change everything you need ######################### 12 | 13 | DB_NAME="db_name" 14 | 15 | SLAVE="full_dns_or_IP_slave_server" 16 | SLAVE_USER="mysql_user_slave_server" 17 | SLAVE_PASS="mysql_password_slave_server" 18 | 19 | MASTER="full_dns_or_IP_master_server" 20 | MASTER_USER="mysql_user_master_server" 21 | MASTER_PASS="mysql_password_master_server" 22 | 23 | TEMPORAL_WORKING_PATH="/root" 24 | 25 | 26 | 27 | ############ I suggest you do not change anything after here ######################### 28 | 29 | DATE_TIME=`date +%Y-%m%-d-%H-%M-%S` 30 | DUMPED_DB_PATH="$TEMPORAL_WORKING_PATH/$DB_NAME.sql_$DATE_TIME" 31 | 32 | echo -e "\nMaking backup of db: $DB_NAME\n" 33 | ssh $SLAVE "mysqldump -u $SLAVE_USER -p$SLAVE_PASS $DB_NAME > $DUMPED_DB_PATH" 34 | echo "We make a backup of your database (DB_NAME) on $DUMPED_DB_PATH" 35 | 36 | echo -e "\nStoping SLAVE ($SLAVE)\n" 37 | ssh $SLAVE "mysql -u $SLAVE_USER -p$SLAVE_PASS -e 'STOP SLAVE;'" 38 | 39 | echo -e "\nReset & Flush master\n" 40 | mysql -u $MASTER_USER -p$MASTER_PASS -e 'RESET MASTER;' 41 | mysql -u $MASTER_USER -p$MASTER_PASS -e 'FLUSH TABLES WITH READ LOCK;' 42 | 43 | echo -e "\nDumping $DB_NAME on $MASTER\n" 44 | mysqldump -u $MASTER_USER -p$MASTER_PASS $DB_NAME > /tmp/$DB_NAME.sql 45 | 46 | echo -e "\nUnlock the tables at master ($MASTER)\n" 47 | mysql -u $MASTER_USER -p$MASTER_PASS -e 'UNLOCK TABLES;' 48 | 49 | echo -e "\nCopying dumped db to SLAVE ($SLAVE) \n" 50 | rsync -avz /tmp/$DB_NAME.sql $SLAVE:/tmp/$DB_NAME.sql 51 | 52 | echo -e "\nRestoring DB ($DB_NAME) on SLAVE ($SLAVE)\n" 53 | ssh $SLAVE "/usr/bin/mysql -u $SLAVE_USER -p$SLAVE_PASS $DB_NAME < /tmp/$DB_NAME.sql" 54 | 55 | echo -e "\nReset SLAVE & Change master_log_file to pos 1\n" 56 | ssh $SLAVE "mysql -u $SLAVE_USER -p$SLAVE_PASS -e 'RESET SLAVE;'" 57 | ssh $SLAVE "mysql -u $SLAVE_USER -p$SLAVE_PASS -e 'CHANGE MASTER TO MASTER_LOG_FILE='\''mysql-bin.000001'\'', MASTER_LOG_POS=1;'" 58 | ssh $SLAVE "mysql -u $SLAVE_USER -p$SLAVE_PASS -e 'START SLAVE;'" 59 | 60 | echo -e "\n\nAll Done !!\n\n" 61 | --------------------------------------------------------------------------------