三台mysql5.7服务器互作主从配置案例

一、架构

三台mysql5.7服务器互作主从配置案例_第1张图片

三台msyql服务器221,222,223,每台服务器开两个实例,3306作为主库,3307作为另外一台服务器的从库

二、每台服务器安装双实例

参照:https://www.cnblogs.com/sky-cheng/p/10919447.html

进行双实例安装

三、每台服务器的3306实例创建一个复制账号

在172.28.5.221上

[root@push-5-221 ~]# mysql -uroot -p -S /var/lib/mysql/3306/mysql.sock 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> grant replication slave,replication client on *.* to 'repl'@'%' identified by 'XXXXXXX';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 

在172.28.5.222上

[root@push-5-222 ~]# mysql -uroot -p -S /var/lib/mysql/3306/mysql.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant replication slave,replication client on *.* to 'repl'@'%' identified by 'XXXXXXXX';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

在172.28.5.223上

[root@push-5-223 ~]# mysql -uroot -p -S /var/lib/mysql/3306/mysql.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant replication slave,replication client on *.* to 'repl'@'%' identified by 'xxxxxxxx';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

 

三、主从配置

1、首先设置好每个3306mysql实例的server_id参数为本机IP地址最后一位,3307实例server_id参数为本机IP地址最后一位再加端口号

在172.28.5.221服务器的3306配置文件中打开日志文件设置

[root@push-5-221 ~]# vim /etc/mysql/my-3306.cnf 

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
# innodb_buffer_pool_size = 128M
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
user=mysql
port=3306
datadir=/home/mysql-5.7.26/data/3306
socket=/var/lib/mysql/3306/mysql.sock
server_id=2213306
log-bin=master-221
binlog_format=row
#skip-grant-tables
symbolic-links=0
pid-file=/var/run/mysqld/3306/mysqld.pid
log-error=/home/mysql-5.7.26/log/3306/mysqld.log

[mysqld_safe]
log-error=/home/mysql-5.7.26/log/3306/mysqld.log
server_id=221 设置server_id
log-bin=master-221 设置log_bin日志文件名
binlog_format=row 指定日志格式为row
2、重启3306实例,使配置生效
[root@push-5-221 ~]# mysqladmin -uroot -p -S /var/lib/mysql/3306/mysql.sock shutdown
[root@push-5-221 ~]# mysqld_safe --defaults-file=/etc/mysql/my-3306.cnf &
[1] 3459
[root@push-5-221 ~]# 2019-05-27T02:55:18.758733Z mysqld_safe Logging to '/home/mysql-5.7.26/log/3306/mysqld.log'.
2019-05-27T02:55:18.813602Z mysqld_safe Starting mysqld daemon with databases from /home/mysql-5.7.26/data/3306

3、客户端连接

^C
[root@push-5-221 ~]# mysql -uroot -p -S /var/lib/mysql/3306/mysql.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

4、显示server_id参数

mysql> show variables like '%server_id%';
+----------------+---------+
| Variable_name  | Value   |
+----------------+---------+
| server_id      | 2213306 |
| server_id_bits | 32      |
+----------------+---------+
2 rows in set (0.00 sec)

 5、显示主库状态

 

mysql> show master status\G;
*************************** 1. row ***************************
             File: master-221.000002
         Position: 154
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

ERROR: 
No query specified

 

此时,需要记住上面的参数:  File: master-221.000001   Position: 154 ,日志文件名和偏移量,需要在从库上做主从设置时要用到这两个参数

6、从库设置

在172.28.5.222服务器上的3307配置文件

[mysqld]
# innodb_buffer_pool_size = 128M
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
user=mysql
port=3307
datadir=/home/mysql-5.7.26/data/3307
socket=/var/lib/mysql/3307/mysql.sock
server_id=2223307
symbolic-links=0
pid-file=/var/run/mysqld/3307/mysqld.pid
log-error=/home/mysql-5.7.26/log/3307/mysqld.log

[mysqld_safe]
log-error=/home/mysql-5.7.26/log/3307/mysqld.log

 

 7、重启3307实例,使配置生效

