MySQL master-slave replication (5)-the construction of master-master replication architecture
1. System environment
MySQL version: MySQL5.7
Linux version: CentOS7.0
Main database A address: 192.168.1.11
Main database B address: 192.168.1.12
2. Configure 192.168.1.11 as the master and 192.168.1.12 as the slave
(1) Arrangement 192.168.1.11
1. Modify the configuration file
Find the configuration file my.cnf of the main database and add the following content in the [mysqld] section:
[root@Mysql11 ~]# vim /etc/my.cnf
[mysqld]
.........
server-id=1 ### 服务器ID
log-bin=mysql-bin ### 开启binlog
binlog_format=mixed ### binlog模式为mixed
binlog_row_image=MINIMAL
binlog-rows-query-log_events=1
### 设置同步时需要忽略哪些数据库,一般同步不要同步mysql库,用户名和密码信息在mysql数据库中
binlog-ignore-db=mysql
binlog-ignore-db=sys
relay-log=mysql-relay-log ### 开启relay log
........
After the configuration is complete, restart the mysql service and check the binlog log file information:
[root@Mysql11 ~]# ll /var/lib/mysql/mysql-bin*
-rw-r-----. 1 mysql mysql 1461 7月 5 21:58 /var/lib/mysql/mysql-bin.000001
-rw-r-----. 1 mysql mysql 19 7月 5 21:57 /var/lib/mysql/mysql-bin.index
2. Create a copy user
mysql> grant replication slave on *.* to 'repl'@'192.168.1.%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> select user,host from mysql.user;
+---------------+-------------+
| user | host |
+---------------+-------------+
| wgx | % |
| repl | 192.168.1.% |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-------------+
5 rows in set (0.03 sec)
3. Export database data and copy to the slave database
[root@Mysql11 ~]# mysqldump -uroot -p123456 --all-databases --master-data=2 --events > /tmp/all_bak.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@Mysql11 ~]# ls /tmp/
all_bak.sql
[root@Mysql11 ~]# scp /tmp/all_bak.sql 192.168.1.12:/tmp/
root@192.168.1.12's password:
all_bak.sql 100% 835KB 835.5KB/s 00:00
[root@Mysql11 ~]#
4. View binary log information
mysql> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 1461 |
+------------------+-----------+
1 row in set (0.01 sec)
(2) Arrangement 192.168.1.12
1. Modify the configuration file
Find the configuration file my.cnf of the slave database and add the following content in the [mysqld] section:
[root@Mysql11 ~]# vim /etc/my.cnf
[mysqld]
.........
server-id=2 ### 服务器ID
log-bin=mysql-bin ### 开启binlog
binlog_format=mixed ### binlog模式为mixed
binlog_row_image=MINIMAL
binlog-rows-query-log_events=1
### 设置同步时需要忽略哪些数据库,一般同步不要同步mysql库,用户名和密码信息在mysql数据库中
binlog-ignore-db=mysql
binlog-ignore-db=sys
relay-log=mysql-relay-log ### 开启relay log
.......
After the configuration is complete, restart the mysql service and check the relay log file information:
[root@localhost ~]# ll /var/lib/mysql/mysql-bin*
-rw-r-----. 1 mysql mysql 154 7月 5 22:09 /var/lib/mysql/mysql-bin.000001
-rw-r-----. 1 mysql mysql 19 7月 5 22:09 /var/lib/mysql/mysql-bin.index
2. Import the data on the main server
[root@localhost ~]# mysql -uroot -p123456 < /tmp/all_bak.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
3. Configure master-slave synchronization parameters
Execute the following commands on the slave server:
change master to
master_host='192.168.1.11',
master_port=3306,
master_user='repl',
master_password='123456',
master_log_file='mysql-bin.000001',
master_log_pos=1461;
4. Turn on the service from the library (start IO and SQL threads)
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
5. View the status of master-slave replication
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-log.000003
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
3. Configure 192.168.1.12 as the master and 192.168.1.11 as the slave
(1) Arrangement 192.168.1.12
1. Modify the configuration file
Find the configuration file my.cnf of the main database and add the following content in the [mysqld] section:
# 开启二进制日志
log-bin=mysql-bin
# 二进制模式为混合模式
binlog-format=mixed
# row模式时,只记录更新的字段
binlog_row_image=MINIMAL
# 即使在row模式下,也保存SQL语句
binlog-rows-query-log_events=1
binlog-ignore-db=mysql
binlog-ignore-db=sys
Restart the mysql service and view the log file information:
[root@localhost mysql]# ll /var/lib/mysql/mysql-bin*
-rw-r-----. 1 mysql mysql 154 7月 5 23:34 /var/lib/mysql/mysql-bin.000001
-rw-r-----. 1 mysql mysql 19 7月 5 23:34 /var/lib/mysql/mysql-bin.index
2. Create a copy user
mysql> set global validate_password_policy=low;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=4;
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to 'repl1'@'192.168.1.%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select host,user from mysql.user;
+-------------+---------------+
| host | user |
+-------------+---------------+
| % | wgx |
| 192.168.1.% | repl1 |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-------------+---------------+
5 rows in set (0.00 sec)
3. View binary log information
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | mysql,sys | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
(2) Arrangement 192.168.1.11
1. Modify the configuration file
Find the configuration file my.cnf of the slave database and add the following content in the [mysqld] section:
[root@Mysql11 ~]# vim /etc/my.cnf
[mysqld]
.........
relay-log=mysql-relay-log ### 开启relay log
........
2. Configure master-slave synchronization parameters
Execute the following commands on the slave server:
change master to
master_host='192.168.1.12',
master_port=3306,
master_user='repl1',
master_password='123456',
master_log_file='mysql-bin.000001',
master_log_pos=154;
3. Open the service from the library (start IO and SQL threads)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
4. View the status of master-slave replication
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.12
Master_User: repl1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 315
Relay_Log_File: mysql-relay-log.000002
Relay_Log_Pos: 481
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Fourth, use data operations to verify master-slave replication
1. Create a database mydata on 192.168.1.11
mysql> create database mydata;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aaa |
| hist |
| mydata |
| mysql |
| performance_schema |
| sys |
| wanggx |
+--------------------+
8 rows in set (0.01 sec)
2. View on 192.168.1.12
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aaa |
| hist |
| mydata |
| mysql |
| performance_schema |
| sys |
| wanggx |
+--------------------+
8 rows in set (0.00 sec)
3. Select the mydata database on 192.168.1.12 and create a table t222
mysql> use mydata
Database changed
mysql> create table t222(id int);
Query OK, 0 rows affected (0.04 sec)
4. View on 192.168.1.11
mysql> use mydata;
Database changed
mysql> show tables;
+------------------+
| Tables_in_mydata |
+------------------+
| t222 |
+------------------+
1 row in set (0.00 sec)