189 8069 5689

mysql-5.6.26主主复制

环境如下:
CentOS6.5_64

创新互联专注于网站建设|成都网站维护公司|优化|托管以及网络推广,积累了大量的网站设计与制作经验,为许多企业提供了网站定制设计服务,案例作品覆盖食品包装袋等行业。能根据企业所处的行业与销售的产品,结合品牌形象的塑造,量身策划品质网站。

MySQL5.6.26
master1:192.168.1.112
master2:192.168.1.114

mysql 安装

这里就忽略了,主要就是一个细心。有时候一个不小心,就会发现安装失败。

配置master1
1.修改my.cnf文件,增加如下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
[mysql

server-id =1 #数据库ID
log-bin=myslq-bin #启用二进制日志
binlog-do-db=tudou1 #需要同步的数据库,这里同步tudou1和tudou2两个数据库
binlog-do-db=tudou2
binlog-ignore-db=mysql #忽略同步的数据库
log-bin=/var/log/mysqlbin/bin_log #设置生成的log文件名,
如果没有var/log/mysqlbin这个目录,则需要创建,并且执行chown -R mysql.mysql /var/log/mysqlbin
log-slave-updates #把从库的写操作记录到binlog中
expire_logs_days=365 #日志文件过期天数,默认是 0,表示不过期
auto-increment-increment=2 #设定为主服务器的数量,防止 auto_increment 字段重复
auto-increment-offset=1 #自增长字段的初始值,在多台 master 环境下,不会出现自增
长 ID 重复

2.添加一个复制的帐号backup
1
2
3
[root@localhost ~]# mysql -uroot -p123456
mysql>grant replication slave on . to backup@'%' identified by '123456';
mysql>flush privileges;
3.在防火墙里面添加mysql端口3306,并重启防火墙生效
1
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
4.在master2上测试用backup用户是否能连接到master1上的数据库
1
[root@master ~]# mysql -ubackup -h 192.168.1.112 -p123456
配置master2

1.修改my.cnf文件,增加如下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
[mysqld]

server-id =2 #数据库ID
log-bin=myslq-bin #启用二进制日志
binlog-do-db=tudou1 #需要同步的数据库,这里同步tudou1和tudou2两个数据库
binlog-do-db=tudou2
binlog-ignore-db=mysql #忽略同步的数据库
log-bin=/var/log/mysqlbin/bin_log #设置生成的log文件名,如果没有var/log/mysqlbin这个目录,则需要创建,并且执行chown -R mysql.mysql /var/log/mysqlbin
log-slave-updates #把从库的写操作记录到binlog中
expire_logs_days=365 #日志文件过期天数,默认是 0,表示不过期
auto-increment-increment=2 #设定为主服务器的数量,防止 auto_increment 字段重复
auto-increment-offset=2 #自增长字段的初始值,在多台 master 环境下,不会出现自增
长 ID 重复

2.添加一个复制的帐号backup
1
2
3
[root@localhost ~]# mysql -uroot -p123456
mysql>grant replication slave on . to backup@'%' identified by '123456';
mysql>flush privileges;
3.在防火墙里面添加mysql端口3306,并重启防火墙生效
1
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
4.在master1上测试用backup用户是否能连接到master2上的数据库
1
[root@master ~]# mysql -ubackup -h 192.168.1.114 -p123456
配置master1-master2同步

重启master1和master2的mysql服务
1
[root@localhost ~]# service mysql restart
查看master状态
master1
1
2
3
4
5
6
7
mysql> show master status;
+----------------+----------+----------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+----------------+------------------+-------------------+
| bin_log.000001 | 120 | tudou1,tudou2 | mysql | |
+----------------+----------+----------------+------------------+-------------------+
1 row in set (0.00 sec)
master2
1
2
3
4
5
6
7
mysql> show master status;
+----------------+----------+----------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+----------------+------------------+-------------------+
| bin_log.000001 | 120 | tudou1,tudou2 | mysql | |
+----------------+----------+----------------+------------------+-------------------+
1 row in set (0.00 sec)
设置master1从master2同步
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
mysql>CHANGE MASTER TO MASTER_HOST='192.168.1.114',MASTER_PORT=3306,MASTER_USER='backup',MASTER_PASSWORD='123456',MASTER_LOG_FILE='bin_log.00
0001',MASTER_LOG_POS=120;
mysql>start slave;
mysql>show slave status\G;
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.114
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: bin_log.000001
Read_Master_Log_Pos: 120
Relay_Log_File: master-relay-bin.000002
Relay_Log_Pos: 281
Relay_Master_Log_File: bin_log.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如出现以下两项,则说明配置成功!
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

设置master2从master1同步
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
mysql>CHANGE MASTER TO MASTER_HOST='192.168.1.112',MASTER_PORT=3306,MASTER_USER='backup',MASTER_PASSWORD='123456',MASTER_LOG_FILE='bin_log.00
0001',MASTER_LOG_POS=120;
mysql>start slave;
mysql> show slave status\G;
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.112
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: bin_log.000001
Read_Master_Log_Pos: 120
Relay_Log_File: master2-relay-bin.000002
Relay_Log_Pos: 281
Relay_Master_Log_File: bin_log.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如出现以下两项,则说明配置成功!
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

测试主主同步:
进入master1 mysql数据库
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
mysql> create database tudou1;
Query OK, 1 row affected (0.02 sec)

mysql> use tudou1;
Database changed
mysql> create table test(id int auto_increment,name varchar(10),primary key(id));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test(name) values('a');
Query OK, 1 row affected (0.01 sec)

mysql> insert into test(name) values('b');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test(name) values('c');
Query OK, 1 row affected (0.01 sec)

mysql> select * from test;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 3 | b |
| 5 | c |
+----+------+
3 rows in set (0.00 sec)
进master2,查看是否有tudou1这个数据库和test表。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
mysql> use tudou1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+------------------+
| Tables_in_tudou1 |
+------------------+
| test |
+------------------+
1 row in set (0.00 sec)

mysql> select from test;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 3 | b |
| 5 | c |
+----+------+
3 rows in set (0.00 sec)
mysql>insert into test(name) values('d');
在master1的数据库中就会发现刚在master2插入的数据也插入到数据库了。
1
2
3
4
5
6
7
8
9
mysql> select
from test;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 3 | b |
| 5 | c |
| 6 | d |
+----+------+


文章题目:mysql-5.6.26主主复制
文章起源:http://cdxtjz.cn/article/jochjo.html

其他资讯