Docker安装MySQL8

1. 查找Docker Hub上的mysql镜像

  命令:docker search mysql

[root@192 ~]# docker search mysql
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                           MySQL is a widely used, open-source relation…   13387     [OK]       
mariadb                         MariaDB Server is a high performing open sou…   5110      [OK]       
phpmyadmin                      phpMyAdmin - A web interface for MySQL and M…   666       [OK]       
percona                         Percona Server is a fork of the MySQL relati…   592       [OK]       
bitnami/mysql                   Bitnami MySQL Docker Image                      78                   [OK]

 

2. 拉取官方的镜像,版本为8.0.27

  命令:docker pull mysql:8.0.27

[root@192 ~]# docker pull mysql:8.0.27
8.0.27: Pulling from library/mysql
72a69066d2fe: Downloading 
93619dbc5b36: Download complete 
99da31dd6142: Download complete 
626033c43d70: Download complete 
37d5d7efb64e: Download complete 
ac563158d721: Download complete 
d2ba16033dad: Download complete 
688ba7d5c01a: Download complete 
00e060b6d11d: Download complete 
1c04857f594f: Download complete 
4d7cfa90e6ea: Download complete 
e0431212d27d: Download complete 
8.0.27: Pulling from library/mysql
72a69066d2fe: Pull complete 
93619dbc5b36: Pull complete 
99da31dd6142: Pull complete 
626033c43d70: Pull complete 
37d5d7efb64e: Pull complete 
ac563158d721: Pull complete 
d2ba16033dad: Pull complete 
688ba7d5c01a: Pull complete 
00e060b6d11d: Pull complete 
1c04857f594f: Pull complete 
4d7cfa90e6ea: Pull complete 
e0431212d27d: Pull complete 
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:8.0.27
docker.io/library/mysql:8.0.27

 

3. 等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为mysql镜像。

[root@192 ~]# docker images | grep mysql
mysql        8.0.27    3218b38490ce   10 months ago   516MB

 

4. 运行镜像

  注意:服务器上同时安装多个版本的MySQL时,配置文件的挂载地址不能相同,否则会出现,我之前先装了5.6的版本,卸载之后才安装成功MySQL8的

[root@192 /]# docker run -p 3306:3306 --name mysql8 -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0.27
e5a23462ac8c4e4d4132ad91a4b95542cec03176a0619500104d62db6aeed81b

  命令说明:

  -p 3306:3306:将容器的 3306 端口映射到主机的 3306 端口。

  --name : 镜像的名称

  -v $PWD/conf:/etc/mysql/conf.d:将主机当前目录下的 conf/my.cnf 挂载到容器的 /etc/mysql/my.cnf。

  -v $PWD/logs:/logs:将主机当前目录下的 logs 目录挂载到容器的 /logs。

  -v $PWD/data:/var/lib/mysql :将主机当前目录下的data目录挂载到容器的 /var/lib/mysql 。

  -e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码。

 

5. 进入容器

[root@localhost hh]# docker exec -it mymysql bash
root@25ff676e442e:/# 

 

6. 操作MySQL

root@25ff676e442e:/# mysql -u root -p123456

mysql>

  接下来就是MySQL的操作了。

 

posted @ 2023-03-09 09:44  闲人鹤  阅读(739)  评论(0编辑  收藏  举报