下载最新版mysql
[root@iZwz9bpj7oo7xbzsrq86kmZ app]# docker pull mysql:latest
latest: Pulling from library/mysql
b380bbd43752: Pull complete
f23cbf2ecc5d: Pull complete
30cfc6c29c0a: Pull complete
b38609286cbe: Pull complete
8211d9e66cd6: Pull complete
2313f9eeca4a: Pull complete
7eb487d00da0: Pull complete
4d7421c8152e: Pull complete
77f3d8811a28: Pull complete
cce755338cba: Pull complete
69b753046b9f: Pull complete
b2e64b0ab53c: Pull complete
Digest: sha256:6d7d4524463fe6e2b893ffc2b89543c81dec7ef82fb2020a1b27606666464d87
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
[root@iZwz9bpj7oo7xbzsrq86kmZ app]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest ecac195d15af 3 weeks ago 516MB
创建挂载目录
[root@iZwz9bpj7oo7xbzsrq86kmZ mysql]# mkdir -p /app/mysql/conf
[root@iZwz9bpj7oo7xbzsrq86kmZ mysql]# mkdir -p /app/mysql/data
[root@iZwz9bpj7oo7xbzsrq86kmZ mysql]# mkdir -p /app/mysql/logs
创建my.cnf
# Copyright (c) 2017, 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 Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Custom config should go here
!includedir /etc/mysql/conf.d/
启动容器
docker run --restart=always -d -v /app/mysql/conf/my.cnf:/etc/mysql/my.cnf -v /app/mysql/logs:/logs -v /app/mysql/data/mysql:/var/lib/mysql -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:latest
查看当前容器的运行状态:
[root@iZwz9bpj7oo7xbzsrq86kmZ conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fcbf1b231e5f mysql:latest "docker-entrypoint.s…" 48 seconds ago Up 47 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
参数解释
--restart=always -> 开机启动容器,容器异常自动重启
-d -> 以守护进程的方式启动容器
-v /app/mysql/conf/my.cnf:/etc/mysql/my.cnf -> 映射配置文件
-v /app/mysql/logs:/logs -> 映射日志
-v /app/mysql/data/mysql:/var/lib/mysql -> 映射数据
-p 3306:3306 -> 绑定宿主机端口
--name mysql -> 指定容器名称
-e MYSQL_ROOT_PASSWORD=123456 -> 写入配置root密码
版权声明:本文为a112626290原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。