MySQL 容器形式搭建
# 容器形式安装 MySQL5.7
# 创建工作目录
mkdir -p /opt/mysql57/{mydir,data,conf}
1
# 编写 docker-compose.yaml
version: '3'
services:
mysql:
image: mysql:5.7
# NOTE: use of "mysql_native_password" is not recommended: https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password
# (this is just an example, not intended to be a production configuration)
command: --default-authentication-plugin=mysql_native_password
restart: always
container_name: mysql57
ports:
- 3306:3306 # 宿主机 port:容器 port
volumes:
- /opt/mysql57/data:/var/lib/mysql
- /opt/mysql57/conf/my.cnf.d:/etc/my.cnf.d/
- /opt/mysql57/conf/my.cnf:/etc/my.cnf
environment:
MYSQL_ROOT_PASSWORD: youpassword
TZ: Asia/Shanghai
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
# 设置配置文件
/opt/mysql/conf/my.cnf
[mysqld]
user=mysql
default-storage-engine=INNODB
character-set-server=utf8
character-set-client-handshake=FALSE
collation-server=utf8_unicode_ci
init_connect='SET NAMES utf8'
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 启动 MySQL
# docker compose up -d
[+] Running 1/1
⠿ Container mysql57 Started
# docker compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
mysql57 mysql:5.7 "docker-entrypoint.s…" mysql 5 minutes ago Up 5 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp
1
2
3
4
5
6
2
3
4
5
6
# 检查 MySQL
进入 MySQL 容器:
# docker exec -it mysql57 bash
1
登录 MySQL:
# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.41 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, 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 |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 参考
https://hub.docker.com/_/mysql/tags
评论
上次更新: 2024/05/29, 14:25:22