[root@push-5-222 ~]# mysqladmin -uroot -p -S /var/lib/mysql/3307/mysql.sock shutdown
[root@push-5-222 ~]# mysqld_safe --defaults-file=/etc/mysql/my-3307.cnf &
[1] 23445
[root@push-5-222 ~]# 2019-05-27T03:11:33.550199Z mysqld_safe Logging to '/home/mysql-5.7.26/log/3307/mysqld.log'.
2019-05-27T03:11:33.610157Z mysqld_safe Starting mysqld daemon with databases from /home/mysql-5.7.26/data/3307

8、查看相应参数

[root@push-5-222 ~]# mysql -uroot -p -S /var/lib/mysql/3307/mysql.sock
mysql> show variables like '%server_id%';
+----------------+---------+
| Variable_name  | Value   |
+----------------+---------+
| server_id      | 2223307 |
| server_id_bits | 32      |
+----------------+---------+
2 rows in set (0.00 sec)

9、执行从库命令

mysql> change master to
    -> master_host='172.28.5.221',
    -> master_port=3306,
    -> master_user='repl',
    -> master_password='xxxxxxxx',
    -> master_log_file='master-221.000002',
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.31 sec)

10、启动从库

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

11、显示从库状态

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

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.28.5.221
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-221.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: push-5-222-relay-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-221.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: 154
              Relay_Log_Space: 533
              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: 2213306
                  Master_UUID: 3e61e5be-7dff-11e9-8945-6c2b5992e632
             Master_Info_File: /home/mysql-5.7.26/data/3307/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
同步线程都已经启动成功、 
Exec_Master_Log_Pos: 154  同步主库日志文件偏移量跟主库的日志文件偏移量相同,说明已经完全同步

12、测试同步
在172.28.5.221上创建test库和test表,并插入一条记录

mysql> create database test;
Query OK, 1 row affected (0.12 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
mysql> use test;
Database changed
mysql> create table test( uid int, name varchar(20));
Query OK, 0 rows affected (0.43 sec)
mysql> insert into test(uid,name)values(1,'aaaa');
Query OK, 1 row affected (0.14 sec)
mysql> select * from test;
+------+------+
| uid  | name |
+------+------+
|    1 | aaaa |
+------+------+
1 row in set (0.00 sec)

此时,在172.28.5.222上连接从库3307

[root@push-5-222 ~]# mysql -uroot -p -S /var/lib/mysql/3307/mysql.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)
mysql> use test;
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_test |
+----------------+
| test           |
+----------------+
1 row in set (0.00 sec)

mysql> select * from test;
+------+------+
| uid  | name |
+------+------+
|    1 | aaaa |
+------+------+
1 row in set (0.00 sec)

已经跟主库数据同步成功了。

同样在172.28.5.222上将3306配置文件打开

[mysqld]
#innodb_buffer_pool_size = 128M
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
user=mysql
port=3306
datadir=/home/mysql-5.7.26/data/3306
socket=/var/lib/mysql/3306/mysql.sock
server_id=2223306
log-bin=master-222
binlog_format=row
#skip-grant-tables
symbolic-links=0
pid-file=/var/run/mysqld/3306/mysqld.pid
log-error=/home/mysql-5.7.26/log/3306/mysqld.log

[mysqld_safe]
log-error=/home/mysql-5.7.26/log/3306/mysqld.log

重启3306实例

[root@push-5-222 ~]# mysqladmin -uroot -p -S /var/lib/mysql/3306/mysql.sock shutdown
Enter password: 
[root@push-5-222 ~]# mysqld_safe --defaults-file=/etc/mysql/my-3306.cnf &
[2] 24587
[root@push-5-222 ~]# 2019-05-27T03:29:21.804192Z mysqld_safe Logging to '/home/mysql-5.7.26/log/3306/mysqld.log'.
2019-05-27T03:29:21.864191Z mysqld_safe Starting mysqld daemon with databases from /home/mysql-5.7.26/data/3306
[root@push-5-222 ~]# mysql -uroot -p -S /var/lib/mysql/3306/mysql.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like '%server_id%';
+----------------+---------+
| Variable_name  | Value   |
+----------------+---------+
| server_id      | 2223306 |
| server_id_bits | 32      |
+----------------+---------+
2 rows in set (0.01 sec)

mysql> show msater status\G;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'msater status' at line 1
ERROR: 
No query specified

mysql> show master status\G;
*************************** 1. row ***************************
             File: master-222.000001
         Position: 154
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

ERROR: 
No query specified

显示主库状态

在172.28.5.223服务器上打开3307配置文件,将其设置为172.28.5.222的3306的从库

[root@push-5-223 ~]# vim /etc/mysql/my-3307.cnf 

[mysqld]
# innodb_buffer_pool_size = 128M
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
user=mysql
port=3307
datadir=/home/mysql-5.7.26/data/3307
socket=/var/lib/mysql/3307/mysql.sock
server_id=2233307
#log-bin=master-223
#binlog_format=row
#skip-grant-tables
symbolic-links=0
pid-file=/var/run/mysqld/3307/mysqld.pid
log-error=/home/mysql-5.7.26/log/3307/mysqld.log

[mysqld_safe]
log-error=/home/mysql-5.7.26/log/3307/mysqld.log

客户端连接,设置从库命令

[root@push-5-223 ~]# mysql -uroot -p -S /var/lib/mysql/3307/mysql.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like '%server_id%';
+----------------+---------+
| Variable_name  | Value   |
+----------------+---------+
| server_id      | 2233307 |
| server_id_bits | 32      |
+----------------+---------+
2 rows in set (0.00 sec)
mysql> change master to
    -> master_host='172.28.5.222',
    -> master_port=3306,
    -> master_user='repl',
    -> master_password='xxxxxx',
    -> master_log_file='master-222.000001',
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.36 sec)

