centos8启动docker-mysql8容器
Posted PacosonSWJTU
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos8启动docker-mysql8容器相关的知识,希望对你有一定的参考价值。
【README】
本文记录了 centos8 安装,启动mysql8的docker容器的步骤;
【1】安装mysql8 docker容器
步骤1, 查看mysql8 docker镜像版本 ;
最简单的方式是上 Docker Hubhttps://hub.docker.com/直接搜索mysql,查看其 tag,如下:
步骤2,下载 mysql:8.0.26 版本镜像;
# 查看本地所有镜像
[root@centos204 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest 4ce9babdd885 2 days ago 680MB
hello-world latest feb5d9fea6a5 3 weeks ago 13.3kB
# 下载 mysql 8.0.26 镜像
[root@centos204 ~]# docker pull mysql:8.0.26
8.0.26: Pulling from library/mysql
b380bbd43752: Pull complete
f23cbf2ecc5d: Pull complete
30cfc6c29c0a: Pull complete
b38609286cbe: Pull complete
8211d9e66cd6: Pull complete
2313f9eeca4a: Pull complete
7eb487d00da0: Pull complete
a5d2b117a938: Pull complete
1f6cb474cd1c: Pull complete
896b3fd2ab07: Pull complete
532e67ebb376: Pull complete
233c7958b33f: Pull complete
Digest: sha256:5d52dc010398db422949f079c76e98f6b62230e5b59c0bf7582409d2c85abacb
Status: Downloaded newer image for mysql:8.0.26
docker.io/library/mysql:8.0.26
# 查看本地所有镜像
[root@centos204 ~]#
[root@centos204 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest 4ce9babdd885 2 days ago 680MB
mysql 8.0.26 9da615fced53 2 days ago 514MB
hello-world latest feb5d9fea6a5 3 weeks ago 13.3kB
[root@centos204 ~]#
步骤3,启动mysql容器
# 启动mysql容器
[root@centos204 ~]# docker run -d --name mysql01 mysql:8.0.26
9c44ea82b1507e6bce54b69a786066ae56cb95c36411efa1cee4695e6c39525f
# 查看容器列表 ,exited 表示启动失败;或未启动
[root@centos204 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c44ea82b150 mysql:8.0.26 "docker-entrypoint.s…" 12 seconds ago Exited (1) 4 seconds ago mysql01
99e3813639c8 tomcat "catalina.sh run" 11 hours ago Exited (255) 2 minutes ago 0.0.0.0:8886->8080/tcp, :::8886->8080/tcp naughty_maxwell
e7f01a261ee8 tomcat "catalina.sh run" 11 hours ago Exited (255) 2 minutes ago 0.0.0.0:8887->8080/tcp, :::8887->8080/tcp beautiful_proskuriakova
0b1be1a3dcd2 tomcat "catalina.sh run" 11 hours ago Exited (255) 2 minutes ago 0.0.0.0:8888->8080/tcp, :::8888->8080/tcp modest_heyrovsky
0a5b713c0021 hello-world "/hello" 35 hours ago Exited (0) 35 hours ago frosty_turing
# 查看mysql容器日志
[root@centos204 ~]# docker logs 9c44ea82b150
2021-10-16 01:33:47+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started.
2021-10-16 01:33:51+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2021-10-16 01:33:51+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started.
# 报错原因
2021-10-16 01:33:51+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of the following:
- MYSQL_ROOT_PASSWORD
- MYSQL_ALLOW_EMPTY_PASSWORD
- MYSQL_RANDOM_ROOT_PASSWORD
[root@centos204 ~]#
步骤4,指定mysql密码为 root,账号默认为root;
# 重新启动mysql docker容器,设置端口映射,密码
[root@centos204 ~]# docker run --name mysql01 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:8.0.26
720f23458abfdfafa93707e83f1cbb2ff085eb47d68c91d43088b0da7db97923
# 查看所有docker容器
[root@centos204 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
720f23458abf mysql:8.0.26 "docker-entrypoint.s…" 49 seconds ago Up 45 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql01
99e3813639c8 tomcat "catalina.sh run" 11 hours ago Exited (255) 17 minutes ago 0.0.0.0:8886->8080/tcp, :::8886->8080/tcp naughty_maxwell
e7f01a261ee8 tomcat "catalina.sh run" 11 hours ago Exited (255) 17 minutes ago 0.0.0.0:8887->8080/tcp, :::8887->8080/tcp beautiful_proskuriakova
0b1be1a3dcd2 tomcat "catalina.sh run" 12 hours ago Exited (255) 17 minutes ago 0.0.0.0:8888->8080/tcp, :::8888->8080/tcp modest_heyrovsky
0a5b713c0021 hello-world "/hello" 35 hours ago Exited (0) 35 hours ago frosty_turing
[root@centos204 ~]#
步骤5,开放3306防火墙访问端口
# 开放3306防火墙访问端口
[root@centos204 ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@centos204 ~]# firewall-cmd --reload
success
[root@centos204 ~]# firewall-cmd --zone=public --list-ports
8888/tcp 8887/tcp 8886/tcp 3306/tcp
步骤6, 通过dbeaver连接 192.168.163.204:3306
【2】 启动多个mysql-docker容器
步骤1,开启2个mysql-docker容器,端口为3302 3303;
并设置mysql服务器参数, 参考了 https://hub.docker.com/_/mysql?tab=description
[root@centos204 ~]# docker run --name mysql02 -p 3302:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:8.0.26 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
3d4caede6869e33e6cd1d267ed80cbc4f89faab8288b64c4b17d5ef40005e91d
[root@centos204 ~]# docker run --name mysql03 -p 3303:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:8.0.26 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
13132c9e47f35aeaf9e6cc9567a72f9d41850ad42deeb294c4a98ef2e9094034
# 查看所有mysql-docker容器
[root@centos204 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
13132c9e47f3 mysql:8.0.26 "docker-entrypoint.s…" 2 minutes ago Up About a minute 33060/tcp, 0.0.0.0:3303->3306/tcp, :::3303->3306/tcp mysql03
3d4caede6869 mysql:8.0.26 "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 33060/tcp, 0.0.0.0:3302->3306/tcp, :::3302->3306/tcp mysql02
720f23458abf mysql:8.0.26 "docker-entrypoint.s…" 39 minutes ago Up 39 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql01
步骤2, 开通3302 3303 防火墙网络权限 ;
# 开通3302 3303 端口
[root@centos204 ~]# firewall-cmd --zone=public --add-port=3302/tcp --permanent
success
[root@centos204 ~]# firewall-cmd --zone=public --add-port=3303/tcp --permanent
success
[root@centos204 ~]# firewall-cmd --reload
success
[root@centos204 ~]# firewall-cmd --zone=public --list-ports
8888/tcp 8887/tcp 8886/tcp 3306/tcp 3302/tcp 3303/tcp
[root@centos204 ~]#
步骤3, win10的dbeaver 测试结果;
【补充】
更多mysql-docker容器启动参数,refer2 Docker Hub
以上是关于centos8启动docker-mysql8容器的主要内容,如果未能解决你的问题,请参考以下文章