写作时间:2020-01-29
Docker: 2.1.0.2, MySQL: 8, MacOS: 10.15.2,

Docker 安装 MySQL

MySQL 是世界上最受欢迎的开源数据库。凭借其可靠性、易用性和性能,MySQL 已成为 Web 应用程序的数据库优先选择。

笔者的环境为MacOS,安装Docker for Mac客户端,请参考SpringBoot之Docker入门

1. 查看可用的 MySQL 版本

访问 MySQL 镜像库地址:https://hub.docker.com/_/mysql

可以通过 Sort by 查看其他版本的 MySQL,默认是最新版本 mysql:latest 。
在这里插入图片描述

默认的选择Description标签,需要切换其它版本,点击Tags便签。
在这里插入图片描述
此外,我们还可以用 docker search mysql 命令来查看可用版本:

% docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9073                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3206                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   673                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   67                                      
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   60                                      
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      35                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   34                                      
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   28                                      [OK]
prom/mysqld-exporter                                                              26                                      [OK]
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   24                                      
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   18                                      
circleci/mysql                    MySQL is a widely used, open-source relation…   16                                      
mysql/mysql-router                MySQL Router provides transparent routing be…   14  

2. 拉取 MySQL 镜像

$ docker pull mysql:latest
#拉取8版本的镜像
$ docker pull mysql:8

在这里插入图片描述

3. 查看本地镜像

使用以下命令来查看是否已安装了 mysql:

$ docker images

在这里插入图片描述

4. 运行容器

% docker run -itd --name docker-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -v ~/Docker/db/mysql/8.0.9:/var/lib/mysql mysql

参数解析:

  1. -itdi是交互式操作,t是一个终端,d指的是在后台运行
  2. -p : 设置端口,
  3. --name : 取名 ,
  4. -e MYSQL_ROOT_PASSWORD=123456 : -e 指定环境变量, 设置 账号为 root ,密码为 123456.
  5. -v ~/mysql/data:/var/lib/mysql 是将Docker的/var/lib/mysql 文件夹映射到本机的~/Docker/db/mysql/8.0.9

查看进程如下:

% docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
e9488be5b71d        mysql               "docker-entrypoint.s…"   7 hours ago         Up 7 hours          0.0.0.0:3306->3306/tcp, 33060/tcp   docker-mysql

如果停掉容器,

$ docker stop Container ID or Names

下次启用已经停掉的容器,用下面的命令即可

$ docker start Container ID or Names

比如笔者启动容器docker-mysql

% docker start docker-mysql
docker-mysql

5. 进入容器

% docker exec -it docker-mysql bash

进入容器后,Terminal打头的格式为root@ContainerId, 比如笔者的

 root@0d4a5de80d4d:/# 

6. 操作数据库

6.1 登录MySQL

$ mysql -u root -p

root@e9488be5b71d:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

显示数据库

$ show databases;

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.05 sec)

6.2. 创建数据库用户zgpeace
创建数据库用户zgpeace,并把所有权限授权与zgpeace, 退出root用户。

mysql> create user 'zgpeace'@'%' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.02 sec)

mysql> grant all privileges on *.* to 'zgpeace'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye

用新创建的用户zgpeace登录。

root@e9488be5b71d:/# mysql -u zgpeace -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

7. 查看MySQL的配置信息,操作日志

打开一个新的Terminal
7.1 查看MySQL的配置信息

$ docker inspect Container ID or Names

笔者的数据库信息

% docker inspect docker-mysql
[
    {
        "Id": "e9488be5b71de86078c1e7714e2ac0ce7869d5216f79d40a918e880b8e52a315",
        "Created": "2020-01-29T01:47:25.889370883Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "mysqld"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            ...
        },
        "Image": "sha256:3a5e53f6328162f8d8bc70131658a721e8e7dcf7495f2fae7cfe4febdbcfefbb",
        ...
    }
]

7.2 查看MySQL的操作日志

$ docker logs Container ID or Names

比如笔者列出容器docker-mysql的关键日志

% docker logs docker-mysql
2020-01-29 01:47:26+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.19-1debian9 started.
2020-01-29 01:47:26+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2020-01-29 01:47:26+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.19-1debian9 started.
2020-01-29 01:47:27+00:00 [Note] [Entrypoint]: Initializing database files
2020-01-29 01:47:31+00:00 [Note] [Entrypoint]: Database files initialized
2020-01-29 01:47:31+00:00 [Note] [Entrypoint]: Starting temporary server
2020-01-29 01:47:36+00:00 [Note] [Entrypoint]: Stopping temporary server
2020-01-29 01:47:38+00:00 [Note] [Entrypoint]: Temporary server stopped
2020-01-29 01:47:38+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.

8. 踩坑填坑简历如下

如果是新电脑,一路走得很顺。笔者遇到坎坷历程如下:

8.1 拉取镜像image报错
Docker获取镜像报错 docker: Error response from daemon

% docker pull mysql:8  
Error response from daemon: Get https://registry-1.docker.io/v2/library/mysql/manifests/8: EOF

解决方案:
点击桌面顶栏的 Docker 图标 > 选择 Preferences > 在 Daemon 标签下的 Registry mirrors 列表中加入下面的镜像地址:

http://141e5461.m.daocloud.io

点击 Apply & Restart 按钮使设置生效。
在这里插入图片描述
8.2 加载镜像image到Container报错:显示端口3306已经被暂用

% docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
105a2534d6e2ea7deecb051092a239b1c6789e112e7605b94895cdc88194924b
docker: Error response from daemon: driver failed programming external connectivity on endpoint mysql-test (bfee7cfce6761d115797dbe0077495c1a5b30afa2f1e8a9c9abfc429d19956db): 
Error starting userland proxy: listen tcp 0.0.0.0:3306: bind: address already in use.

尝试查找占用端口3306的进程,并杀掉该进程。
查找暂用端口3306的进程<PID>

// sudo lsof -i :port
sudo lsof -i :3306

杀掉该进程<PID>

sudo kill -9 <PID>

发现可以杀掉该进程,但是又启用了新的进程暂用端口3306. Root Cause是因为笔者本地安装了MySQL。
解决方法:左上角苹果标识 > System Preferences > 最下面一行 MySQL > Instances > Stop MySQL Server. 关闭以后的截图如下:
在这里插入图片描述
启动Container成功

% docker run -itd --name docker-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
e9488be5b71de86078c1e7714e2ac0ce7869d5216f79d40a918e880b8e52a315

9. 参考

https://segmentfault.com/a/1190000016083023

https://stackoverflow.com/questions/3855127/find-and-kill-process-locking-port-3000-on-mac/59962930#59962930

https://www.runoob.com/docker/docker-install-mysql.html

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