1、查看可用的 MySQL 版本
访问 MySQL 镜像库地址:
https://hub.docker.com/_/mysql?tab=tags 。
2、拉取 MySQL 8.0.30 镜像
拉取官方的指定版本的镜像:
docker pull mysql:8.0.30
[root@localhost deploy]# docker pull mysql:8.0.30
8.0.30: Pulling from library/mysql
295ca2342728: Pull complete
79af4312a7e0: Pull complete
48d3d73d1704: Pull complete
521b8724b397: Pull complete
b8cf360b4a14: Pull complete
0115482cc006: Pull complete
a360b08917ea: Pull complete
12deeb3c1323: Pull complete
ee1dc10db1e9: Pull complete
64be404ad29c: Pull complete
1921fe8879a2: Pull complete
Digest: sha256:3c1aab708f6e57fc4dccafe36baccc7219acfb4ec450f3fb6d370ea89409e906
Status: Downloaded newer image for mysql:8.0.30
docker.io/library/mysql:8.0.30
3、查看本地镜像
使用以下命令来查看是否已安装了 mysql:
docker images
[root@localhost deploy]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8.0.30 dbaea59d1b41 7 weeks ago 449MB
在上图中可以看到我们已经安装了8.0.30版本的 mysql 镜像。
4、运行容器
docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:8.0.30
参数说明:
-
-p 3306:3306 :映射容器服务的 3306 端口到宿主机的 3306 端口,外部主机可以直接通过 宿主机ip:3306 访问到 MySQL 的服务。
-
MYSQL_ROOT_PASSWORD=123456:设置 MySQL 服务 root 用户的密码
[root@localhost deploy]# docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:8.0.30
aab502119678b471aa2c7cb4e0b2d5f091c65e46db0e48be76f462dccb5a1d4c
5、安装成功
通过 docker ps 命令查看是否安装成功:
[root@localhost deploy]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aab502119678 mysql:8.0.30 "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql-test
可以通过 root 和密码 123456 访问 MySQL 服务。
[root@localhost deploy]# docker exec -it mysql-test /bin/bash #进入MySQL容器
bash-4.4# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.30 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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>