MySQL-master1:192.168.0.128
MySQL-master2:192.168.0.129
OS版本:CentOS 5.4
MySQL版本:5.5.10
一、MySQL master-master配置
1、修改MySQL配置文件
两台MySQL均如要开启binlog日志功能,开启方法:在MySQL-master1 配置文件/etc/my.cnf的[MySQLd]段中加上
log-bin=mysql -bin
server-id = 1
#binlog-do-db=radius
binlog-ignore-db=mysql ,test
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
在MySQL-master2 配置文件/etc/my.cnf的[MySQLd]段中加上
log-bin=mysql -bin
server-id = 2
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2
将两个配置文件保存,分别重启mysql 服务器
binlog-do-db=database 是要记录日志的数据库;
binlog-ignore-db 是不要记录日志的数据库名,多个数据库中间用逗号(,)隔开;
2、将192.168.0.128设为192.168.0.129的主服务器
在192.168.0.128上新建授权用户
MySQL> grant replication slave on *.* to ‘replication’@'%’ identified by ‘replication’;
Query OK, 0 rows affected (0.00 sec)
MySQL>flush privileges;
Query OK, 0 rows affected (0.00 sec)
MySQL> show master status;
+——————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| MySQL-bin.000003 | 374 | | |
+——————+———-+————–+——————+
1 row in set (0.00 sec)
在192.168.0.129将192.168.0.128为自己的主服务器
MySQL> change master to master_host=’192.168.0.128′,master_user=’replication’,master_password=’replication’,master_log_file=’MySQL-bin.000003′,master_log_pos=374;
Query OK, 0 rows affected (0.05 sec)
MySQL> start slave;
Query OK, 0 rows affected (0.00 sec)
MySQL> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.128
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: MySQL-bin.000003
Read_Master_Log_Pos: 374
Relay_Log_File: MySQL-master2-relay-bin.000002
Relay_Log_Pos: 235
Relay_Master_Log_File: MySQL-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
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: 374
Relay_Log_Space: 235
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)
红色标注部分为192.168.0.128主机中show master status命令结果中的file postion两个值
3、将192.168.0.129设为192.168.0.128的主服务器
在192.168.0.129上新建授权用户
MySQL> grant replication slave on *.* to ‘replication’@'%’ identified by ‘replication’;
Query OK, 0 rows affected (0.00 sec)
MySQL>flush privileges;
Query OK, 0 rows affected (0.00 sec)
MySQL> show master status;
+——————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| MySQL-bin.000003 | 374 | | |
+——————+———-+————–+——————+
1 row in set (0.00 sec)
在192.168.0.128将192.168.0.129为自己的主服务器
MySQL> change master to master_host=’192.168.0.129′,master_user=’replication’,master_password=’replication’,master_log_file=’MySQL-bin.000003′,master_log_pos=374;
Query OK, 0 rows affected (0.05 sec)
MySQL> start slave;
Query OK, 0 rows affected (0.00 sec)
MySQL> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.129
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: MySQL-bin.000003
Read_Master_Log_Pos: 374
Relay_Log_File: MySQL-master2-relay-bin.000002
Relay_Log_Pos: 235
Relay_Master_Log_File: MySQL-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
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: 374
Relay_Log_Space: 235
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)
mysql 配置文件my.cnf中
log-slave-updates 表示如果一个MASTER挂掉的话,另外一个马上接管。
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1 指的是服务器频繁的刷新日志。这个保证了在其中一台挂掉的话,日志刷新到另外一台。从而保证了数据的同步 。
如果主数据库有数据的话,数据库锁表操作,不让数据再进行写入动作。
mysql > FLUSH TABLES WITH READ LOCK ;
看主数据库的状态
mysql > show master status;
把主服务器数据文件复制到从服务器,最好先用tar处理一下
取消主数据库锁定
mysql >UNLOCK TABLES;