Mysql双向同步复制

 

Mysql双向同步复制  

 

http://zhumeng8337797.blog.163.com/blog/static/1007689142011590160330/

2011-06-09 00:16:00|  分类: my复制 |  标签:mysql  test1  master  slave  同步  |字号 订阅

 
 
请参考文章《Mysql主从复制详解》做以下实验:
 
1、修改原slave 服务器配置
// 配置原slave服务器/etc/my.cnf文件,添加红字的内容:
server-id=2                          # 从服务器ID号,不要和主ID相同 
master-host=192.168.20.155           # 指定主服务器IP地址
master-user=replication              # 指定在主服务器上可以进行同步的
                                       用户名
master-password=123456               # 密码
master-port=3306                     # 同步所用端口
master-connect-retry=60              # 断点从新连接时间
replicate-ignore-db=mysql            # 屏蔽对mysql库的同步
replicate-do-db=test1                # 同步的数据库的名称
log-bin=/var/log/mysql/updatelog     # 设定生成log文件名
binlog-do-db=test1                   # 设置同步数据库名
binlog-ignore-db=mysql               # 避免同步mysql用户配置,以免不必
                                       要的麻烦
2、创建更新日志的目录并给mysql用户的权限
# mkdir /var/log/mysql
# chown -R mysql.mysql /var/log/mysql
3、重新启动mysql服务,创建一个同步专用账号
# service mysqld restart
//给与从服务器用户replication的同步权限
# mysql -uroot -p123456
mysql>  GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.20.%'IDENTIFIED BY '123456';
//刷新权限,使设置生效
mysql>Flush privileges;
 
4、修改原master配置文件
// 配置原master务器/etc/my.cnf文件,添加红字的内容:
log-bin=mysql-bin                   #  启动二进制日志系统
server-id=1                         # 本机数据库ID 标示为主log-bin=/var/log/mysql/updatelog        # 设定生成log文件名,这里的路径没
                                      有mysql目录要手动创建并给于它
                                      mysql用户的权限。
binlog-do-db=test1                  # 二进制需要同步的数据库名
binlog-ignore-db=mysql,test         # 避免同步mysql用户配置,以免不必
                                      要的麻烦
master-host=192.168.20.128          # 设置从原slave数据库同步更新
master-user=replication             # 更新用户
master-password=123456              # 密码
master-port=3306                    # 端口
replicate-do-db=test1               # 需要更新的库
// 重启mysql服务
# service mysqld restart
//在原从服务器查询
# mysql -uroot -p123456
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| updatelog.000001 |      106 | test1        | mysql            |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
//在原主服务器查询
# mysql -uroot -p123456
mysql> SHOW MASTER STATUS;
//先停止slave服务
mysql> SLAVE STOP;
mysql> CHANGE MASTER TO MASTER_HOST='192.168.20.128',MASTER_USER='replication',MASTER_PASSWORD='123456',MASTER_PORT=3306,MASTER_LOG_FILE='updatelog.000001',MASTER_LOG_POS=106;
// 根据上面主服务器的show master status的结果,进行从服务器的二进制数据库记录回归,达到同步的效果
//启动从服务器同步服务
mysql> SLAVE START;
 
5、验证配置
//在beijing原主服务器上进入mysql命令行
mysql> SHOW SLAVE STATUS\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
此处Slave_IO_Running ,Slave_SQL_Running 都应该是yes,表示从库的I/O,Slave_SQL线程都正确开启.表明数据库正在同步。
//在shanghai原从服务器上进入mysql命令行
mysql> SHOW SLAVE STATUS\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
此处Slave_IO_Running ,Slave_SQL_Running 都应该是yes,表示从库的I/O,Slave_SQL线程都正确开启.表明数据库正在同步。
6、测试
//在beijing服务器上建立一个表
mysql> use test1;
mysql> create table test1(id int(4),name varchar(20));
mysql> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| name            |
| test1           |
| user            |
+-----------------+
3 rows in set (0.00 sec)
//在shanghai服务器上查询
mysql> use test1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql>  show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| name            |
| test1           |
| user            |
+-----------------+
3 rows in set (0.00 sec)
//在shanghai服务器上建立一个表
mysql> create table test2(id int(4),name varchar(20));
mysql> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| name            |
| test1           |
| test2           |
| user            |
+-----------------+
4 rows in set (0.00 sec)| user            |
+-----------------+
3 rows in set (0.00 sec)
//在beijing服务器上查询
mysql> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| name            |
| test1           |
| test2           |
| user            |
+-----------------+
4 rows in set (0.00 sec)
双向复制试验成功!!!

你可能感兴趣的:(mysql)