部署环境
主机系统:CentOS8
IP地址:192.168.186.123
Docker版本:20.10.11
实现目的
Docker创建 MySQL 8.0版本的容器作为数据库,容器内数据库端口 3306 映射到主机的 13306 端口,并且实现外部第三方软件 Navicat for MySQL 可以远程连接数据库
一、主机Docker配置
1、查看本地镜像:docker images
[root@CentOS8 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest aea9b698d7d1 6 days ago 113MB
nginx latest f652ca386ed1 7 days ago 141MB
centos latest 5d0da3dc9764 2 months ago 231MB
2、如果本地镜像没有数据库则搜索MySQL数据库:docker search mysql
[root@CentOS8 ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11803 [OK]
mariadb MariaDB Server is a high performing open sou… 4492 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 885 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 92
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 89
centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 54
prom/mysqld-exporter 44 [OK]
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
tutum/mysql Base docker image to run a MySQL database se… 35
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 34
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 31 [OK]
mysql/mysql-router MySQL Router provides transparent routing be… 23
centos/mysql-56-centos7 MySQL 5.6 SQL database server 21
arey/mysql-client Run a MySQL client from a docker container 19 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 17 [OK]
genschsa/mysql-employees MySQL Employee Sample Database 8 [OK]
yloeffler/mysql-backup This image runs mysqldump to backup data usi… 7 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3
idoall/mysql MySQL is a widely used, open-source relation… 3 [OK]
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 3 [OK]
centos/mysql-80-centos7 MySQL 8.0 SQL database server 2
jelastic/mysql An image of the MySQL database server mainta… 2
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 1 [OK]
3、拉取MySQL数据库:docker pull mysql
[root@CentOS8 ~]# docker pull mysql
4、再次查看本地镜像:docker images
[root@CentOS8 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest aea9b698d7d1 6 days ago 113MB
mysql latest bbf6571db497 7 days ago 516MB
nginx latest f652ca386ed1 7 days ago 141MB
centos latest 5d0da3dc9764 2 months ago 231MB
5、开启主机防火墙端口 13306,用于数据库的远程链接
# 开启端口 13306
[root@CentOS8 ~]# firewall-cmd --zone=public --add-port=13306/tcp --permanent
success
# 重启防火墙使配置生效
[root@CentOS8 ~]# systemctl restart firewalld.service
二、容器的操作
1、创建一个 MySQL 容器
使用镜像名称为 mysql 创建一个 mysql8-test 名称的容器,赋予系统root权限、设置数据库密码为123456、将容器的3306端口映射到主机的13306端口、设置数据库编码和字符集为UTF8
使用命令:
docker run --name=mysql8-test --privileged=true -v /data/docker:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -p 13306:3306 -d mysql --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
[root@CentOS8 ~]# docker run --name=mysql8-test --privileged=true -v /data/docker:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -p 13306:3306 -d mysql --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
98834d45062dc501ce19b2e06e074b056d80e44b73fd22dd11497c841b1bcf7b
命令解释:
--name=mysql8-test # 创建容器名称为:mysql8-test
--privileged=true # 赋予系统root权限
MYSQL_ROOT_PASSWORD=123456 # 设置数据库密码为:123456
-p 13306:3306 # 映射容器3306端口到主机的13306端口
--character-set-server=utf8mb4 # 设置数据库的默认编码和字符集为utf8
--collation-server=utf8mb4_unicode_ci
特别注意:-v /data/docker:/var/lib/mysql
这个参数的意思是:将容器内的 /var/lib/mysql 目录(默认是数据库存放的路径) 挂载到主机的 /data/docker目录下(自动创建)
重点:主机的/data/docker目录要确保在创建容器之前不存在,不然容器启动会有问题(经验之谈)
2、查看创建的容器ID:docker ps -a
[root@CentOS8 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
98834d45062d mysql "docker-entrypoint.s…" 3 minutes ago Up 2 minutes 33060/tcp, 0.0.0.0:13306->3306/tcp, :::13306->3306/tcp mysql8-test
数据库的容器ID为:7e5152bec6c1
容器名称为:mysql8-test
端口为:0.0.0.0:13306->3306/tcpSTATUS:状态为 Up表示容器已经启动,
如果是Created状态则启动下容器:docker start 7e5152bec6c1
3、进入容器并使用 bash 命令控制:docker exec -it mysql8-test /bin/bash
[root@CentOS8 ~]# docker exec -it mysql8-test /bin/bash
root@98834d45062d:/#
三、数据库的配置
1、进入数据库:mysql -uroot -p
root@98834d45062d:/# mysql -uroot -p
Enter password:
2、设置密码 123456 永不过期:
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)
3、刷新权限:FLUSH PRIVILEGES;
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
可以测试远程链接到数据库了
# mysql 配置文件路径
root@98834d45062d:/# ls /etc/mysql
conf.d my.cnf my.cnf.fallback
# 数据存放位置
root@98834d45062d:/# ls /var/lib/mysql
'#ib_16384_0.dblwr' binlog.000001 ca.pem ib_logfile0 mysql public_key.pem test123
'#ib_16384_1.dblwr' binlog.000002 client-cert.pem ib_logfile1 mysql.ibd server-cert.pem undo_001
'#innodb_temp' binlog.index client-key.pem ibdata1 performance_schema server-key.pem undo_002
auto.cnf ca-key.pem ib_buffer_pool ibtmp1 private_key.pem sys
如果需要修改数据库配置文件可以将 my.cnf 文件拷贝到主机挂载的 /data 目录下,修改完毕后再拷贝回容器 /etc/mysql 目录下(命令在主机模式下进行)
拷贝容器文件到主机:docker cp <容器ID>:/容器文件路径 /主机文件路径
拷贝主机文件到容器:docker cp /主机文件路径 <容器ID>:/容器文件路径
# 拷贝容器/etc/mysql/my.cnf目录下的文件到主机的/data目录
[root@CentOS8 ~]# docker cp 98834d45062d:/etc/mysql/my.cnf /data
# 这样就可以在主机下使用vi或者vim对文件进行编辑操作了
[root@CentOS8 ~]# vi /data/my.cnf
# 修改完配置文件以后再拷贝回容器目录下
[root@CentOS8 ~]# docker cp /data/my.cnf 98834d45062d:/etc/mysql/
# 修改完配置记得重启下容器
[root@CentOS8 ~]# docker restart 98834d45062d
my.cnf 默认配置文件
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Custom config should go here
!includedir /etc/mysql/conf.d/
四、测试远程链接
运行Navicat for MySQL,连接数据库
如果远程链接失败,请按照以下方法检查
1、检查防火墙是否开启对应的端口,比如这里的 13306
2、检查数据库是否开启远程链接权限
【A】进入数据库
【B】查看当前root账户是否有开启远程连接权限:select host,user from user where user='root';
如下所示:如果有 “| % | root |”这行说明远程链接已经开启
mysql> select host,user from user where user='root';
+-----------+------+
| host | user |
+-----------+------+
| % | root |
| localhost | root |
+-----------+------+
2 rows in set (0.00 sec)
如果没有开启则运行数据库命令
开启root账户远程链接权限:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'WITH GRANT OPTION;
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES; # 记得要刷新数据库权限使其生效
Query OK, 0 rows affected (0.00 sec)