Docker 中配置 MySQL 主从同步(1)

因为一些工作上的业务需求,需要在 Docker 环境下配置 MySQL 主从服务器,相关的技术博客很少,踩过一些坑后决定把配置过程记录下来

主要步骤

1. 安装 Docker

这里不再详细说明 Docker 的安装过程,如有需要,可以参考这篇文章

我使用的开发环境是 macOS High Sierra

2. 用 Dockerfile 构建 MySQL 镜像

因为主从服务器的 mysql.cnf 配置不同,所以需要构建两个不同的镜像

3. 使用 docker build 和 docker run 命令构建运行两个容器

4. 分别修改主从服务器的配置,测试

下面是详细步骤


1. 下载 Docker 中的 MySQL 镜像

Docker 提供了 docker search 命令来搜索 Docker Hub 上的镜像,运行

1
docker search mysql

可以看到列出了很多 mysql 的镜像,这里我选择 stars 最多的,也就是官方提供的镜像,运行

1
docker pull mysql

等待下载完成后可以运行

1
docker images

查看下载好的镜像,今后下载的镜像都可以这样进行查看,如果想要删除某个镜像,可以运行

1
docker rmi [id]

删除镜像需要保证没有使用该镜像运行的容器,可以通过运行

1
docker ps -a

查看所有运行中和未运行的容器,可以使用

1
docker rm [cid]

删除容器,这里要和删除镜像的命令有所区分,删除容器前需要停止容器的运行,可以使用

1
docker stop [cid]

以上是关于 docker 常用命令的说明,如果需要获取更多帮助可以运行

1
docker

查看所有命令,运行

1
docker [cmd] --help

查看命令帮助

2. 使用 Dockerfile 构建镜像并创建容器

这里我选择1主2从的架构,方便后面部署读写分离时进行两从库的读取时负载均衡

创建三个文件夹,masterslave1slave2,对应一个主库和两个从库,分别在三个文件夹下新建 Dockerfile 文件,注意,这里不需要后缀

在每个 Dockerfile 文件中写入以下内容:

1
2
3
4
5
6
7
FROM mysql #从mysql镜像构建
COPY mysqld.cnf /etc/mysql/mysql.conf.d #把当前目录下的 mysqld.cnf 文件拷贝到 /etc/mysql/mysql.conf.d 目录下,替换已有文件
EXPOSE 3306 #暴露 3306 端口,这里和 mysql 默认端口保持一致
CMD ["mysqld"] #启动 mysql

上述内容中的 mysqld.cnf/etc/mysql/mysql.conf.d 以最新 mysql 版本 5.7 为准,不同版本下可能不同,有的版本是 my.cnf/etc/mysql 目录

mysqld.cnf 文件可以先在本机上装一个 mysql 的对应版本,然后在 /etc/mysql/mysql.conf.d 目录下找到 mysqld.cnf 文件,复制到 master 和另外两个文件夹下,这样就获得了基础配置文件

或者将下面内容复制到 mysqld.cnf 文件中,但要保证 mysql 版本大于 5.7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
# Here is entries for some specific programs
# The following values assume you have at least 32M ram
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
#
# * Basic Settings
#
user = mysql
character-set-server=utf8
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
skip-external-locking
#
# 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
#
# * Fine Tuning
#
key_buffer_size = 16M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options = BACKUP
#max_connections = 100
#table_cache = 64
#thread_concurrency = 10
#
# * Query Cache Configuration
#
query_cache_limit = 1M
query_cache_size = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file = /var/log/mysql/mysql.log
#general_log = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#log_slow_queries = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
# other settings you may need to change.
#server-id = 1
#log_bin = /var/log/mysql/mysql-bin.log
expire_logs_days = 10
max_binlog_size = 100M
#binlog_do_db = include_database_name
#binlog_ignore_db = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem

接下来,分别修改master、slave1 和 slave2 下的 mysqld.cnf 文件

  • master:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [mysqld]
    # [必须]启用二进制日志
    log-bin = mysql-bin
    # [必须]服务器唯一ID,默认是1,一般取IP最后一段,这里看情况分配
    server-id = 1
    #忽略写入binlog的库
    binlog-ignore-db = mysql
  • slave1:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [mysqld]
    # [必须]启用二进制日志
    log-bin = mysql-bin
    # [必须]服务器唯一ID,默认是1,一般取IP最后一段,这里看情况分配
    server-id = 2
    #忽略写入binlog的库
    binlog-ignore-db = mysql
  • slave2:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [mysqld]
    # [必须]启用二进制日志
    log-bin = mysql-bin
    # [必须]服务器唯一ID,默认是1,一般取IP最后一段,这里看情况分配
    server-id = 3
    #忽略写入binlog的库
    binlog-ignore-db = mysql

以上,完成了构建镜像的准备工作,接下来开始正式构建镜像