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> 

總結

本文爲了簡單說明主從的複製功能主要的使用方式,設定內容能夠滿足主寫從讀,在實際使用中需要進一步擴展和設定以滿足相應的需求。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章