docker快速部署mysql

docker部署mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@docker101 ~]# docker container run -d -p 3306:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -e  MYSQL_DATABASE=haha -e MYSQL_USER=ha -e MYSQL_PASSWORD=123456 --name mysql-server mysql:8.0.36-oracle
Unable to find image 'mysql:8.0.36-oracle' locally
8.0.36-oracle: Pulling from library/mysql
bd37f6d99203: Pull complete
d2433cba0951: Pull complete
13702d9fe3c3: Pull complete
83bcc87284a1: Pull complete
c38d8660e1fa: Pull complete
7e1bc321f421: Pull complete
bddd54b9c549: Pull complete
4eaae1e844ac: Pull complete
5196e1e87d8f: Pull complete
6586d096303c: Pull complete
cf55ff1c80af: Pull complete
Digest: sha256:a532724022429812ec797c285c1b540a644c15e248579c6bfdf12a8fbaab4964
Status: Downloaded newer image for mysql:8.0.36-oracle
6f83e282c657e0cf2fffd85dee76dd7fe5a02797372dff18f348da01f1aa33f8

#解析
docker container run -d \ #创建并启动
-p 3306:3306 \ #端口映射
-e MYSQL_ALLOW_EMPTY_PASSWORD=yes \ #设置环境变量,允许 root 用户使用空密码登录/设置仅适用于开发/测试环境
-e MYSQL_DATABASE=haha \ #首次启动创建一个名为haha的库
-e MYSQL_USER=ha \ #创建一个非root用户
-e MYSQL_PASSWORD=123456 \ #设置密码
--name mysql-server \ #给容器指定一个易读的名字
mysql:8.0.36-oracle #像来源:Docker Hub 上的官方 MySQL 镜像
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
执行测试
[root@docker101 ~]# docker exec -it mysql-server mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.36 MySQL Community Server - GPL

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

mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| haha |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)

mysql> SELECT USER,host FROM mysql.user;
+------------------+-----------+
| USER | host |
+------------------+-----------+
| ha | % |
| root | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
6 rows in set (0.01 sec)

mysql> SHOW GRANTS FOR ha;
+----------------------------------------------+
| Grants for ha@% |
+----------------------------------------------+
| GRANT USAGE ON *.* TO `ha`@`%` |
| GRANT ALL PRIVILEGES ON `haha`.* TO `ha`@`%` |
+----------------------------------------------+
2 rows in set (0.00 sec)

mysql>


docker快速部署mysql
http://example.com/2025/02/04/docker部署mysql/
作者
发布于
2025年2月4日
许可协议