参考百度链接 https://jingyan.baidu.com/article/8275fc869f110c46a03cf6e6.html
主mysql 服务器ip 192.168.13.177 备mysql服务器ip 192.168.13.187
实现两台mysql同时备份
首先两台服务器能互通,设置mysql能远程连接
1、在主mysql(ip192.168.13.177) 切换root账号
2、登录mysql 执行命令 mysql -uroot -proot
3、执行下面的命令
grant replication slave on . to ‘root’@‘192.168.13.187’ identified by ‘root’;
flush privileges;
4、设置主mysql my.cnf文件,需要把下面的代码复制进去就行,修改你要同步的数据就行
binlog-do-db=wechat #需要记录进制日志的数据库.如果有多个数据库可用逗号分隔,或者使用多个binlog-do-db选项
binlog-ignore-db=mysql #不需要记录进制日志的数据库.如果有多个数据库可用逗号分隔,或者使用多个binlog-do-db选项
replicate-do-db=wechat #需要进行同步的数据库.如果有多个数据库可用逗号分隔,或者使用多个replicate-do-db选项
replicate-ignore-db=mysql,information_schema #不需要同步的数据库.如果有多个数据库可用逗号分隔,或者使用多个replicate-ignore-db选项
#同步参数:
server-id=1
log_bin = /var/log/mysql/mysql-bin
#保证slave挂在任何一台master上都会接收到另一个master的写入信息
log-slave-updates
sync_binlog=1
auto_increment_offset=1
auto_increment_increment=2
slave-skip-errors=all #过滤掉一些没啥大问题的错误
5、重启mysql 服务
service mysqld restart
6、登录mysql 命令 mysql -uroot -proot
执行刷新日志命令 flush logs;
查看日志
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 106
Binlog_Do_DB: wechat
Binlog_Ignore_DB:
1 row in set (0.00 sec)
把查看的日志 File: mysql-bin.000001 、 Position: 106提供给备份mysql使用
7、登录备份mysql (ip192.168.13.187) 切换root账号
8、登录mysql 执行命令 mysql -uroot -proot
9、执行下面的命令
grant replication slave on . to ‘root’@‘192.168.17.157’ identified by ‘root’;
flush privileges;
10、设置主mysql my.cnf文件,需要把下面的代码复制进去就行,修改你要同步的数据就行
mysql 备 my.cnf 文件
server-id=2 #设置一个不同的id、
binlog-do-db=wechat #需要记录二进制日志的数据库.如果有多个数据库可用逗号分隔,
binlog-ignore-db=mysql #不需要记录进制日志的数据库.如果有多个数据库可用逗号分隔
#需要同步的数据库
replicate-do-db=wechat
#需要进行同步的数据库.如果有多个数据库可用逗号分隔,或者使用多个binlog-do-db选项
replicate-ignore-db=mysql,information_schema #不需要同步的数据库.多个数据库可用逗号分隔
#同步参数:
log-bin =mysql-bin
#保证slave挂在任何一台master上都会接收到另一个master的写入信息
log-slave-updates
sync_binlog=1
auto_increment_offset=2
auto_increment_increment=2
slave-skip-errors=all #过滤掉一些没啥大问题的错误
11、重启mysql 服务
service mysqld restart
12、登录mysql 命令 mysql -uroot -proot
执行stop slave;
CHANGE MASTER TO
MASTER_HOST=‘192.168.17.157’,
MASTER_USER=‘root’,
MASTER_PASSWORD=‘root’,
MASTER_LOG_FILE=‘mysql-bin.000013’,
MASTER_LOG_POS=106;
执行 start slave;
执行
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.13.177
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 106
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: wechat
Replicate_Ignore_DB: mysql,information_schema
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 106
Relay_Log_Space: 407
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
看到两个yes就行了
然后做去主mysql数据添加一个数据测试,看看备份mysql服务器有没有数据(成功了,下一步)
13、在备份服务器 里面查看日志情况
执行刷新日志命令 flush logs;
查看日志
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000005
Position: 106
Binlog_Do_DB: wechat
Binlog_Ignore_DB:
1 row in set (0.00 sec)
把查看的日志 File: mysql-bin.000005 、 Position: 106提供给主mysql使用
14、切换主mysql 服务器
执行stop slave;
mysql> CHANGE MASTER TO
-> MASTER_HOST=‘192.168.13.187’,
-> MASTER_USER=‘root’,
-> MASTER_PASSWORD=‘root’,
-> MASTER_LOG_FILE=‘mysql-bin.000005’,
-> MASTER_LOG_POS=106;
Query OK, 0 rows affected (0.10 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.13.187
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 106
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: wechat
Replicate_Ignore_DB: mysql,information_schema
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 106
Relay_Log_Space: 407
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
然后在做测试就行了!