Docker 部署MySQL5.7.20
# 一、下载镜像
[root@node-11 ~]# docker pull mysql:5.7.20
1
# 二、创建mysql目录
[root@node-11 ~]# mkdir /opt/mysql/{data,conf,logs} -p
目录说明:
data:数据目录
conf:配置文件
logs:日志目录
1
2
3
4
5
2
3
4
5
# 三、编辑配置文件
[root@node-11 ~]# vim /opt/mysql/conf/mysqld.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
lower_case_table_names = 1 ## 忽略mysql查询大小写
1
2
3
4
5
6
2
3
4
5
6
注意:这只是简单的mysql参数,具体的参数需要根据业务需求,自行添加即可,这里需要注意的是,涉及到mysql数据目录的话,一定要写容器内的路径,而不是映射到宿主机的目录。
# 四、启动
[root@node-11 ~]# docker run -d -p 3306:3306 --name mysql \
-v /opt/mysql/data:/var/lib/mysql \
-v /opt/mysql/logs:/var/log/mysql \
-v /opt/mysql/conf:/etc/mysql/mysql.conf.d \
-e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.20
1
2
3
4
5
2
3
4
5
参数解释:
- --name: 容器名称
- -p 3306: 映射端口
- MYSQL_ROOT_PASSWORD:mysql root账号初始密码
- -v冒号前面是宿主机目录,后面是容器目录
查看启动状态
# 五、设置远程登录
[root@node-11 ~]# docker exec -it mysql /bin/bash
root@cd0fb7617eb9:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 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> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
mysql> exit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
注意
如果是8.0以上版本,则使用以下方式:
# 查看释放有root 用户 正常情况启动的时候已经自动创建
mysql> SELECT user, host FROM mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| root | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
# 如果没有 root 或者想创建其他普通用户则执行创建用户命令【本文以下创建用户省略】
mysql> create user 'pub'@'%' identified by 'zzFynQJsmbrwrU&3';
# 授权root可以远程访问
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
mysql> exit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 六、测试远程登录
使用navicat输入ip地址,用户名密码测试连接即可
编辑 (opens new window)
上次更新: 2024/03/20, 16:20:57