MySQL主从复制(5)——主主复制架构的搭建

MySQL主从复制(5)——主主复制架构的搭建

一、系统环境

MySQL版本:MySQL5.7

Linux版本:CentOS7.0

主数据库A地址:192.168.1.11

主数据库B地址:192.168.1.12

二、配置192.168.1.11为主,192.168.1.12为从

(一)配置192.168.1.11

1、修改配置文件

找到主数据库的配置文件my.cnf,在[mysqld]部分添加如下内容:

[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
........

配置完成后重启mysql服务,查看binlog日志文件信息:

[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、创建复制用户

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、导出数据库数据,并复制到从库

[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、查看二进制日志信息

mysql> show master logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |      1461 |
+------------------+-----------+
1 row in set (0.01 sec)

(二)配置192.168.1.12

1、修改配置文件

找到从数据库的配置文件my.cnf,在[mysqld]部分添加如下内容:

[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
.......

配置完成后重启mysql服务,查看relay log文件信息:

[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、导入主服务器上的数据

[root@localhost ~]# mysql -uroot -p123456 < /tmp/all_bak.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.

3、配置主从同步参数

在从服务器上执行如下命令:

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、开启从库的服务(启动IO和SQL线程)

mysql> start slave;
Query OK, 0 rows affected (0.02 sec)

5、查看主从复制的状态

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

三、配置192.168.1.12为主,192.168.1.11为从

(一)配置192.168.1.12

1、修改配置文件

找到主数据库的配置文件my.cnf,在[mysqld]部分添加如下内容:

# 开启二进制日志
 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

重启mysql服务,查看日志文件信息:

扫描二维码关注公众号,回复: 11781886 查看本文章
[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、创建复制用户

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、查看二进制日志信息

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)

(二)配置192.168.1.11

1、修改配置文件

找到从数据库的配置文件my.cnf,在[mysqld]部分添加如下内容:

[root@Mysql11 ~]# vim /etc/my.cnf

[mysqld]
.........
relay-log=mysql-relay-log  ### 开启relay log
........

2、配置主从同步参数

在从服务器上执行如下命令:

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、开启从库的服务(启动IO和SQL线程)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

4、查看主从复制的状态

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

四、利用数据操作验证主从复制

1、在192.168.1.11上创建数据库 mydata

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、在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、在192.168.1.12上选择 mydata 数据库,创建一张表 t222

mysql> use mydata
Database changed
mysql> create table t222(id int);
Query OK, 0 rows affected (0.04 sec)

4、在192.168.1.11上查看

mysql> use mydata;
Database changed
mysql> show tables;
+------------------+
| Tables_in_mydata |
+------------------+
| t222             |
+------------------+
1 row in set (0.00 sec)

主主复制配置成功!

猜你喜欢

转载自blog.csdn.net/weixin_44377973/article/details/107147719