参考资料
- DockerHub MySQL:https://hub.docker.com/_/mysql/
Docker搭建MySQL服务
操作步骤
- 拉取MySQL镜像
- 创建并启动MySQL容器
- 连接测试MySQL
准备工作
$ docker-machine ip
192.168.99.100
$ docker-machine ls
$ docker-machine ssh default
docker@default:~$ docker
查找镜像
在DockerHub中查找MySQL镜像
docker@default:~$ docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 8058 [OK]
mariadb MariaDB is a community-developed fork of MyS… 2725 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 603 [OK]
zabbix/zabbix-server-mysql Zabbix Server with MySQL database support 185 [OK]
hypriot/rpi-mysql RPi-compatible Docker Image with Mysql 113
zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server wi… 96 [OK]
centurylink/mysql Image containing mysql. Optimized to be link… 60 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 51
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK]
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 44
tutum/mysql Base docker image to run a MySQL database se… 31
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 26 [OK]
bitnami/mysql Bitnami MySQL Docker Image 26 [OK]
zabbix/zabbix-web-apache-mysql Zabbix frontend based on Apache web-server w… 26 [OK]
zabbix/zabbix-proxy-mysql Zabbix proxy with MySQL database support 20 [OK]
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 20
centos/mysql-56-centos7 MySQL 5.6 SQL database server 13
circleci/mysql MySQL is a widely used, open-source relation… 12
mysql/mysql-router MySQL Router provides transparent routing be… 11
dsteinkopf/backup-all-mysql backup all DBs in a mysql server 6 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
jelastic/mysql An image of the MySQL database server mainta… 1
cloudposse/mysql Improved `mysql` service with support for `m… 0 [OK]
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 0 [OK]
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 0 [OK]
拉取镜像
拉取官方镜像,下载MySQL镜像。
docker@default:~# docker pull mysql
若出现错误,建议再次尝试。
docker@default:~$ docker pull mysql
Using default tag: latest
Error response from daemon: Get https://registry-1.docker.io/v2/library/mysql/manifests/latest: Get https://auth.docker.io/token?scope=repository%3Alibrary%2Fmysql%3Apull&service=registry.docker.io: net/http: request canceled (Client.Timeout exceeded while awaiting headers)
在本地镜像列表中查看repository为mysql的镜像
docker@default:~# docker images | grep mysql
mysql latest 7bb2586065cd 4 weeks ago 477MB
创建容器
创建并启动MySQL容器
$ docker run --name mysql --rm -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql
c5e21285f86c772a9e7d513839d0a847b6cd9c8d95db6750b39800712e52dc05
docker run --name mysql --rm -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql
-
--name
为新建的容器命名 -
-e
配置信息,MYSQL_ROOT_PASSWORD配置MySQL的root用户的登录密码 -
-p
端口映射,此处映射主机3306端口到容器的3306端口 -
-d
以后台守护进程方式运行,成功启动容器后输出容器的完整ID -
-v
将宿主机中MySQL配置挂载到容器中MySQL配置中 -
mysql
MySQL镜像名称
查看容器运行状态
docker@default ~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5e21285f86c mysql "docker-entrypoint.s…" 3 seconds ago Up 3 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
db68453f8674 swoft/swoft "php /var/www/swoft/…" 13 hours ago Up 6 hours 0.0.0.0:80->80/tcp myswoft
运行容器
进入mysql容器
docker@default ~# docker exec -it mysql bash
查看MySQL数据库安装路径
root@c5e21285f86c:/# whereis mysql
mysql: /usr/bin/mysql /usr/lib/mysql /etc/mysql
连接MySQL数据库
root@c5e21285f86c:/# mysql -uroot -p
查看MySQ数据库版本
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.15 |
+-----------+
1 row in set (0.00 sec)
查看MySQL配置文件
root@c5e21285f86c:/# whereis mysql
mysql: /usr/bin/mysql /usr/lib/mysql /etc/mysql
root@c5e21285f86c:/# cd /etc/mysql && ls -alih
total 20K
1259 drwxr-xr-x 1 root root 4.0K Apr 25 00:23 .
2098259 drwxr-xr-x 1 root root 4.0K May 24 16:43 ..
1260 drwxrwxr-x 1 root root 4.0K Apr 25 00:22 conf.d
1262 -rw-rw-r-- 1 root root 1.2K Apr 25 00:22 my.cnf
918 -rw-r--r-- 1 root root 1.5K Apr 13 12:08 my.cnf.fallback
查看MySQL著配置文件
root@c5e21285f86c:/# cat 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/
这里需要将容器中MySQL的配置文件和数据文件映射到宿主机上。需要映射的目录分别为:
- 主配置文件
/etc/mysql/my.cnf
- 配置文件夹
/etc/mysql/conf.d/
- 数据文件夹
/var/lib/mysql
在宿主机上分别建立目录
/share/mysql/my.cnf
/share/mysql/conf.d/
/share/mysql/data/
将容器中主配置文件内容拷贝到宿主机对应文件中,家下来停止并关闭容器,重新运行并映射MySQL。
docker@default:~$ docker rm mysql
docker@default:~$ docker ps -a
先不添加--rm
参数,根据日志查看是否运行成功。
docker@default:~$ docker run --name mysql -v /share/mysql/my.cnf:/etc/mysql/my.cnf -v /share/mysql/conf.d:/etc/mysql/conf.d -v /share/mysql/data:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_P ASSWORD=root -d mysql
docker@default:~$ docker logs mysql
ERROR: mysqld failed while attempting to check config
command was: "mysqld --verbose --help"
mysqld: [Warning] World-writable config file '/etc/mysql/my.cnf' is ignored.
mysqld: Error on realpath() on '/var/lib/mysql-files' (Error 2 - No such file or directory)
2019-05-24T17:09:41.734998Z 0 [ERROR] [MY-010095] [Server] Failed to access directory for --secure-file-priv. Please make sure that directory exists and is accessible by MySQL Server. Supplied value : /var/lib/mysql-files
2019-05-24T17:09:41.737001Z 0 [ERROR] [MY-010119] [Server] Aborting
问题是运行容器的时候,容器内的数据文件夹还没有建立,此时映射失败,是这样的吗?
先删掉再说,这里注意下docker run
命令中的双下划线应该在单下划线的前面,否则参数无法生效。
root@default:~# docker rm mysql
想想先挂载能挂载的,挂载成功后在挂载不能挂载的,问题是如何在挂载成功后再增加挂载呢?
$ root@default:~# docker run --rm --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql -v /share/mysql/my.cnf:/etc/mysql/my.cnf -v /share/mysql/conf.d:/etc/mysql/conf.d
居然挂载成功呢,那问题就在/var/lib/mysql
这个文件夹上面。
错误日志是这样显示的:文件或文件夹不存在
mysqld: Error on realpath() on '/var/lib/mysql-files' (Error 2 - No such file or directory)
注意-v
参数中宿主机的目录是需要提前建立的,对应容器中的目录是自动创建的。既然是这样,是不是/var/lib/mysql
创建时文件权限的问题呢?沿着这个思路,先修改主配置文件my.cnf
中datadir
的路径。
$ vim my.cnf
datadir = /data
再来重新挂载
root@default:~# docker run --rm --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql -v /share/mysql/my.cnf:/etc/mysql/my.cnf -v /share/mysql/conf.d:/etc/mysql/conf.d -v /share/my sql/data:/data
OK,挂载成功,容器正常运行中!不过,马上问题来了,接着容器挂掉了。
root@default:~# docker logs mysql
Initializing database
2019-05-24T17:37:22.859827Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2019-05-24T17:37:22.859842Z 0 [ERROR] [MY-010083] [Server] --verbose is for use with --help; did you mean --log-error-verbosity?
2019-05-24T17:37:22.859892Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.16) initializing of server in progress as process 29
2019-05-24T17:37:24.394670Z 0 [ERROR] [MY-010147] [Server] Too many arguments (first extra is '/share/mysql/my.cnf:/etc/mysql/my.cnf').
2019-05-24T17:37:24.394679Z 0 [ERROR] [MY-013236] [Server] Newly created data directory /var/lib/mysql/ is unusable. You can safely remove it.
2019-05-24T17:37:24.395044Z 0 [ERROR] [MY-010119] [Server] Aborting
2019-05-24T17:37:26.153483Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.16) MySQL Community Server - GPL.
查看错误
[ERROR] [MY-010147] [Server] Too many arguments (first extra is '/share/mysql/my.cnf:/etc/mysql/my.cnf').
[ERROR] [MY-013236] [Server] Newly created data directory /var/lib/mysql/ is unusable. You can safely remove it.
最后考虑是不是命令有问题呢?
docker@default:~$ docker ps -a
docker@default:~$ docker run -it -d -p 3306:3306 --restart always -e MYSQL_ROOT_PASSWORD=root --name mydb -v /share/mysql/my.cnf:/etc/mysql/my.cnf -v /share/mysql/conf.d:/etc/mysql/conf.d -v /share/mysql/data:/data mysql/mysql-server
OK,先用着这个吧!
创建远程连接账户
MySQL创建远程连接账户
mysql> CREATE USER 'jc'@'%' IDENTIFIED WITH mysql_native_password BY 'junchow';
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'jc'@'%';
Query OK, 0 rows affected (0.00 sec)
验证调试
本地Windows系统使用Navicat连接测试
若连接失败,主要原因如下:
- 防火墙是否添加3306端口
- 是否添加或设置远程连接账户