实验环境

Master:192.168.200.112

Slave:192.168.200.111

两台机器全部安装的MySQL5.7

 

Master配置

修改主配置文件

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

[mysqld]

datadir=/usr/local/mysql/data

socket=/tmp/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks

 

log-bin=mysql-bin       //开启二进制日志

enforce-gtid-consistency=true          //强制gtid的一致性

gtid_mode=ON                   //打开gtid功能

server-id=1

 

symbolic-links=0

# Settings user and group are ignored when systemd is used.

# If you need to run mysqld under a different user or group,

# customize your systemd unit file for mariadb according to the

# instructions in http://fedoraproject.org/wiki/Systemd

 

[mysqld_safe]

log-error=/usr/local/mysql/data/mysql.log

pid-file=/usr/local/mysql/data/mysql.pid

 

#

# include all files from the config directory

#

!includedir /etc/my.cnf.d

[root@localhost ~]# /etc/init.d/mysql restart

进入数据库设置授权

[root@localhost ~]# mysql -uroot -p123456

mysql> grant replication slave on *.* to 'myslave'@'192.168.200.%' identified by '123456';

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show master status;

+------------------+----------+--------------+------------------+------------------------------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |

+------------------+----------+--------------+------------------+------------------------------------------+

| mysql-bin.000002 |      848 |              |                  | 0bb6bae0-f02d-11e9-91d1-000c29d76afe:1-3 |

+------------------+----------+--------------+------------------+------------------------------------------+

1 row in set (0.00 sec)

12345678910

Slave配置

配置文件修改

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

[mysqld]

datadir=/usr/local/mysql/data

socket=/tmp/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks

 

log-bin=mysql-bin

enforce-gtid-consistency=true

gtid_mode=ON

server-id=2           //不要与主库相同

 

symbolic-links=0

# Settings user and group are ignored when systemd is used.

# If you need to run mysqld under a different user or group,

# customize your systemd unit file for mariadb according to the

# instructions in http://fedoraproject.org/wiki/Systemd

 

[mysqld_safe]

log-error=/usr/local/mysql/data/error.log

pid-file=/usr/local/mysql/data/mysql.pid

 

#

# include all files from the config directory

#

!includedir /etc/my.cnf.d

[root@localhost ~]# /etc/init.d/mysql restart

连接主库

[root@localhost ~]# mysql -uroot -p123456

mysql> stop slave;

Query OK, 0 rows affected, 1 warning (0.00 sec)

 

mysql> change master to master_host='192.168.200.112',master_user='myslave',master_password='123456',master_log_file='mysql-bin.000002',MASTER_AUTO_POSITION=0;

Query OK, 0 rows affected, 3 warnings (0.41 sec)

 

mysql> start slave;

Query OK, 0 rows affected (0.13 sec)

 

mysql> show slave status\G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.200.112

                  Master_User: myslave

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000002

          Read_Master_Log_Pos: 848

               Relay_Log_File: node1-relay-bin.000002

                Relay_Log_Pos: 1061

        Relay_Master_Log_File: mysql-bin.000002

             Slave_IO_Running: Yes       //已开启

            Slave_SQL_Running: Yes              //已开启

              Replicate_Do_DB:

          Replicate_Ignore_DB:

           Replicate_Do_Table:

       Replicate_Ignore_Table:

      Replicate_Wild_Do_Table:

  Replicate_Wild_Ignore_Table:

                   Last_Errno: 0

                   Last_Error:

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 848

              Relay_Log_Space: 1268

              Until_Condition: None

               Until_Log_File:

                Until_Log_Pos: 0

           Master_SSL_Allowed: No

           Master_SSL_CA_File:

           Master_SSL_CA_Path:

              Master_SSL_Cert:

            Master_SSL_Cipher:

               Master_SSL_Key:

        Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 0

                Last_IO_Error:

               Last_SQL_Errno: 0

               Last_SQL_Error:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 1

                  Master_UUID: 0bb6bae0-f02d-11e9-91d1-000c29d76afe

             Master_Info_File: /usr/local/mysql/data/master.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

           Master_Retry_Count: 86400

                  Master_Bind:

      Last_IO_Error_Timestamp:

     Last_SQL_Error_Timestamp:

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set: 0bb6bae0-f02d-11e9-91d1-000c29d76afe:1-3

            Executed_Gtid_Set: 0bb6bae0-f02d-11e9-91d1-000c29d76afe:1-3,

678e29a7-f02e-11e9-98af-000c2928f574:1

                Auto_Position: 0

         Replicate_Rewrite_DB:

                 Channel_Name:

           Master_TLS_Version:

1 row in set (0.00 sec)

测试

在主库中创建一个库

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

4 rows in set (0.00 sec)

 

mysql> create database aaaaa;

Query OK, 1 row affected (0.00 sec)

 

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| aaaaa              |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

5 rows in set (0.00 sec)

从库查看

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

4 rows in set (0.00 sec)

 

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| aaaaa              |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

5 rows in set (0.00 sec)