教程目的: Linux 下 Mysql主、从双向同步

使用工具:Putty、SecureCRT等远程工具

适用系统:Linux 系列系统

操作过程:

1.两台安装好数据库服务器主机,互相可以访问(由于MySQL不同版本之间的(二进制日志)binlog格式可能会不一样,因此最好的搭配组合是Master的MySQL版本和Slave的版本相同,或者Master的版本肯定不能高于Slave版本。)

例如:

Master: DaoBiDaoA (10.135.28.112)

Slave: DaoBiDaoB (10.132.21.19)

2.设置Master服务器

在Mysql配置文件my.cnf (一般是存放在 /etc/my.cnf ) 中,找到 server-id 编辑

1
2
3
server-id        = 1
binlog-do-db     = daobidao
binlog-ignore-db = mysql

其中:server-id 是设置数据库服务编号,binlog-do-db 是设置需要记录二进制日志的数据库(如果是多个数据库需要记录二进制日志,就在添加一条此信息),binlog-ignore-db 是设置不需要记录二进制日志的数据库。

注意:需要开启log-bin二进制日志文件

1
log-bin=mysql-bin

启动Mysql服务,添加同步数据库服务用户信息

1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> CREATE USER 'daobidao'@'10.132.21.19' IDENTIFIED BY 'daobidao';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL ON daobidao.* TO 'daobidao'@'10.132.21.19';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES ;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    53
Current database: *** NONE ***
Query OK, 0 rows affected (0.00 sec)

其中:命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password';  是创建用户;例如命令:CREATE USER 'daobidao'@'10.132.21.19' IDENTIFIED BY 'daobidao';  创建用户daobidao,并且指定允许登录ip地址为10.132.21.19(此ip地址为Slave的ip地址),密码为daobidao。GRANT ALL ON daobidao.* TO 'daobidao'@'10.132.21.19'; 此命令是授权daobidao用户对数据库daobidao有所有权限。

3.配置Slave服务器my.cnf

1
2
3
4
5
6
7
8
9
10
server-id        = 2
binlog-do-db     = daobidao
binlog-ignore-db = mysql
master-host      = 10.135.28.112
master-user      = daobidao
master-password  = daobidao
master-port      = 3306
replicate-ignore-db = mysql  
replicate-do-db = daobidao

其中:master-host = 10.135.28.112 是设置Master的服务器地址,master-user = daobidao 同步到用户名,master-password = daobidao 同步用户的密码,master-port = 3306 Master的端口,replicate-ignore-db = mysql 指定不进行同步的数据库,replicate-do-db = daobidao 指定进行同步的数据库,如需要同步多个数据库,就写多条此信息。

4.查看Master的数据库信息

mysql> show master status;

1
2
3
4
5
6
7
8
9
10
No connection. Trying to reconnect...
Connection id:    1
Current database: *** NONE ***
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000007 |     6520 | daobidao     | mysql            |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

其中:需要看到是File和Position的记录信息。

5.Slave设置同步(此步操作完成,就实现了Slave同步Master数据库)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
mysql> slave stop;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to master_host='10.135.28.112',  master_user='daobidao',
master_password='daobidao', master_log_file='mysql-bin.000007', master_log_pos=6520;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    43
Current database: *** NONE ***
Query OK, 0 rows affected (0.14 sec)
mysql> slave start;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting to reconnect after a failed master event read
Master_Host: 10.135.28.112
Master_User: daobidao
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 6520
Relay_Log_File: AY130424102832Z-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: No
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: 6520
Relay_Log_Space: 106
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)

一般主要看到:Slave_IO_Running: Yes  Slave_SQL_Running: Yes  如果都是yes,那代表已经在同步。


继续阅读原文:http://iblog.daobidao.com/linux-mysql-master-and-slave-sync-master-two-way-synchronization-from.DaoBiDao