mysql-5.6.26 主主複製

環境以下:
CentOS6.5_64mysql

MySQL5.6.26
master1:192.168.1.112
master2:192.168.1.114sql

mysql 安裝數據庫

這裏就忽略了,主要就是一個細心。有時候一個不當心,就會發現安裝失敗。服務器

配置master1
1.修改my.cnf文件,增長以下內容:
1
2
3
4
5
6
7
8
9
10
11
12
13
[mysqltcp

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 重複ide

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]ui

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 重複this

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同步rest

重啓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插入的數據也插入到數據庫了。123456789mysql> select from test;+----+------+| id | name |+----+------+| 1 | a || 3 | b || 5 | c || 6 | d | +----+------+

相關文章
相關標籤/搜索