2.docker配置mysql主从实现读写分离

2.1 获取mysql镜像

  • 获取镜像
$ sudo docker pull mysql
  • 查看镜像
$ sudo docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              7d83a47ab2d2        4 weeks ago         408MB

2.2 安装mysql-client,用于访问mysql容器数据库

$ sudo apt-get install mysql-client-core-5.7

2.3创建工作目录,保存mysql数据

$ cd ~
$ mkdir docker-mysql-data
$ cd docker-mysql-data
$ mkdir mysql-master
$ mkdir mysql-slave1

2.4创建主从mysql配置文件

  • 创建主mysql配置文件
$ cd ~/docker-mysql-data
$ vim my-master.cnf

复制如下配置:

# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#
# The MySQL Community Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[client]
port        = 3306
socket      = /var/run/mysqld/mysqld.sock

[mysqld_safe]
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamp

log-bin = mysql-bin 
server-id = 1 

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address   = 127.0.0.1

#log-error  = /var/log/mysql/error.log

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

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

# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
  • 创建从mysql配置文件
cd ~/docker-mysql-data
$ vim my-slave1.cnf

复制主mysql配置文件,注意:
将server-id = 1 修改为server-id = 2

2.5运行主myqsl容器

$ sudo docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name mysql-master  -v /home/yangqian/docker-mysql-data/mysql-master:/var/lib/mysql -v /home/yangqian/docker-mysql-data/my-master.cnf:/etc/mysql/my.cnf -p 3306:3306 mysql

参数详解:

run     运行一个容器
--name      后面是这个镜像的名称
-p 3306:3306        表示在这个容器中使用3306端口(第二个)映射到本机的端口号也为3306(第一个)
-d      表示使用守护进程运行,即服务挂在后台
  • 查看主mysql容器状态:
$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
e37e29f090b7        mysql               "docker-entrypoint.s…"   27 minutes ago      Up 27 minutes       0.0.0.0:3306->3306/tcp   mysql-master
  • 连接数据库查看
$ mysql -uroot -p123456 -h127.0.0.1

2.6运行从mysql容器

$ sudo docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name mysql-slave1 -v /home/yangqian/docker-mysql-data/mysql-slave1:/var/lib/mysql -v /home/yangqian/docker-mysql-data/my-slave1.cnf:/etc/mysql/my.cnf -p 3307:3306 mysql
  • 查看mysql容器状态:
$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
3dc37943fb7a        mysql               "docker-entrypoint.s…"   12 seconds ago      Up 11 seconds       0.0.0.0:3307->3306/tcp   mysql-slave1
e37e29f090b7        mysql               "docker-entrypoint.s…"   31 minutes ago      Up 31 minutes       0.0.0.0:3306->3306/tcp   mysql-master

  • 连接数据库查看
$ mysql -uroot -p123456 -h127.0.0.1 --port 3307

2.7 主从服务配置

  • 查看容器ip,后面主从服务配置需要用到两个容器的ip
# 查看mysql容器id
$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
3dc37943fb7a        mysql               "docker-entrypoint.s…"   8 minutes ago       Up 8 minutes        0.0.0.0:3307->3306/tcp   mysql-slave1
e37e29f090b7        mysql               "docker-entrypoint.s…"   39 minutes ago      Up 39 minutes       0.0.0.0:3306->3306/tcp   mysql-master

方法1:

$ sudo docker inspect 3d |grep 'IPAddress'

            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",

$ sudo docker inspect e3 |grep 'IPAddress'

            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",

方法2:

# 进入主mysql容器终端
$ sudo docker exec -it e3 /bin/bash
root@e37e29f090b7:/# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
27: eth0@if28: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
root@e37e29f090b7:/# exit
exit
# 进入从mysql容器终端
$ sudo docker exec -it 3d /bin/bash
root@3dc37943fb7a:/# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
29: eth0@if30: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
root@3dc37943fb7a:/# exit
exit
  • 开启两个终端分别用来连接主从mysql容器
# 终端1:主mysql容器
$ mysql -uroot -p123456 -h127.0.0.1 --port 3306
# 终端2:从mysql容器
$ mysql -uroot -p123456 -h127.0.0.1 --port 3307
  • 主mysql容器配置
mysql> grant replication slave on *.* to 'slave'@'%' identified by 'slave';

mysql> flush privileges;

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |      1160 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
  • 从mysql容器配置
mysql> change master to master_host='172.17.0.2',master_user='slave',master_password='slave',
master_log_file='mysql-bin.000004',master_log_pos=1160,master_port=3306;
mysql> start slave;
mysql> show slave status \G

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.17.0.2
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 1160
               Relay_Log_File: 3dc37943fb7a-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000004
             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: 1160
              Relay_Log_Space: 534
              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: 39f6cd9a-f870-11e7-b14e-0242ac110002
             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)
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