MySQL master-master replication 深入了解
来源:互联网 发布:linux 添加www用户组 编辑:程序博客网 时间:2024/04/19 21:40
MySQL企业版和社区版提供master-slave异步的replication机制,MySQL cluster提供同步的replication机制,关于cluster replication机制本文不做叙述,Mysql可以配置为互为master-slave,这样就相当于可以做到master-master,配置步骤就是将以下配置在相反的lab上再做一次。
架设步骤: [Master Server 方面] [Slave Server 方面] [Slave Server] 一、MasterServer 方面 -1 之间。但为了方便识别,通常我们会把这个值设定为 IP 的最后一组数字,例如若 IP为 192.168.1.1,则 server-id 设定为 1; 若 IP 为 192.168.1.2,则 server-id 设定为 2。 引用: MySQL 的 Binary Log 会将所有对于数据库的修改操作全部记录起来,而 Slave 与 Master 之间进行数据同步的方式很简单,就是 Slave 会把 Master Server 的 Binary Log 拿过来执行,也就是说 Slave Server 会 "重做" 在 Master Server 上发生的各种修改 操作。因此 MasterServer 勿必要开启 BinaryLog 功能,否则 Master/Slave架构无法运作。 C.设定 Replication Slave 权限 Slave Priviledges),所需使用的指令如下: 引用: 意思为: 此时您可以从 SlaveServer(192.168.1.2) 使用 mysql client program 进行验证,看是否有正确的开启权限,例如使用以下的指令 :(注意,是在 Slave Server 上进行验证) 引用: 接着系统会要求您输入密码,若可以顺利登入即表示设定成功。 A.设定 Server-id 引用: 引用: MASTER_HOST='192.168.1.1', 意思为: C.启动与检查 Slave Server 的状态 当你执行 'STARTslave;' 后,可使用以下的指令来检查执行状态: 引用: 执行后应该会看到如下的报表: 引用: 重点是那三行: Slave_IO_Running: Slave_SQL_Running:
[事前准备]
确保 Master 与 Slave 之间的数据一致
设定 Server-id
开启 Binary Log
设定 ReplicationSlave 权限
设定 Server-id
将 Master_Host 设定为 Master Server
启动与检查 SlaveServer 的状态
在此范例中我们假设:
[Master Server]
IP 为 192.168.1.1
server-id 为 1
进行数据同步时所使用的帐户信息:
账号:slave_server
密码: 12345678
IP 为 192.168.1.2
server-id 为 2
Master/Slave Server 架设
A.设定 Server-id
首先要设定server-id。基本上没有什么特别的限制,只要 Master 和 Slave 的 server-id 不一样即可,但其值必需为 1 至 2^32
[mysqld]
server-id=1
B.开启 Binary Log
修改 MySQLServer 的系统配置文件,在[mysqld] 下方加上log-bin=mysql-bin,例如:
[mysqld]
log-bin=mysql-bin
我们必须要在 MasterServer 上做设定,让 Slave 具有可以从 Master Server 上 Copy 数据的权限(正式的说法为 Replication
GRANT REPLICATION SLAVE ON *.* TO 'slave_server'@'192.168.1.2',
IDENTIFIED BY '12345678'
允许 192.168.1.2这个 IP 使用 slave_server 账号,来进行数据同步(Replication)。
slave_server 这个账号的密码为12345678。
mysql -h 192.168.1.1 -u slave_server -p
二、Slave Server方面
在 Slave 我们将其设定为 2:
[mysqld]
server-id=2
B.将 Master_Host 设定为 Master Server
我们必须要明确的告诉Slave Server 哪一台 Server 才是 Master Server,使用以下的指令即可:
CHANGE MASTER TO
MASTER_PORT=3306, MASTER_USER='slave_server',
MASTER_PASSWORD='12345678';
以上可直接写入my.cnf,注意用小写
Master Server 是 192.168.1.1
使用 TCP Port3306 连接
使用slave_server 这个账号登入
登入时使用的密码为12345678
设定好后,Master/Slave机制仍未启动,您必须要使用以下的指令来开启或关闭 Master/Slave 机制:
START slave; (启动Master/Slave 机制)
STOP slave; (停止Master/Slave 机制)
SHOW SLAVE STATUS /G
mysql> SHOW SLAVE STATUS /G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.1
Master_User: slave_server
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000019
Read_Master_Log_Pos: 16492717
Relay_Log_File: www-relay-bin.000018
Relay_Log_Pos: 16492854
Relay_Master_Log_File: mysql-bin.000019
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: example
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 16492717
Relay_Log_Space: 16492854
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
1 row in set (0.00 sec)
是否要从 MasterServer 复制 Binary Log 数据,必须为 Yes。
是否要执行从 MasterServer 复制过来的 BinaryLog 数据,必须为 Yes。
Seconds_Behind_Master:
Slave 的数据落后了 Master 多少秒,执行一段时间后应该会是零。
一些有用的总结:
1,测试结果:拔掉网线,在两个mysql上做insert,update,然后插上网线,会自动同步,并且对于update有冲突的情况(update同一条记录),会根据时间点来选择。
2,一台 MasterServer 可以拥有很多台 SlaveServer;但一台Slave Server 只可对应到一台 Master Server。
3,Slave Replication Error,
There is another mechanism that you can use, which is the skip counter.You can skip the current transaction that the replication thread is hung on andcontinue. You can issue the following command in mysql to skip a transaction:
mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;
mysql> START SLAVE;
4, If you use autoincremeneting id's and you're using Master Master with both masters active youmight want to consider setting the following options on Master 1 and Master 2so that the auto increment values don't end up clashing in the case where aninsert happens to occur at exactly the same time on both servers:
Make Master 1 only auto-increment odd numbers byadding this to my.cnf under [mysqld]:
auto_increment_increment= 2
auto_increment_offset = 1
Make Master 2 only auto-increment even numbers byadding this to my.cnf under [mysqld]:
auto_increment_increment= 2
auto_increment_offset = 2
This is particularly important if you're usingauto increment id columns as primary keys
- MySQL master-master replication 深入了解
- MySQL master-master replication 深入了解 (转)
- MySQL Master-Master Replication Manager
- Mysql Master-Master Replication Manager
- MySQL master-master replication configuration
- MySQL Master-Slave Replication
- MySQL Master-Slave Replication
- mysql 5.0 Replication master-master 实操
- 搭建Master-Master Mysql Replication 集群
- Windows下搭建MySql Master-Master Replication
- 搭建mysql master-slave replication
- MySQL Master Slave Replication配置
- master to master replication
- Mysql Master-Master Replication MYSQL数据库主主同步设置
- MySQL Master-Master Replication Manager(1)-(2)-(3)
- MySQL双主(master-master)+半同步(Semisync Replication)
- mysql的master和slave同步(replication)
- mysql-proxy实现replication(master&slave)
- 基于UDP协议的Java聊天小程序!
- 如果Linux提示"list too long"用我这个办法
- 递归-背包问题
- 开通博客的第一篇
- HDU 1299(数论,求n的素因子个数)
- MySQL master-master replication 深入了解
- Windows CE嵌入式导航系统研究(硬件设计2)
- Android 开发环境的搭建
- autorun.inf完全操作手册
- 数据分页
- 一周总结
- AjaxAnywhere的用法
- 概念篇-SOA,ESB, JBI, SCA区别
- (转)C/C++中的日期和时间 time_t与struct tm转换