本文说明,刚上来使用MySQL高版本的,结果MySQL容器正常运行后,外部客户端连接不上,最后降低版本就OK
拉取MySQL数据库镜像
# docker pull daocloud.io/library/mysql:5.7.22
[root@localhost ~]# docker pull daocloud.io/library/mysql:5.7.22Trying to pull repository daocloud.io/library/mysql ...5.7.22: Pulling from daocloud.io/library/mysql
be8881be8156: Pull complete
c3995dabd1d7: Pull complete
9931fdda3586: Pull complete
bb1b6b6eff6a: Pull complete
a65f125fa718: Pull complete
2d9f8dd09be2: Pull complete
37b912cb2afe: Pull complete
79592d21cb7f: Pull complete
00bfe968d82d: Pull complete
79cf546d4770: Pull complete
2b3c2e6bacee: Pull complete
Digest: sha256:e744510d4d03fddd1162651312afd1e591cf33b051c6f29ed64b9a3e64b97aa7
Status: Downloaded newer imagefor daocloud.io/library/mysql:5.7.22
查看拉取的镜像文件
# docker images
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
daocloud.io/library/mysql 5.7.22 6bb891430fb6 9 months ago 372 MB
镜像重命名
# docker tag daocloud.io/library/mysql:5.7.22 mysql:5.7.22
删除原有镜像名
# docker rmi daocloud.io/library/mysql:5.7.22
[root@localhost ~]# docker rmi daocloud.io/library/mysql:5.7.22Untagged: daocloud.io/library/mysql:5.7.22Untagged: daocloud.io/library/mysql@sha256:e744510d4d03fddd1162651312afd1e591cf33b051c6f29ed64b9a3e64b97aa7
查看确认
# docker images
[root@localhost ~]# docker images
mysql5.7.22 6bb891430fb6 9 months ago 372 MB
创建MySQL容器,并后台启动
# docker run --name djlmysql -e MYSQL_ROOT_PASSWORD=123456 -p 3307:3306 -d mysql:5.7.22
[root@localhost ~]# docker run --name djlmysql -e MYSQL_ROOT_PASSWORD=123456 -p 3307:3306 -d mysql:5.7.22fa0c44b306c1b5038d39cbe5add1dc05a9f034d21dc1e62bb52487bc8b7ff04f
[root@localhost~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fa0c44b306c1 mysql:5.7.22 "docker-entrypoint..." 22 seconds ago Up 21 seconds 0.0.0.0:3307->3306/tcp djlmysql
-e环境变量说明:
MYSQL_ROOT_PASSWORD
本变量必填,它指定了 MySQL root 的用户的密码
MYSQL_DATABASE
本变量可选,通过该变量当 MySQL 启动时会创建一个由你指定的数据库。如果你另外又提供了一对用户名和密码(见下方),那么他将会被授予本数据库的所有权限。
MYSQL_USER, MYSQL_PASSWORD
这两个变量可选,同时使用的话会创建一个新用户并设置相应的密码,该用户会被授予由 MYSQL_DATABASE 变量指定的数据库的所有权限(见上方)。只有当同时提供了这两个变量时该用户才会被创建。
特别注意没有必要使用这个机制来创建 root 用户,root 用户的密码会被设置为 MYSQL_ROOT_PASSWORD 变量的值。
MYSQL_ALLOW_EMPTY_PASSWORD
本变量可选,当其被设置为 yes 时将会允许当前容器中的 root 用户能够使用空密码。注意:绝对不建议将该变量设置为 yes,除非你知道自己在做什么。如果这么做的话你的 MySQL 服务将会失去保护,所有人都可以以超级用户的身份访问该 MySQL 服务。
MySQL配置文件的挂载目录
当 MySQL 服务启动时会以 /etc/mysql/my.cnf 为配置文件,本文件会导入 /etc/mysql/conf.d 目录中所有以 .cnf 为后缀的文件。这些文件会拓展或覆盖 /etc/mysql/my.cnf 文件中的配置。因此你可以创建你自己需要的配置文件并挂载至 MySQL 容器中的 /etc/mysql/conf.d 目录。
# cat my.cnf
root@70d513b30042:/etc/mysql# ls -l
total8drwxr-xr-x 2 root root 62 Jul 27 2018conf.d
lrwxrwxrwx1 root root 24 Jul 27 2018 my.cnf -> /etc/alternatives/my.cnf-rw-r--r-- 1 root root 839 Jul 9 2016my.cnf.fallback-rw-r--r-- 1 root root 796 Mar 4 2018mysql.cnf
drwxr-xr-x 2 root root 24 Jul 27 2018mysql.conf.d
root@70d513b30042:/etc/mysql# catmy.cnf
# Copyright (c)2016, Oracle and/or its affiliates. All rights reserved.
#
# This program isfree 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; version2of the License.
#
# This program is distributedinthe 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 Licensefor moredetails.
#
# You should have received a copy of the GNU General Public License
# along with this program;if not, writeto the Free Software
# Foundation, Inc.,51 Franklin St, Fifth Floor, Boston, MA 02110-1301USA!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
拷贝源生MySQL配置文件到宿主机
# docker cp djlmysql:/etc/mysql/mysql.conf.d /home/mysqldata/
# docker cp djlmysql:/etc/mysql/conf.d /home/mysqldata/
[root@docker-servers ~]# docker cp djlmysql:/etc/mysql/mysql.conf.d /home/mysqldata/[root@docker-servers ~]#
[root@docker-servers ~]# ls /home/mysqldata/mysql.conf.d
[root@docker-servers ~]#
[root@docker-servers ~]#
[root@docker-servers ~]# docker cp djlmysql:/etc/mysql/conf.d /home/mysqldata/[root@docker-servers ~]#
[root@docker-servers ~]#
[root@docker-servers ~]# ls /home/mysqldata/conf.d mysql.conf.d
MySQL储存数据的位置(可用于挂载到本地):/var/lib/mysql
# docker cp djlmysql:/var/lib/mysql /home/mysqldata/
[root@docker-servers ~]# docker cp djlmysql:/var/lib/mysql /home/mysqldata/[root@docker-servers ~]#
[root@docker-servers ~]# ls /home/mysqldata/conf.d mysql mysql.conf.d
[root@docker-servers ~]#
[root@docker-servers ~]# cd /home/mysqldata/mysql
[root@docker-servers mysql]#
[root@docker-servers mysql]# lsauto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys
ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem
创建MySQL容器,挂载数据和配置文件到本地
-e TZ="Asia/Shanghai"
# docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -v /home/mysqldata/mysql.conf.d:/etc/mysql/mysql.conf.d -v /home/mysqldata/conf.d:/etc/mysql/conf.d -v /home/mysqldata/mysql:/var/lib/mysql -d mysql:5.7.22
[root@docker-servers ~]# docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -v /home/mysqldata/mysql.conf.d:/etc/mysql/mysql.conf.d -v /home/mysqldata/conf.d:/etc/mysql/conf.d -v /home/mysqldata/mysql:/var/lib/mysql -d mysql:5.7.229996e8b903389408070e2083abdd4811c195c5652df376ca91e91d0e5e727d87
客户端外部连接
参考博客
https://blog.csdn.net/woniu211111/article/details/80968154
https://www.cnblogs.com/pwc1996/p/5425234.html
https://hub.daocloud.io/repos/fa51c1d6-9dc2-49d9-91ac-4bbfc24a1bda