欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL双Master配置的方法详解

程序员文章站 2024-02-21 21:55:28
刚刚抽空做了一下mysql 的主主同步。把步骤写下来,至于会出现的什么问题,以后随时更新。这里我同步的数据库是test1、环境描述。主机:192.168.0.231(a)主...
刚刚抽空做了一下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
skip-name-resolve
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
skip-name-resolve
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。