博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

上帝他爸

逆境,是上帝帮你淘汰竞争者的地方!
  yueliangdao0608.cublog.cn

关于作者
姓名:杨涛
位置:中国-北京
QQ1:38257291(已满)
QQ2:871099152
QQ群:92099217
MSN:yueliangdao0608@gmail.com
|| << >> ||
我的分类


[原创]MYSQL 的 MASTER到MASTER的主主循环同步
刚刚抽空做了一下MYSQL 的主主同步。
把步骤写下来,至于会出现的什么问题,以后随时更新。这里我同步的数据库是TEST
1、环境描述。
   主机:192.168.0.231(A)
   主机:192.168.0.232(B)
   MYSQL 版本为5.1.21
2、授权用户。
A:
mysql> grant replication slave,file on *.* to 'repl1'@'192.168.0.232' identified
 by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
B:
mysql> grant replication slave,file on *.* to 'repl2'@'192.168.0.231' identified
 by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
然后都停止MYSQL 服务器。

3、配置文件。
在两个机器上的my.cnf里面都开启二进制日志 。
A:
user = mysql
log-bin=mysql-bin
server-id       = 1
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1

B:
user = mysql
log-bin=mysql-bin
server-id       = 2
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2

至于这些参数的说明具体看手册。
红色的部分非常重要,如果一个MASTER 挂掉的话,另外一个马上接管。
紫红色的部分指的是服务器频繁的刷新日志。这个保证了在其中一台挂掉的话,日志刷新到另外一台。从而保证了数据的同步 。
4、重新启动MYSQL服务器。
在A和B上执行相同的步骤
[root@localhost ~]# /usr/local/mysql/bin/mysqld_safe &
[1] 4264
[root@localhost ~]# 071213 14:53:20 mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
/usr/local/mysql/bin/mysqld_safe: line 366: [: -eq: unary operator expected
071213 14:53:20 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

5、进入MYSQL的SHELL。
A:
mysql> flush tables with read lock\G
Query OK, 0 rows affected (0.00 sec)

mysql> show master status\G
*************************** 1. row ***************************
            File: mysql-bin.000007
        Position: 528
    Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)

B:
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status\G
*************************** 1. row ***************************
            File: mysql-bin.000004
        Position: 595
    Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
然后备份自己的数据,保持两个机器的数据一致。
方法很多。完了后看下一步。
6、在各自机器上执行CHANGE MASTER TO命令。
A:
mysql> change master to
    -> master_host='192.168.0.232',
    -> master_user='repl2',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000004',
    -> master_log_pos=595;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)


B:
mysql> change master to
    -> master_host='192.168.0.231',
    -> master_user='repl1',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000007',
    -> master_log_pos=528;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

7、查看各自机器上的IO进程和 SLAVE进程是否都开启。
A:

mysql> show processlist\G
*************************** 1. row ***************************
     Id: 2
   User: repl
   Host: 192.168.0.232:54475
     db: NULL
Command: Binlog Dump
   Time: 1590
  State: Has sent all binlog to slave; waiting for binlog to be updated
   Info: NULL
*************************** 2. row ***************************
     Id: 3
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 1350
  State: Waiting for master to send event
   Info: NULL
*************************** 3. row ***************************
     Id: 4
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 1149
  State: Has read all relay log; waiting for the slave I/O thread to update it
   Info: NULL
*************************** 4. row ***************************
     Id: 5
   User: root
   Host: localhost
     db: test
Command: Query
   Time: 0
  State: NULL
   Info: show processlist
4 rows in set (0.00 sec)

B:

mysql> show processlist\G
*************************** 1. row ***************************
     Id: 1
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 2130
  State: Waiting for master to send event
   Info: NULL
*************************** 2. row ***************************
     Id: 2
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 1223
  State: Has read all relay log; waiting for the slave I/O thread to update it
   Info: NULL
*************************** 3. row ***************************
     Id: 4
   User: root
   Host: localhost
     db: test
Command: Query
   Time: 0
  State: NULL
   Info: show processlist
*************************** 4. row ***************************
     Id: 5
   User: repl2
   Host: 192.168.0.231:50718
     db: NULL
Command: Binlog Dump
   Time: 1398
  State: Has sent all binlog to slave; waiting for binlog to be updated
   Info: NULL
4 rows in set (0.00 sec)

如果红色部分没有出现,检查DATA目录下的错误文件。

8、释放掉各自的锁,然后进行插数据测试。
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

插入之前两个机器表的对比:
A:

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb     |
| t22            |
+----------------+
B:

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb     |
| t22            |
+----------------+
从A机器上进行插入
A:
mysql> create table t11_replicas
    -> (id int not null auto_increment primary key,
    -> str varchar(255) not null) engine myisam;
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t11_replicas(str) values
    -> ('This is a master to master test table');
