文档课题:docker下部署mysql 5.6.51
系统:rhel 7.3 64位
1、删旧镜像
[root@Leo-Docker ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

[root@Leo-Docker ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
b24358c2ff28   hello-world   "/hello"   10 minutes ago   Exited (0) 10 minutes ago             wizardly_bhaskara

[root@Leo-Docker ~]# docker rm -f b24358c2ff28
b24358c2ff28

[root@Leo-Docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

[root@Leo-Docker ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    9c7a54a9a43c   3 months ago   13.3kB

[root@Leo-Docker ~]# docker rmi 9c7a54a9a43c
Untagged: hello-world:latest
Untagged: hello-world@sha256:dcba6daec718f547568c562956fa47e1b03673dd010fe6ee58ca806767031d1c
Deleted: sha256:9c7a54a9a43cca047013b82af109fe963fde787f63f9e016fdc3384500c2823d
Deleted: sha256:01bb4fce3eb1b56b05adf99504dafd31907a5aadac736e36b27595c8b92f07f1

[root@Leo-Docker ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
2、安装mysql
--搜索镜像
[root@Leo-Docker ~]# docker search mysql
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                           MySQL is a widely used, open-source relation…   14381     [OK]       
mariadb                         MariaDB Server is a high performing open sou…   5490      [OK]       
percona                         Percona Server is a fork of the MySQL relati…   620       [OK]       
phpmyadmin                      phpMyAdmin - A web interface for MySQL and M…   848       [OK]       
bitnami/mysql                   Bitnami MySQL Docker Image                      93                   [OK]
circleci/mysql                  MySQL is a widely used, open-source relation…   29                   
bitnami/mysqld-exporter                                                         5                    
ubuntu/mysql                    MySQL open source fast, stable, multi-thread…   51                   
cimg/mysql                                                                      0                    
rapidfort/mysql                 RapidFort optimized, hardened image for MySQL   23                   
rapidfort/mysql8-ib             RapidFort optimized, hardened image for MySQ…   9                    
google/mysql                    MySQL server for Google Compute Engine          23                   [OK]
hashicorp/mysql-portworx-demo                                                   0                    
rapidfort/mysql-official        RapidFort optimized, hardened image for MySQ…   9                    
newrelic/mysql-plugin           New Relic Plugin for monitoring MySQL databa…   1                    [OK]
elestio/mysql                   elestio mysql Docker Image                      0                    
databack/mysql-backup           Back up mysql databases to... anywhere!         89                   
bitnamicharts/mysql                                                             0                    
linuxserver/mysql               A Mysql container, brought to you by LinuxSe…   39                   
mirantis/mysql                                                                  0                    
docksal/mysql                   MySQL service images for Docksal - https://d…   0                    
linuxserver/mysql-workbench                                                     50                   
vitess/mysqlctld                vitess/mysqlctld                                1                    [OK]
eclipse/mysql                   Mysql 5.7, curl, rsync                          0                    [OK]
drupalci/mysql-5.5              https://www.drupal.org/project/drupalci         3                    [OK]

--拉取镜像
[root@Leo-Docker ~]# docker pull mysql:5.6
5.6: Pulling from library/mysql
35b2232c987e: Pull complete 
fc55c00e48f2: Pull complete 
0030405130e3: Pull complete 
e1fef7f6a8d1: Pull complete 
1c76272398bb: Pull complete 
f57e698171b6: Pull complete 
f5b825b269c0: Pull complete 
dcb0af686073: Pull complete 
27bbfeb886d1: Pull complete 
6f70cc868145: Pull complete 
1f6637f4600d: Pull complete 
Digest: sha256:20575ecebe6216036d25dab5903808211f1e9ba63dc7825ac20cb975e34cfcae
Status: Downloaded newer image for mysql:5.6
docker.io/library/mysql:5.6

--查看images
[root@Leo-Docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
mysql        5.6       dd3b2a5dcb48   20 months ago   303MB

3、创建容器
说明:
1)、容器内网络服务和外部机器不能直接通信;
2)、外部机器和宿主机可以直接通信;
3)、宿主机和容器可以直接通信;
4)、当容器中的网络服务需要被外部机器访问时,可将容器中提供服务的端口映射到宿主机的端口,外部机器访问宿主机的端口,从而间接访问容器的服务,此操作称为端口映射。

注意事项:
1)、需进行端口映射;
2)、需在宿主机的防火墙上开启对外访问端口;
3)、连接时需宿主机的IP地址.

--在/root下创建mysql目录用于存储mysql数据信息
[root@Leo-Docker ~]# mkdir /root/mysql
[root@Leo-Docker ~]# cd mysql

--在/root/mysql目录下创建容器.
# docker run -id \                 #镜像启动一个容器,-id表示交互式运行
-p 3307:3306 \                     #3307对外开放的端口号,3306为容器的端口,此步为端口映射,此处最好是3306:3306,3307以示区别
--name=mysql01 \                   #给该容器取名mysql01
-v $PWD/conf:/etc/mysql/conf.d \   #'-v'表示数据点挂载,'$PWD'为当前目录,容器内部会创建/etc/mysql/conf.d存放配置文件
-v $PWD/logs:/logs \               #存放日志文件的路径
-v $PWD/data:/var/lib/mysql \      #存放数据文件的路径
-e MYSQL_ROOT_PASSWORD=123456 \    #mysql对外开放的root密码
mysql:5.6                          #启动的镜像的名称

6c90d595cfeb0e1166f55735c409ad227475b8665e946e2eee6351f5b0030bc1

参数说明:
-i     #启动交互式容器
-d     #后台运行
-p     #端口映射
-v     #卷挂载
-e     #环境配置
--name #容器名字

4、相关查询
4.1、文件查询
[root@Leo-Docker mysql]# ll
total 0
drwxr-xr-x. 2 root    root   6 Aug 13 23:40 conf
drwxr-xr-x. 5 polkitd root 129 Aug 13 23:54 data
drwxr-xr-x. 2 root    root   6 Aug 13 23:40 logs
[root@Leo-Docker mysql]# pwd
/root/mysql
[root@Leo-Docker mysql]# ll data
total 110604
-rw-rw----. 1 polkitd input       56 Aug 13 23:40 auto.cnf
drwx------. 2 polkitd input       54 Aug 14 00:09 booksDB
-rw-rw----. 1 polkitd input 12582912 Aug 14 00:09 ibdata1
-rw-rw----. 1 polkitd input 50331648 Aug 14 00:09 ib_logfile0
-rw-rw----. 1 polkitd input 50331648 Aug 13 23:40 ib_logfile1
drwx------. 2 polkitd input     4096 Aug 13 23:40 mysql
drwx------. 2 polkitd input     4096 Aug 13 23:40 performance_schema

[root@Leo-Docker mysql]# cd /
[root@Leo-Docker /]# docker ps
CONTAINER ID   IMAGE   COMMAND                  CREATED          STATUS          PORTS                                  NAMES
6c90d595cfeb   mysql:5.6  "docker-entrypoint.s…" 20 seconds ago Up 19 seconds   0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mysql01

--进入docker查询文件
[root@Leo-Docker ~]# docker exec -it mysql01 /bin/bash
root@6c90d595cfeb:/etc/mysql# pwd
/etc/mysql
root@6c90d595cfeb:/etc/mysql# ls -ltr
total 8
-rw-r--r--. 1 root root  839 Jul  9  2016 my.cnf.fallback
-rw-r--r--. 1 root root 1497 Jan  5  2021 mysql.cnf
lrwxrwxrwx. 1 root root   24 Dec 21  2021 my.cnf -> /etc/alternatives/my.cnf
drwxr-xr-x. 2 root root   24 Dec 21  2021 mysql.conf.d
drwxr-xr-x. 2 root root    6 Aug 13 15:40 conf.d

root@6c90d595cfeb:/# ls -ltr logs
total 0
root@6c90d595cfeb:/# ls -ltr /var/lib/mysql
total 110604
-rw-rw----. 1 mysql mysql 50331648 Aug 13 15:40 ib_logfile1
drwx------. 2 mysql mysql     4096 Aug 13 15:40 performance_schema
-rw-rw----. 1 mysql mysql       56 Aug 13 15:40 auto.cnf
drwx------. 2 mysql mysql     4096 Aug 13 15:40 mysql
drwx------. 2 mysql mysql       54 Aug 13 16:09 booksDB
-rw-rw----. 1 mysql mysql 12582912 Aug 17 15:35 ibdata1
-rw-rw----. 1 mysql mysql 50331648 Aug 17 15:35 ib_logfile0

4.2、数据库验证
--进入mysql容器
[root@Leo-Docker /]# docker exec -it mysql01 /bin/bash
--登录mysql
root@6c90d595cfeb:/# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, 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 |
+--------------------+
3 rows in set (0.01 sec)

mysql> create database booksDB;
Query OK, 1 row affected (0.00 sec)

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

mysql> use booksDB;
Database changed

5、客户端连接
5.1、开启端口
使用外部mysql客户端操作mysql server,连接时需注意端口要和映射的端口保持一致。

注意:第一次配置完,若外部无法访问mysql server,重启docker、mysql容器即可。

开启端口
[root@Leo-Docker mysql]# firewall-cmd --zone=public --add-port=3307/tcp --permanent
success
[root@Leo-Docker mysql]# firewall-cmd --reload
success

5.2、连接测试
--客户端连接测试
[root@leo-mysql5721 ~]# ip a | grep 96
    inet 192.168.133.96/24 brd 192.168.133.255 scope global ens33
[root@leo-mysql5721 ~]# mysql -uroot -p123456 -h 192.168.133.108 -P 3307
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> 

注意:外部客户端连接容器内数据库使用的是3307的端口.

参考网址:
https://www.bilibili.com/video/BV1mU4y1Z7Mp/?spm_id_from=333.337.search-card.all.click&vd_source=8c872e2fd1d99229b38a73ed6718b776