參考於:https://hub.docker.com/r/mysql/mysql-server/
1.獲取MySql鏡像
[root@hanzo hanzo]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relati... 4976 [OK]
......
[root@hanzo hanzo]# docker pull mysql
......
[root@hanzo hanzo]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest b4e78b89bcf3 43 hours ago 412MB
......
2.啓動實例
sudo docker run -d \
-p 3306:3306 \
--name mysql \
-v $PWD/Dockers/mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=guiwang \
mysql:latest
普通用戶啓動:
[hanzo@hanzo ~]$ sudo docker run -d \
> -p 3306:3306 \
> --name mysql \
> -v $PWD/Dockers/mysql/conf:/etc/mysql/conf.d \
> -e MYSQL_ROOT_PASSWORD=guiwang \
> mysql:latest
d1fefcac7ca160635e90eadee054b9069f8ab8d778e5190e8fd4a6c70fd7cd4f
[hanzo@hanzo ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1fefcac7ca1 mysql:latest "docker-entrypoint..." 8 minutes ago Up 8 minutes 0.0.0.0:3306->3306/tcp mysql
- -p 3306:3306:将容器的3306端口映射到主机的3306端口
- -v $PWD/Dockers/mysql/conf:/etc/mysql/conf.d:将主机当前目录下的/Dockers/mysql/conf目錄挂载到容器的/etc/mysql/conf.d目錄。根據需求定義目錄,在所定義的目錄下內置一個後綴名爲cnf的文件,然後在執行上述命令。
- -e MYSQL_ROOT_PASSWORD=guiwang:初始化root用户的密码
3.連接MySql
- 通過容器的MySql命令行工具連接
[root@hanzo hanzo]# docker exec -it mysql mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.19 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.19 | #mysql版本
+-----------+
1 row in set (0.00 sec)
也可以先進入容器,然後在連接MySql。
[root@hanzo hanzo]# docker exec -it mysql bash
root@d1fefcac7ca1:/# mysql -uroot -p
Enter password: