Home > Database > Mysql Tutorial > Detailed explanation of MySQL's replication two-way backup configuration

Detailed explanation of MySQL's replication two-way backup configuration

零下一度
Release: 2017-04-27 10:00:43
Original
1439 people have browsed it

MySQL的replication双向备份配置,分享如下。

一、 设置A服务器

编辑/usr/local/mysql/etc/my.cnf文件,在mysqld块里添加如下内容:

log-bin=/jongo/mysql/logs/mysql-bin
server-id       = 2
binlog-do-db=bbscs8    这里的binlog-do-db是用来设置可以被slave服务器同步的数据
库名称
binlog-do-db=bdt
binlog-do-db=adsense
binlog-do-db=bdt_bbs
binlog-do-db=bdt_help
Copy after login

master-host = 192.168.2. 备份192.168.2.7上的数据库,即这台做为备机的需要备份的数据库的主机器地址

master-user = backup 上面机器上设置的,用来同步的用户名胜古迹

master-password = 17buybackup@#$ 要用主服务器上同步过来的数据库的名称

log-slave-updates

grant replication slave on *.* to 'backup'@'192.168.2.%' identified by 'bdtbackup@#$';

给同步添加一个用户

二、 设置B服务器

server-id = 1

binlog-do-db= jongo17buy 这里的binlog-do-db是用来设置可以被slave服务器同步的数据

库名称

master-host = 192.168.2.5 备份192.168.2.7上的数据库,即这台做为备机的需要备份的数据库的主机器地址

master-user = backup 上面机器上设置的,用来同步的用户名胜古迹

master-password = bdtbackup@#$ 要用主服务器上同步过来的数据库的名称

grant replication slave on *.* to 'backup'@'192.168.2.%' identified by '17buybackup@#$';

三、 两个服务器上

在两个服务器上,将要同步的数据库分别在各的备机手工同步或重新导入一下。

然后,分别启动 mysql> start slave

四、 同步监控脚本

mysql_monitor.sh文件
#!/bin/bash
. /root/.bash_profile
. /jongo/shell/contact_config.sh
MYSQL_PASSWD="admin8*"
IO_RUNING=`echo "show slave status \G;" |mysql -uroot -p$MYSQL_PASSWD |grep "Slave_IO_Running" |awk -F ":" '{ print $2 }'`
SQL_RUNING=`echo "show slave status \G;" |mysql -uroot -p$MYSQL_PASSWD |grep "Slave_SQL_Running" |awk -F ":" '{ print $2 }'`
SQL_ERROR=`echo "show slave status \G;" |mysql -uroot -p$MYSQL_PASSWD |grep "Last_SQL_Error" |awk -F ":" '{ print $2 }'`
echo "io_status=\"$IO_RUNING\"" >/jongo/shell/tmp.txt
echo "sql_status=\"$SQL_RUNING\"">>/jongo/shell/tmp.txt
echo "sql_error=\"$SQL_ERROR\"">>/jongo/shell/tmp.txt
. /jongo/shell/tmp.txt
if [ "$io_status" != " Yes" -o "$sql_status" != " Yes" ]
    then
        echo "17buy的mysql数据库的slave(203.156.192.29) 于"`date "+%Y-%m-%d %H:%m:%S"`"发生错误:">/jongo/shell/mysql_monitor.log
     echo "请立即检查,谢谢!" >>/jongo/shell/mysql_monitor.log
     echo "slave 服务器的状态如下:">>/jongo/shell/mysql_monitor.log
     echo "show slave status \G;" |mysql -uroot -p$MYSQL_PASSWD >>/jongo/shell/mysql_monitor.log
     for email in $contact_email
      do
        mail -s "mysql slave on 17buy was false" $email /shell/mysql_monitor.log
         done
fi
contact_config.sh文件
contact_email=wayne.wang@jongogroup.com
Copy after login

五、 常见问题

a. 出现Error initializing relay log position: Could not find target log during relay log initialization,Error_code: 1593错误

~ 这个一般是由于在mysql数据库的数据目录下找不到localhost-relay-bin.***文件导致的,可以用类似下列语句来调整:

CHANGE MASTER TO
    ->    MASTER_HOST='192.168.2.5’,
    ->    MASTER_USER='backup',
    ->    MASTER_PASSWORD='./bdtbackup@#$',
    ->    MASTER_PORT=3306,
    ->    MASTER_LOG_FILE='master2-bin.001',
    ->    MASTER_LOG_POS=4,
    ->    MASTER_CONNECT_RETRY=10,CHANGE MASTER TO
    ->    RELAY_LOG_FILE='./slave-relay-bin.006',
    ->    RELAY_LOG_POS=4025;
Copy after login

注意:

这里指定的文件路径是绝对路径,如果要用相对路径一定要用./filename形式,否报找不到对应的文件错误;

b. 出现Slave I/O: error connecting to master 'backup@192.168.2.5:3306' - retry-time: 60 retries: 86400, Error_code: 1045 错误:

出现这个错误可能有:

1) 用户的权限不对;

2) 主服务器不上连接,如防火墙没有打开相应的端口;

3) 从服务器上的RELAY_LOG_POS的值不对,可以用上面的CHANGE MASTER TO语句来调整

c. 同步出错处理

当服务器同步出错了,可以有以下两种方法来处理它:

(a) 如果只是数据更新或者插入错误,而且对将来的影响不是很大,这个数据可以不要时,可以:

stop slave;
SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1:
Copy after login

将这条出错的查询跳掉就可以了,然后:
start slave; 重启同步过程就可以了

(b) 另外的方法是 :当服务器出现严重的问题时可以:
stop slave
然后用其他软件手动同步一下,然后再主机上:

show master status;
Copy after login

查到master_log_file和master_log_pos,然后再从机器上:

change master to master_log_file='mysql-bin.000007',master_log_pos=261836644 ;
start slave
Copy after login

就有可以同步了

The above is the detailed content of Detailed explanation of MySQL's replication two-way backup configuration. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template