Docker安装MySQL5

参考文档:https://www.runoob.com/docker/docker-install-mysql.html

1. 查找Docker Hub上的mysql镜像

  命令:docker search mysql

[root@localhost hh]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   8696                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3036                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   643                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   63                                      
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   53                                      
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]

  

2. 拉取官方的镜像,标签为5.6

  命令:docker pull mysql:5.6

[root@localhost hh]# docker pull mysql:5.6
5.6: Pulling from library/mysql
80369df48736: Pull complete 
e8f52315cb10: Pull complete 
cf2189b391fc: Pull complete 
cc98f645c682: Pull complete 
27a27ac83f74: Pull complete 
f3f8d5bb9498: Pull complete 
bbfacbe5a185: Pull complete 
9db7adff1e15: Pull complete 
f8402500c236: Pull complete 
b0319efc9cd8: Pull complete 
d19ab308a635: Pull complete 
Digest: sha256:411cb46904b646d1260e075505ee75d7457511412011eb03928e956eac1c0bf9
Status: Downloaded newer image for mysql:5.6
docker.io/library/mysql:5.6

 

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

[root@localhost hh]# docker images |grep mysql
mysql               5.6                 b3983abaa3fe        10 hours ago        302MB

 

4. 运行镜像

[root@localhost hh]# docker run -p 3306:3306 --name mymysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6
25ff676e442e517a895a94ca744266a024ccf1c8c2656f55dfb5fb73af0a835a

  命令说明:

  -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 ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
25ff676e442e        mysql:5.6           "docker-entrypoint.s…"   32 seconds ago      Up 29 seconds       0.0.0.0:3306->3306/tcp   mymysql

 

6. 进入容器

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

 

7. 操作MySQL

root@25ff676e442e:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.46 MySQL Community Server (GPL)

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

后续就是操作MySQL的东西了,总的来说,使用Docker安装MySQL,比直接在CentOS上安装步骤和时间都省太多了。

不过:因为MySQL需要大量的硬件资源,如果放到Docker中迁移维护都是相当麻烦的,所以也就自己虚拟机上玩玩可以,公司的任何环境都不要使用这种方式,否则后患无穷。

posted @ 2019-10-17 23:27  闲人鹤  阅读(452)  评论(1编辑  收藏  举报