设置MySQL数据双向同步
一、准备服务器
由于MySQL不同版本之间的(二进制日志)binlog格式可能会不一样,因此最好的搭配组合是Master的MySQL版本和Slave的版本相同或者更低,Master的版本肯定不能高于Slave版本。
more.. | less.. | 本文中,我们假设主服务器(以下简称Master)和从服务器(以下简称Slave)的版本都是5.0.27,操作系统是RedHat Linux 9。
假设同步Master的主机名为:master(IP:192.168.1.123),Slave主机名为:slave(IP:192.168.1.124),2个MySQL的basedir目录都是/usr/local/mysql,datadir都是:/var/lib/mysql。
二、设置同步服务器
1、设置同步Master
修改 my.cnf 文件,在
# Replication Master Server (default)
# binary logging is required for replication
添加如下内容:
-
#log-bin=/var/log/mysql/updatelog
-
server-id = 1
-
binlog-do-db=discuz
-
binlog-ignore-db=mysql
重启MySQL,创建一个MySQL帐号为同步专用
-
# /usr/local/mysql/bin/mysql -u root -p
-
mysql> GRANT REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
-
如果想要在Slave上有权限执行 "LOAD TABLE FROM MASTER" 或 "LOAD DATA FROM MASTER" 语句的话,必须授予全局的 FILE 和 SELECT 权限:
-
mysql>GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
-
mysql> FLUSH PRIVILEGES ;
2、设置同步Slave
修改my.cnf文件,添加
-
server-id = 2
-
master-host = 192.168.1.123
-
master-user = back
-
master-password = back
-
master-port = 3306
-
replicate-ignore-db=mysql
-
replicate-do-db=discuz
重启MySQL
3、启动同步
在主服务器master MySQL命令符下
-
# /usr/local/mysql/bin/mysql -u root -p
-
mysql> show master status;
显示(当然这个是我机器的情况,你的不可能跟我一样哈,只是个例子):
+------------------+----------+-------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+-------------------+------------------+
| mysql-bin.000009 | 98 | discuz | mysql |
+------------------+----------+-------------------+------------------+
在从服务器master MySQL命令符下
-
# /usr/local/mysql/bin/mysql -u root -p
-
mysql> slave stop;
-
mysql> change master to master_host='192.168.1.123', master_user='back', master_password='back', master_log_file='mysql-bin.000009', master_log_pos=98;
-
mysql> slave start;
用show slave status/G;看一下从服务器的同步情况
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如果都是yes,那代表已经在同步
往表里面写点数据测试一下看是否同步成功,如果不成功,绝对不是你的RP问题,再检查一下操作步骤!
4、设置双向同步
修改slave服务器的my.cnf,添加
-
log-bin=/var/log/mysql/updatelog
-
binlog-do-db=discuz
-
binlog-ignore-db=mysql
重启MySQL,创建一个MySQL帐号为同步专用
-
mysql> GRANT REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
-
mysql> GRANT FILE,SELECT,REPLICATION SLAVE ON *.* TO [email=]'back'@'%'[/email] IDENTIFIED BY 'back';
-
mysql> FLUSH PRIVILEGES ;
修改master服务器的my.cnf,添加
-
master-host = 192.168.1.124
-
master-user = back
-
master-password = back
-
master-port = 3306
-
replicate-ignore-db=mysql
-
replicate-do-db=discuz
重启MySQL
在主服务器slave MySQL命令符下:
show master status;
+------------------+----------+-------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+-------------------+------------------+
| mysql-bin.000013 | 98 | discuz | mysql |
+------------------+----------+-------------------+------------------+
在服务器A MySQL命令符下:
-
mysql> slave stop;
-
mysql> change master to master_host='192.168.1.124', master_user='back', master_password='back', master_log_file='mysql-bin.000013', master_log_pos=98;
-
mysql> slave start;
注意:关于要复制多个数据库时,binlog-do-db和replicate-do-db选项的设置,网上很多人说是用半角逗号分隔,经过测试,这样的说法是错误的,MySQL官方文档也明确指出,如果要备份多个数据库,只要重复设置相应选项就可以了。
比如:
binlog-do-db=a
binlog-do-db=b
replicate-do-db=a
replicate-do-db=b
(责任编辑:IT) |