## Docker 部署 MySQL
### 1. 单节点部署
#### 1.1 搜索镜像
进入 docker hub 镜像仓库地址:https://hub.docker.com/
搜索 MySQL,结果网站:[[Mysql - Official Image | Docker Hub](https://hub.docker.com/_/mysql)
内容:
```text
Supported tags and respective Dockerfile links
8.0.26, 8.0, 8, latest
5.7.35, 5.7, 5
5.6.51, 5.6
```
#### 1.2 拉取镜像
拉取mysql:5.7.35到本地
```shell
docker pull mysql:5.7.35
```
查看镜像
```shell
docker images
```
返回:
```shell
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7.35 8a8a506ccfdc 14 hours ago 448MB
```
#### 1.3 创建和启动容器
```shell
docker run -d --name mysql5.7.35 -p 13306:3306 -v $PWD/conf:/etc/mysql -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 8a8a506ccfdc
```
> -d:后台运行容器
>
> --name:指定容器名
>
> -p:指定服务运行的端口(13306:应用在宿主机的访问端口)
>
> -v:映射目录或文件
>
> -e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码
>
> 8a8a506ccfdc:镜像 ID
查看容器启动情况:
```shell
$ docker ps|grep mysql
50635c37c5b4 8a8a506ccfdc "docker-entrypoint.s…" 49 seconds ago Up 48 seconds 33060/tcp, 0.0.0.0:13306->3306/tcp, :::13306->3306/tcp mysql5.7.35
```
#### 1.4 进入 MySQL 容器
登陆容器
```shell
$ docker exec -it mysql5.7.35 bash
```
登陆 MySQL
```shell
$ mysql -uroot -p --default-character-set=utf8
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, 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.
```
```sql
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
```
设置远程登陆 MySQL
```sql
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
```
```sql
mysql> select host,user from user;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
4 rows in set (0.00 sec)
```
> +-----------+---------------+
> | host | user |
> +-----------+---------------+
> | % | root |
> +-----------+---------------+
说明 root 用户可以在任何地方可以访问该数据库
> ```
> # 设置root用户在任何地方进行远程登录,并具有所有库任何操作权限,(公司绝对不能这么做,暴露的攻击面太大),这里只是做测试。
> mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
>
> # 刷新权限
> mysql> FLUSH PRIVILEGES;
> ```
#### 1.5 清除
查看容器
```shell
$ docker ps|grep mysql
50635c37c5b4 8a8a506ccfdc "docker-entrypoint.s…" 19 minutes ago Up 19 minutes 33060/tcp, 0.0.0.0:13306->3306/tcp, :::13306->3306/tcp mysql5.7.35
```
停止容器
```shell
docker stop 50635c37c5b4
```
删除容器
```shell
docker rm 50635c37c5b4
```
删除宿主机的挂载目录:/root/mysql