分类:
2010-04-12 10:33:50
假设同步Master的主机名为:A(IP:192.168.0.1),Slave主机名为:B(IP:192.168.0.2),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=test
binlog-ignore-db=mysql
重启MySQL,创建一个MySQL帐号为同步专用
GRANT REPLICATION SLAVE ON *.* TO IDENTIFIED BY '123456';
FLUSH PRIVILEGES ;
FLUSH TABLES WITH READ LOCK;
2、设置同步Slave
修改my.cnf文件,添加
server-id = 2
master-host = 192.168.0.1
master-user = back
master-password = back
master-port = 3306
replicate-ignore-db=mysql (我的是Ver 14.14 Distrib 5.1.22-rc版,这个参数好像用不上)
replicate-do-db=test
重启MySQL
3、启动同步
在主服务器A MySQL命令符下:
show master status;
显示(当然这个是我机器的情况,你的不可能跟我一样哈,只是个例子):
+------------------+----------+-------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+-------------------+------------------+
| updatelog.000028 | 313361 | test | mysql |
+------------------+----------+-------------------+------------------+
在从服务器A MySQL命令符下:
slave stop;
CHANGE MASTER TO MASTER_LOG_FILE='solaris8-bin.000008',MASTER_LOG_POS=237;
CHANGE MASTER TO MASTER_HOST='172.16.100.202', MASTER_USER='bak',Master_Port=3306,MASTER_PASSWORD='123456',MASTER_LOG_FILE='solaris8-bin.000001',MASTER_LOG_POS=79;
slave start;
用show slave status\G;看一下从服务器的同步情况
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如果都是yes,那代表已经在同步
往表里面写点数据测试一下看是否同步成功,如果不成功,绝对不是你的RP问题,再检查一下操作步骤!
4、设置双向同步
修改B服务器的my.cnf,添加
log-bin=/var/log/mysql/updatelog
binlog-do-db=test
binlog-ignore-db=mysql
重启MySQL,创建一个MySQL帐号为同步专用
GRANT REPLICATION SLAVE,RELOAD,SUPER, ON *.* TO IDENTIFIED BY 'back' ;
FLUSH PRIVILEGES ;
修改A服务器的my.cnf,添加
master-host = 192.168.0.2
master-user = back
master-password = back
master-port = 3306
replicate-ignore-db=mysql
replicate-do-db=test
重启MySQL
在主服务器B MySQL命令符下:
show master status;
+------------------+----------+-------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+-------------------+------------------+
| updatelog.000028 | 13753 | test | mysql |
+------------------+----------+-------------------+------------------+
在服务器A MySQL命令符下:
slave stop;
CHANGE MASTER TO MASTER_LOG_FILE='updatelog.000028',MASTER_LOG_POS=13753;
slave start;
其实也就是A->B单向同步的反向操作