楠槡

Docker Compose Mysql

2018-05-31

因开发需要,安装 Mysql 环境并使用 Docker Compose 容器化

Just lookup in github - raphaelli/Dockerfiles/mysql/

Mysql 的 Docker Compose 安装

拉取镜像:

1
2
3
docker pull mysql:5.7
docker images | grep mysql # 查看镜像是否已经安装了

配置docker-compose 文件

创建需要的目录:

1
2
mkdir -p /docker/mysql/data #数据存储目录
mkdir -p /docker/mysql/conf #配置目录

新建目录后,将你需要的mysql配置文件 mymysqld.cnf 放到conf目录里:

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
#
# 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
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 = 0.0.0.0
#
# * 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

上面的配置是默认的配置, 只修改了 bind-address = 0.0.0.0, 用于远程链接。

编写docker-compose 文件docker-compose.yml:

1
2
3
4
5
6
7
8
9
10
11
12
version: '3'
services:
mysql_compose:
image: mysql:5.7
container_name: mysql_compose
ports:
- 6606:3306
volumes:
- /docker/mysql/data:/var/lib/mysql
- /docker/mysql/conf/mymysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
environment:
MYSQL_ROOT_PASSWORD: "123456"

注意: 端口(ports) 和 卷(volumes) 的配置。以及 root 密码!

Docker使用-p选项允许容器上的端口映射到主机上的端口。如果你如上所述启动容器(6606:3306),你可以通过将客户机连接到主机上的端口(6606)来连接到数据库。

运行mysql image

进入到上面编写的docker-compose.yml文件的目录,运行:

1
docker-compose up -d

开机自动运行

找到/etc/rc.local文件(exit 0)之前添加,添加以下脚本

1
/usr/local/bin/docker-compose -f /docker/mysql/docker-compose.yml up -d

其中-f参数是指定docker-compose.yml文件的参数
设置完以后,重启操作系统,耐心等待一会,就可以看到对应的docker容器都启动起来了

Tags: Docker
使用支付宝打赏
使用微信打赏

若你觉得我的文章对您有帮助,点击上方按钮请我喝杯咖啡☕

若文章中存在问题,或您有任何意见和疑问,均可与我联系

扫描二维码,分享此文章 📱