免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1515 | 回复: 0
打印 上一主题 下一主题

[MySQL]master-master replication【原创】 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-23 16:20 |只看该作者 |倒序浏览

                                                Master1:10.20.15.120
Master2:10.20.15.121
Step1:按照MySQL 5.0 Reference Manual中2.4.15. MySQL Installation Using a Source Distribution安装MySQL。Master1/Master2执行相同的操作。
Step2: 配置'root'@'localhost'的密码
On Master1:
[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.0.67-log Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use mysql;
mysql> update user set Password=password('10.20.15.120') where user='root' and host='localhost';
mysql> flush privileges;
mysql> exit;
On Master2:
[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.0.67-log Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use mysql;
mysql> update user set Password=password('10.20.15.121') where user='root' and host='localhost';
mysql> flush privileges;
mysql> exit;
Step3: 关闭mysqld进程。
On Master1/Master2 分别执行
[root@localhost ~]# mysqladmin -p shutdown
Step4:修改/etc/my.cnf配置
在Master1的/etc/my.cnf配置如下:
[client]
port            = 3306
socket          = /tmp/mysql.sock
[mysqld]
port            = 3306
socket          = /tmp/mysql.sock
default-character-set = utf8
skip-locking
skip-federated
log-bin=mysql-bin    #开启二进制日志
server-id       = 120    #server-id必须配置,且Master1/Master2的server-id必须唯一。
replicate-do-db = test_db1    #指定复制的db
replicate-ignore-db = mysql    #指定不复制的db,可以有多行
auto_increment_increment = 2
auto_increment_offset = 1 #以上两行的目的是避免在有auto_increment的字段的表同时被Master1/Master2插入值时中引起的自增长主键的冲突问题
[mysql]
auto-rehash
default-character-set = utf8
在Master2的/etc/my.cnf配置如下:
[client]
port            = 3306
socket          = /tmp/mysql.sock
[mysqld]
port            = 3306
socket          = /tmp/mysql.sock
skip-locking
default-character-set = utf8
skip-federated
log-bin=mysql-bin
server-id       = 121
replicate-do-db = test_db1
replicate-ignore-db = mysql
auto_increment_increment = 2
auto_increment_offset = 2
[mysql]
default-character-set = utf8
auto-rehash
Step5: 启动mysqld进程。
On Master1/Master2:
[root@localhost ~]# mysqld_safe --user=mysql &
Step6:授权REPLICATION SLAVE
On Master1:
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication1'@'10.20.15.121' IDENTIFIED BY 'replication1';
mysql> flush privileges;
On Master2:
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication2'@'10.20.15.120' IDENTIFIED BY 'replication2';
mysql> flush privileges;
Step7:记录master状态,执行change master to...
On Master1:
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.01 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |     2379 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
On Master2:
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.06 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |     1902 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.01 sec)

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

On Master1:
mysql> CHANGE MASTER TO MASTER_HOST='10.20.15.121', MASTER_PORT=3306,MASTER_USER='replication2', MASTER_PASSWORD='replication2',MASTER_LOG_FILE='mysql-bin.000004',MASTER_LOG_POS=1902;
mysql> start slave;
mysql> show slave status;
On Master2:
mysql> CHANGE MASTER TO MASTER_HOST='10.20.15.120', MASTER_PORT=3306,MASTER_USER='replication1',MASTER_PASSWORD='replication1',MASTER_LOG_FILE='mysql-bin.000004',MASTER_LOG_POS=2379;
mysql> start slave;
mysql> show slave status;
至此,我们的master-master replication模式配置起来了。在Master1上,执行
mysql> create database test_db1;
mysql> use database test_db1;
mysql> create table test_tb1(id int auto_increment primary key,name varchar(10)) engine=myisam;
在Master1/Master2上同时对表test_tb1执行insert语句。
可以看到在Master1/Master2几乎同时看到表test_tb1中最新最完整的数据记录。
关于replication的选项的含义和使用方法请参考:
http://dev.mysql.com/doc/refman/5.0/en/replication-options.html

               
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/74751/showart_1736816.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP