MySQL基础:容器化的主从模式

这篇文章介绍一下使用容器化方式构成一主两从的MySQL数据库。

docker-compose.yml

liumiaocn:mysql liumiao$ cat docker-compose.yml 
version: '2'
services:
  master:
    image: liumiaocn/mysql:5.7.16
    environment:
      - MYSQL_ROOT_PASSWORD=liumiaocn
    restart: always
    ports:
      - "3306:3306"
  
  slave1:
    image: liumiaocn/mysql:5.7.16
    environment:
      - MYSQL_ROOT_PASSWORD=liumiaocn
    restart: always
    ports:
      - "3307:3306"

  slave2:
    image: liumiaocn/mysql:5.7.16
    environment:
      - MYSQL_ROOT_PASSWORD=liumiaocn
    restart: always
    ports:
      - "3308:3306"
liumiaocn:mysql liumiao$ 

步骤1: 启动服务

liumiaocn:mysql liumiao$ docker-compose up -d
Creating network "mysql_default" with the default driver
Creating mysql_slave2_1 ... done
Creating mysql_master_1 ... done
Creating mysql_slave1_1 ... done
liumiaocn:mysql liumiao$ docker-compose ps
     Name                  Command             State           Ports         
-----------------------------------------------------------------------------
mysql_master_1   docker-entrypoint.sh mysqld   Up      0.0.0.0:3306->3306/tcp
mysql_slave1_1   docker-entrypoint.sh mysqld   Up      0.0.0.0:3307->3306/tcp
mysql_slave2_1   docker-entrypoint.sh mysqld   Up      0.0.0.0:3308->3306/tcp
liumiaocn:mysql liumiao$ 

步骤2: 设定Master节点

设定serverid和log-bin

  • 修改对象文件:/etc/mysql/my.cnf
  • 添加如下内容:
    [mysqld]
    log-bin=mysql-bin
    server-id=1

执行日志如下:

liumiaocn:mysql liumiao$ docker exec -it mysql_master_1 sh
# cd /etc/mysql
# ls
conf.d	my.cnf	my.cnf.fallback  mysql.cnf  mysql.conf.d
# cp -p my.cnf my.cnf.org
# cat <<EOF >>my.cnf
> [mysqld]
log-bin=mysql-bin
server-id=1> > 
> EOF
# diff my.cnf my.cnf.org
18,20d17
< [mysqld]
< log-bin=mysql-bin
< server-id=1
# 

设定复制用户和权限

liumiaocn:mysql liumiao$ mysql -uroot -pliumiaocn -h127.0.0.1 -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> create user 'replicationuser'@'%' identified by 'replicationuser';
Query OK, 0 rows affected (0.01 sec)

mysql> grant replication slave on *.* to 'replicationuser'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
Empty set (0.01 sec)

mysql> 

步骤3: 设定Slave节点

设定Slave1的serverid

liumiaocn:mysql liumiao$ docker exec -it mysql_slave1_1 sh
# cd /etc/mysql
# ls 
conf.d	my.cnf	my.cnf.fallback  mysql.cnf  mysql.conf.d
# cp my.cnf my.cnf.org
# cat <<EOF >>my.cnf
> [mysqld]
server-id=2> 
> EOF
# diff my.cnf my.cnf.org
18,19d17
< [mysqld]
< server-id=2
# 

设定Slave2的serverid

liumiaocn:mysql liumiao$ docker exec -it mysql_slave2_1 sh
# cd /etc/mysql
# ls
conf.d	my.cnf	my.cnf.fallback  mysql.cnf  mysql.conf.d
# cp my.cnf my.cnf.org
# cat <<EOF >>my.cnf
> [mysqld]
server-id=3> 
> EOF
# diff my.cnf my.cnf.org
18,19d17
< [mysqld]
< server-id=3
# 

步骤4: 重启Master节点与Slave节点

  • 重启Master节点与Slave节点
liumiaocn:mysql liumiao$ docker-compose restart
Restarting mysql_slave1_1 ... done
Restarting mysql_master_1 ... done
Restarting mysql_slave2_1 ... done
liumiaocn:mysql liumiao$ docker-compose ps
     Name                  Command             State           Ports         
-----------------------------------------------------------------------------
mysql_master_1   docker-entrypoint.sh mysqld   Up      0.0.0.0:3306->3306/tcp
mysql_slave1_1   docker-entrypoint.sh mysqld   Up      0.0.0.0:3307->3306/tcp
mysql_slave2_1   docker-entrypoint.sh mysqld   Up      0.0.0.0:3308->3306/tcp
liumiaocn:mysql liumiao$ docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                    NAMES
38568ff39efd        liumiaocn/mysql:5.7.16   "docker-entrypoint.s…"   18 minutes ago      Up 6 seconds        0.0.0.0:3307->3306/tcp   mysql_slave1_1
1fd3808076b2        liumiaocn/mysql:5.7.16   "docker-entrypoint.s…"   18 minutes ago      Up 6 seconds        0.0.0.0:3306->3306/tcp   mysql_master_1
1f6ae8f3384c        liumiaocn/mysql:5.7.16   "docker-entrypoint.s…"   18 minutes ago      Up 6 seconds        0.0.0.0:3308->3306/tcp   mysql_slave2_1
liumiaocn:mysql liumiao$ 
  • 确认状态
liumiaocn:mysql liumiao$ mysql -uroot -pliumiaocn -h127.0.0.1 -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.16-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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 master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> 

步骤5: 设定并启动slave

设定Slave1

liumiaocn:mysql liumiao$ mysql -uroot -pliumiaocn -h127.0.0.1 -P3307
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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 slave status;
Empty set (0.00 sec)

mysql> 
mysql> change master to master_host='master',master_port=3306, master_user='replicationuser',master_password='replicationuser',master_log_file='mysql-bin.000001',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: master
                  Master_User: replicationuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: 38568ff39efd-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 154
              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: NULL
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: 0
                  Master_UUID: 
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           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: 
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 

注意此时Slave_IO_Running和Slave_SQL_Running均为No

  • 启动slave1
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> 
  • 确认状态
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: master
                  Master_User: replicationuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: 38568ff39efd-relay-bin.000003
                Relay_Log_Pos: 367
        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: 154
              Relay_Log_Space: 747
              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: 457caec2-a898-11ea-8e55-0242c0a83003
             Master_Info_File: /var/lib/mysql/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: 
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 

设定Slave2

Slave2与Slave1同样的操作完整做一遍,唯一区别在于连接时指定的端口为3308,执行日志如下所示。

liumiaocn:mysql liumiao$ mysql -uroot -pliumiaocn -h127.0.0.1 -P3308
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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 slave status;
Empty set (0.00 sec)

mysql> change master to master_host='master',master_port=3306, master_user='replicationuser',master_password='replicationuser',master_log_file='mysql-bin.000001',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.03 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: master
                  Master_User: replicationuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: 1f6ae8f3384c-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 154
              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: NULL
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: 0
                  Master_UUID: 
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           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: 
1 row in set (0.00 sec)

ERROR: 
No query specified

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

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: master
                  Master_User: replicationuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: 1f6ae8f3384c-relay-bin.000003
                Relay_Log_Pos: 367
        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: 154
              Relay_Log_Space: 747
              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: 457caec2-a898-11ea-8e55-0242c0a83003
             Master_Info_File: /var/lib/mysql/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: 
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 

结果确认

主从结构最重要的在于主写从读的复制功能,在Master库中创建一个数据库,在数据库中建表,在表中插入数据库,然后在从库中确认数据库、表和数据的内容,从而验证上述一主两从的MySQL是否能够进行最基本的动作。

Master操作

  • 建库
liumiaocn:mysql liumiao$ mysql -uroot -pliumiaocn -h127.0.0.1 -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.16-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database testdb;
Query OK, 1 row affected (0.00 sec)

mysql> 
  • 建表
mysql> use testdb;
Database changed
mysql> create table person (id int not null, name varchar(20));
Query OK, 0 rows affected (0.02 sec)

mysql> desc person;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql>
  • 插入数据
mysql> insert into person values(1001,'liumiao');
Query OK, 1 row affected (0.01 sec)

mysql> select * from person;
+------+---------+
| id   | name    |
+------+---------+
| 1001 | liumiao |
+------+---------+
1 row in set (0.00 sec)

mysql> 

Slave1节点确认

liumiaocn:mysql liumiao$ mysql -uroot -pliumiaocn -h127.0.0.1 -P3307
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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                |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)

mysql> use testdb
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_testdb |
+------------------+
| person           |
+------------------+
1 row in set (0.00 sec)

mysql> desc person;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> select * from person;
+------+---------+
| id   | name    |
+------+---------+
| 1001 | liumiao |
+------+---------+
1 row in set (0.00 sec)

mysql> 

Slave 2节点确认

liumiaocn:mysql liumiao$ mysql -uroot -pliumiaocn -h127.0.0.1 -P3308
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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                |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)

mysql> use testdb
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_testdb |
+------------------+
| person           |
+------------------+
1 row in set (0.00 sec)

mysql> desc person
    -> ;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> select * from person;
+------+---------+
| id   | name    |
+------+---------+
| 1001 | liumiao |
+------+---------+
1 row in set (0.00 sec)

mysql> 

总结

本文为了简单说明主从的复制功能主要的使用方式,设定内容能够满足主写从读,在实际使用中需要进一步扩展和设定以满足相应的需求。

猜你喜欢

转载自blog.csdn.net/liumiaocn/article/details/106603243