启动从库

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

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.28.5.222
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-222.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: push-5-223-relay-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-222.000001
             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: 154
              Relay_Log_Space: 533
              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: 2223306
                  Master_UUID: de99b6b7-8018-11e9-9a45-6c2b5992e6d2
             Master_Info_File: /home/mysql-5.7.26/data/3307/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: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 

此时,从库已经启动,并且同步线程启动成功,同步完毕

测试数据

在172.28.5.222的3306上创建test库个test表,并插入一条数据

mysql> create database test;
Query OK, 1 row affected (0.11 sec)

mysql> use test;
Database changed
mysql> create table test (uid int,name varchar(20));
Query OK, 0 rows affected (0.26 sec)
mysql> insert into test values(1,'bbbb');
Query OK, 1 row affected (0.15 sec)
mysql> select * from test;
+------+------+
| uid  | name |
+------+------+
|    1 | bbbb |
+------+------+
1 row in set (0.00 sec)

此时,在172.28.5.223上连接3307从库

[root@push-5-223 ~]# mysql -uroot -p -S /var/lib/mysql/3307/mysql.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use test;
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> select * from test;
+------+------+
| uid  | name |
+------+------+
|    1 | bbbb |
+------+------+
1 row in set (0.00 sec)

数据同步成功。

同样在172.28.5.223的3306和172.28.5.221的3307做主从配置

编辑172.28.5.223的3306配置文件

[root@push-5-223 ~]# vim /etc/mysql/my-3306.cnf 

[mysqld]
# innodb_buffer_pool_size = 128M
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
user=mysql
port=3306
datadir=/home/mysql-5.7.26/data/3306
socket=/var/lib/mysql/3306/mysql.sock
server_id=2233306
log-bin=master-223
binlog_format=row
#skip-grant-tables
symbolic-links=0
pid-file=/var/run/mysqld/3306/mysqld.pid
log-error=/home/mysql-5.7.26/log/3306/mysqld.log

[mysqld_safe]
log-error=/home/mysql-5.7.26/log/3306/mysqld.log

重启3306实例

