刚刚抽空做了一下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自增字段冲突的问题。
具体文章见:
[url]http://dev.mysql.com/tech-resources/articles/advanced-mysql-replication.html[/url]

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

MYSQL 的 MASTER到MASTER的主主循环同步相关推荐

  1. mysql多实例复制,Mysql实例使用Rotate Master实现MySQL 多主复制的实现方法

    搜索热词 <MysqL实例使用Rotate Master实现MysqL 多主复制的实现方法>要点: 本文介绍了MysqL实例使用Rotate Master实现MysqL 多主复制的实现方法 ...

  2. mysql 主主忽略错误_MySQL 主主报错: Fatal error: The slave I/O thread stops because master and slave have...

    Mysql 主主启动错误处理 error 信息: Slave_IO_State: Master_Host: 192.168.6.87 Master_User: replication Master_P ...

  3. mysql master slave 灾备技术_MySQL灾备恢复在线主从复制变成主主复制及多源复制【转】...

    生产主主复制(AB),和灾备主从复制(B--->C).当生产出现问题时,数据写入切换到灾备数据库,待生产恢复后,将灾备回写到生产.步骤如下: 1.灾备与生产其中一台建立主主复制,这样生产的那台就 ...

  4. mysql 5.6 主主复制_Percona MySQL 5.6 主主复制环境报错Got fatal error 1236 from master.....

    Percona MySQL 5.6 主主复制环境报错: mysql> show slave status\G *************************** 1. row ******* ...

  5. mysql启用keepalive_keepalive+mysql 主主配置

    1.   环境说明: 机器名 eth0 说明 server01 192.168.100.30/24 Mysql.keepalive server02 192.168.100.31/24 Mysql.k ...

  6. mysql keepalived低版本_Mysql+keepalived主主切换

    Mysql+keepalived主主切换 一,环境介绍 网络结构: VIP :192.168.1.30 MYSQL A:192.168.1.21 MYSQL B:192.168.1.22 二.mysq ...

  7. keepalived mysql集群_keepalived + Mysql(主主)实现高可用集群

    Master1 192.168.20.145Master2 192.168.20.146安装mysqlmysql安装脚本:#!/bin/bash yum -y install cmake tar zx ...

  8. 快速理解mysql主从,主主备份原理及实践

    双机热备的概念简单说一下,就是要保持两个数据库的状态自动同步.对任何一个数据库的操作都自动应用到另外一个数据库,始终保持两个数据库数据一致. 这样做的好处多. 1. 可以做灾备,其中一个坏了可以切换到 ...

  9. mysql主主复制、主从复制、半同步的实现

    实验前提:两台服务器 Master server:172.16.23.1 slave server:172.16.23.2 一.mysql主从服务器实现 简单介绍: MySQL支持单向.异步复制,复制 ...

最新文章

  1. 安装R语言开发环境RStudio服务器版
  2. Java Reflection(九):泛型
  3. IOS-字符串太长换行拼接
  4. 机器学习基础一(TP,TN,FP,FN等)
  5. github的使用教程
  6. Linux:sudo命令实例讲解
  7. 项目解析jsx文件_React系列二十二 云音乐项目实战
  8. [编程与人生的韵味]注重实效的哲学
  9. centos locale报错问题
  10. sublime text3 3207 下载安装破解
  11. EXcel用法——如何冻结前两行,如何删除筛选的行
  12. uni-app 图片上传插件使用说明
  13. d3.js v5 数据加载
  14. 制作一个谷歌浏览器插件,实现网页数据爬虫
  15. 作业 20180925-4 单元测试,结对
  16. 调用本地主干的预训练的.pth文件
  17. google android win10 ios,Flutter入门安装 ,win10 Android studio 教程
  18. 规格书搜索网站分享和体验
  19. C语言编程>第一周 ⑧ 输入两个正整数m和n,求其最大公约数和最小公倍数。
  20. apache中的php模块安装

热门文章

  1. 未来智能实验室成立,建设世界第一个AI智商评测和趋势研究机构
  2. Tensorflow— 下载google图像识别网络inception-v3并查看结构
  3. 《用Python进行自然语言处理》第8章 分析句子结构
  4. 为什么很难创造出新的处理器?
  5. 百度:2020年十大科技趋势
  6. 最后期限已至,高通收购恩智浦全剧终!中国一刀切断高通物联网全局梦!
  7. MIT教授Tomaso Poggio演讲与专访:智能背后的科学与工程 | 腾讯AI Lab学术论坛
  8. 剑指云内存数据库,阿里云在下一盘大棋
  9. 如何正确拒绝老板的加班要求?学起来!
  10. 啧啧,这种程序员……| 每日趣闻