MySQL5.7 main automatic synchronization scripts from the environment

Main Library IP: 192.168.8.129 hostname: master
from the library IP: 192.168.8.130 hostname: Slave
1, Description: The script for the article "CentOS7.X installation deployment mysql5.7 master-slave environment" set up after the completion of the main synchronization at any time using https://blog.51cto.com/8355320/2448056
2, MySQL master-slave synchronization variety of reasons such as network delay occurs, the main cause of sync from the environment when the need to re-sync from the master environment, need to wait until business is not busy at night or on weekends to carry out, and use a backup script, with the full database backup, with binlog log offset record values, we can achieve master-slave synchronization operation at any time
3, the main backup script:


[root@master]# cat auto_mysql_masterbak.sh

! # / bin / bash
#Author Danrtsey
#function: backup all main libraries and record master status, used to call the shots when using synchronous recovery from
BACKUP_FOLDERNAME = / the Data / masterbak
db_username = "root"
DB_PASSWORD = "password"

LOG_FILE=${BACKUPFOLDERNAME}/mysqllogsdate +%F.log
DATA_FILE=${BACKUP_FOLDERNAME}/mysqlbackupdate +%F.sql.gz

MYSQL_CMD="/bin/mysql -u$DB_USERNAME -p$DB_PASSWORD"
MYSQL_DUMP="/bin/mysqldump --set-gtid-purged=off -u$DB_USERNAME -p$DB_PASSWORD -A -B --flush-logs --single-transaction -e"

# Lock table - full backup - to view the status of the main library and record - Unlocked table
$ MYSQL_CMD -e "flush the Tables with the Read Lock;"
echo "the Result ----- ----- Show Master Status" $ LOG_FILE >>
$ MYSQL_CMD -e "Show Master Status;" >> $ LOG_FILE
$ MYSQL_DUMP {} | gzip> $ DATA_FILE
$ MYSQL_CMD -e "UNLOCK the Tables;"
# 10 to retain copies the backup file
find $ {BACKUP_FOLDERNAME} -mtime + -name 10 " .sql.gz" RM -rf -exec {} \;
Find BACKUP_FOLDERNAME} {$ -mtime +10 -name "
.log" RM -rf -exec {} \;
. 4, synchronize the script from the library

[root @ Slave] CAT auto_mysql_slavebak.sh #
! # / bin / bash
#Author Danrtsey
#function: from the library to use the main library / data / masterbak backup
# View / data / masterbak following mysqllogs record of the main library master status information
# fill in the relevant information from the library synchronization for simultaneous use at any time from the library

# Note that the change value and the value MASTER_LOG_POS MASTER_LOG_FILE corresponding, for example as follows
# MASTER_LOG_FILE = 'MySQL-bin.000002',
# MASTER_LOG_POS = 342;

BACKUP_FOLDERNAME=/data/slavebak
DB_USERNAME="root"
DB_PASSWORD="密码"

LOG_FILE=${BACKUPFOLDERNAME}/mysqllogsdate +%F.log
DATA_FILE=${BACKUP_FOLDERNAME}/mysqlbackupdate +%F.sql.gz

MYSQL_CMD="/bin/mysql -u$DB_USERNAME -p$DB_PASSWORD"

#recover
cd ${BACKUP_FOLDERNAME}
gzip -d mysqlbackupdate +%F.sql.gz
$MYSQL_CMD < mysqlbackupdate +%F.sql

$MYSQL_CMD -e "stop slave;"
#config slave
$MYSQL_CMD -e "CHANGE MASTER TO MASTER_HOST='192.168.8.129',MASTER_PORT=3306,MASTER_USER='slave',MASTER_PASSWORD='密码',MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=342;"

$MYSQL_CMD -e "start slave;"
$MYSQL_CMD -e "show slave status\G"|egrep "IO_Running|SQL_Running" >$LOG_FILE
cat $LOG_FILE


From time database synchronization, see the log shows successful synchronization yes means there are two from the library

Guess you like

Origin blog.51cto.com/8355320/2448346