[root@push-5-223 ~]# mysqladmin -uroot -p -S /var/lib/mysql/3306/mysql.sock shutdown
Enter password: 
[root@push-5-223 ~]# mysqld_safe --defaults-file=/etc/mysql/my-3306.cnf &
[2] 10352
[root@push-5-223 ~]# 2019-05-27T03:46:18.899652Z mysqld_safe Logging to '/home/mysql-5.7.26/log/3306/mysqld.log'.
2019-05-27T03:46:18.965604Z mysqld_safe Starting mysqld daemon with databases from /home/mysql-5.7.26/data/3306
^C
[root@push-5-223 ~]# mysql -uroot -p -S /var/lib/mysql/3306/mysql.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like'%server_id%';
+----------------+---------+
| Variable_name  | Value   |
+----------------+---------+
| server_id      | 2233306 |
| server_id_bits | 32      |
+----------------+---------+
2 rows in set (0.01 sec)

mysql> show master status \G
*************************** 1. row ***************************
             File: master-223.000001
         Position: 154
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

主库启动成功

在172.28.5.221的3307配置文件

[root@push-5-221 ~]# vim /etc/mysql/my-3307.cnf 

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
# innodb_buffer_pool_size = 128M
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
user=mysql
port=3307
datadir=/home/mysql-5.7.26/data/3307
socket=/var/lib/mysql/3307/mysql.sock
server_id=2213307

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

log-error=/home/mysql-5.7.26/log/3307/mysqld.log
pid-file=/var/run/mysqld/3307/mysqld.pid

[myqld_safe]
log-error=/home/mysql-5.7.26/log/3307/mysqld.log

重启3307实例

[root@push-5-221 ~]# mysqladmin -uroot -p -S /var/lib/mysql/3307/mysql.sock shutdown
[root@push-5-221 ~]# mysqld_safe --defaults-file=/etc/mysql/my-3307.cnf &
[2] 6725
[root@push-5-221 ~]# 2019-05-27T03:49:31.427996Z mysqld_safe Logging to '/home/mysql-5.7.26/log/3307/mysqld.log'.
2019-05-27T03:49:31.482868Z mysqld_safe Starting mysqld daemon with databases from /home/mysql-5.7.26/data/3307
^C
[root@push-5-221 ~]# mysql -uroot -p -S /var/lib/mysql/3307/mysql.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like '%server_id%';
+----------------+---------+
| Variable_name  | Value   |
+----------------+---------+
| server_id      | 2213307 |
| server_id_bits | 32      |
+----------------+---------+
2 rows in set (0.01 sec)

执行从库命令

mysql> change master to
    -> master_host='172.28.5.223',
    -> master_port=3306,
    -> master_user='repl',
    -> master_password='Zaq1xsw@',
    -> master_log_file='master-223.000001',
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.42 sec)

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

mysql> 

测试数据

在172.28.5.223的3306创建test库和test表,并插入一条记录

mysql> create database test;
Query OK, 1 row affected (0.06 sec)

mysql> use test;
Database changed
mysql> create table test(id int,name varchar(10));
Query OK, 0 rows affected (0.34 sec)

mysql> insert into test values(1,'ccc');
Query OK, 1 row affected (0.14 sec)

mysql> select * from test;
+------+------+
| id   | name |
+------+------+
|    1 | ccc  |
+------+------+
1 row in set (0.00 sec)

在172.28.5.221的3307上查看

sion for the right syntax to use near 'database' at line 1
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> use test;
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> select * from test;
+------+------+
| id   | name |
+------+------+
|    1 | ccc  |
+------+------+
1 row in set (0.00 sec)

数据同步成功

至此3台MYSQL服务器互为主从设置完毕。

四、多实例的启动和停止

停止3306实例:  mysqladmin -uroot -p -S /var/lib/mysql/3307/mysql.sock shutdown

启动3306实例: mysqld_safe --defaults-file=/etc/mysql/my-3306.cnf & 

连接3306实例:mysql -uroot -p -S /var/lib/mysql/3306/mysql.sock

 
 

你可能感兴趣的:(三台mysql5.7服务器互作主从配置案例)