Query OK, 1 row affected (0.01 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb     |
| t11_replicas   |
| t22            |
+----------------+
3 rows in set (0.00 sec)

mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str                                   |
+----+---------------------------------------+
|  1 | This is a master to master test table |
+----+---------------------------------------+
1 row in set (0.00 sec)


现在来看B机器:

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t11_innodb     |
| t11_replicas   |
| t22            |
+----------------+
3 rows in set (0.00 sec)

mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str                                   |
+----+---------------------------------------+
|  1 | This is a master to master test table |
+----+---------------------------------------+
1 row in set (0.00 sec)

现在反过来从B机器上插入数据:
B:

mysql> insert into t11_replicas(str) values('This is a test 2');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str                                   |
+----+---------------------------------------+
|  1 | This is a master to master test table |
|  2 | This is a test 2                      |
+----+---------------------------------------+
2 rows in set (0.00 sec)
我们来看A
A:
mysql> select * from t11_replicas;
+----+---------------------------------------+
| id | str                                   |
+----+---------------------------------------+
|  1 | This is a master to master test table |
|  2 | This is a test 2                      |
+----+---------------------------------------+
2 rows in set (0.00 sec)

好了。现在两个表互相为MASTER。


多MASTER自增字段冲突的问题。
具体文章见:
http://dev.mysql.com/tech-resources/articles/advanced-mysql-replication.html

在邮件列表中看到有人讨论在线同步与忽略库与表的问题,具体看:
http://dev.mysql.com/doc/refman/5.1/en/replication-rules.html


发表于: 2007-12-13,修改于: 2008-03-04 16:28,已浏览3767次,有评论11条 推荐 投诉


网友评论
网友: 本站网友 时间:2007-12-13 16:06:59 IP地址:222.66.167.★
log-bin=mysql-bin
server-id       = 1
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
再加上
master_开头的选项,而不要使用Change master to 命令,开是否会出现问题,你有没有测试过啊......我意思是使用 my.cnf文件配置是否存在mysqld启动 与start slave都没有从my.cnf文件去加载my.cnf文件的配置(master_开头的)....
注意版本哦:mysql5.0.27...........也许是这个版本的问题....
怎么老是我密码错误啊..只能匿名了:eugene.........
http://www.itpub.net/thread-831154-4-1.html

Blog作者的回复:
兄弟留下EMAIL吧


网友: yueliangdao0608 时间:2007-12-13 16:15:37 IP地址:121.34.40.★
在配置文件里写的时候
master_host要写成

master-host

注意中间的字符。

网友: 本站网友 时间:2007-12-14 09:33:47 IP地址:222.66.167.★
不是这个问题....我看过MySQL官网上的注释:在my.cnf中
master-host与 master_gost 是一样的意思...我也曾测试过,MySQL中的代码会处理成一个选项........我可能已经找到问题,但是还不敢断定.......等我验证了再说.....再次恭喜你成版主了....:):)

网友: yueliangdao0608 时间:2007-12-14 09:35:20 IP地址:121.34.41.★
是不是你自己要试验一下。
还是按照手册上说的来比较好。

网友: 本站网友 时间:2008-02-28 11:20:19 IP地址:58.240.74.★
唉,mysql什么时候能像oracle那样实现多台服务器双向复制

Blog作者的回复:
这个MYSQL也可以做到。


网友: huzi1986 时间:2008-06-18 00:38:23 IP地址:116.7.88.★
grant replication slave,file on *.* to 'repl1'@'192.168.0.232' identified
 by '123456';
可以告诉我上面的
slave,file 是什么意思吗?谢谢

Blog作者的回复:
与复制相关的权限!


网友: cafemore 时间:2008-07-07 15:24:44 IP地址:58.247.7.★
看你的还是看不懂。能说说原理吗?思路是怎么样的?

网友: 本站网友 时间:2008-07-14 14:08:20 IP地址:222.223.34.★
请问第4步和第5步是必须的吗?我的机子上没有my.cnf,只有my.ini,是一样的吗?我要把第三步写在MY.INI的什么地方,写在最后面可以吗?我不知道4,5步写在什么地方?请帮忙 我的邮箱cvily1@sina.com

Blog作者的回复:
都写在[mysql]下面


网友: 本站网友 时间:2008-08-07 09:43:09 IP地址:116.76.160.★
 如果同步库的名字不相同,这样可以否?

Blog作者的回复:
为什么不搞成一样的呢?


网友: 本站网友 时间:2008-12-02 09:44:17 IP地址:61.141.158.★
在master1启动了slave之后,不小心重启了master2的server,master2就报错了。请问是什么原因?版本是mysql5.0
081202 09:37:27  mysqld started
081202  9:37:27  InnoDB: Started; log sequence number 0 43655
081202  9:37:28 [ERROR] Error reading master configuration
081202  9:37:28 [ERROR] Failed to initialize the master info structure
081202  9:37:28 [Note] /usr/libexec/mysqld: ready for connections.
Version: '5.0.45-log'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  Source distribution

网友: yueliangdao0608 时间:2008-12-02 14:49:05 IP地址:220.249.72.★
这个时候你重新改一下master_log_file 和master_log_pos 就可以了。

 发表评论