【Docker】 安装 mysql8.0

目录

前言

  • CentOS 7.9
  • Docker Version: 1.13.1

准备

  • docker 已安装。

  • 选择合适的mysql镜像(mysql:5.7.31)。

拉取镜像

bash 复制代码
shell> docker pull mysql:8.0.34
Trying to pull repository docker.io/library/mysql ... 
5.7.31: Pulling from docker.io/library/mysql
bb79b6b2107f: Pull complete 
49e22f6fb9f7: Pull complete 
842b1255668c: Pull complete 
9f48d1f43000: Pull complete 
c693f0615bce: Pull complete 
8a621b9dbed2: Pull complete 
0807d32aef13: Pull complete 
6d2fc69dfa35: Pull complete 
56153548dd2c: Pull complete 
3bb6ba940303: Pull complete 
3e1888da91a7: Pull complete 
Digest: sha256:b3dc8d10307ab7b9ca1a7981b1601a67e176408be618fc4216d137be37dae10b
Status: Downloaded newer image for docker.io/mysql:5.7.31

查看已拉取的镜像

bash 复制代码
shell> docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world   latest              feb5d9fea6a5        6 weeks ago         13.3 kB
docker.io/mysql         5.7.31              42cdba9f1b08        12 months ago       448 MB

创建容器

创建容器方式1:快速创建容器

bash 复制代码
shell> docker create -p 3306:3306 \
-e MYSQL_ROOT_PASSWORD='my-secret-pw' \
-e LOWER_CASE_TABLE_NAMES=1 \
--name mysql1 mysql:8.0.34 \
--lower-case-table-names=1 \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_unicode_ci

创建容器方式2:创建容器+配置文件+数据目录

bash 复制代码
shell> docker create \
-v /data/mysql1/data:/var/lib/mysql \
-v /data/mysql1/conf.d:/etc/mysql/conf.d  \
-e MYSQL_ROOT_PASSWORD='my-secret-pw' \
-p 3306:3306 \
--name mysql1 mysql:8.0.34
  • --restart=always: 自动运行
  • -v /data/mysql1/data:/var/lib/mysql: 将宿主机的/data/mysql1/data挂载成容器的/var/lib/mysql
  • -v /data/mysql1/conf.d:/etc/mysql/conf.d: 将宿主机的/data/mysql1/conf.d挂载成容器的/etc/mysql/conf.d

my.cnf

使用"创建容器方式2"时,需要在/data/mysql1/conf.d目录下创建my.cnf文件,内容如下:

复制代码
[mysqld]
# 默认密码插件,兼容旧版本的客户端
default_authentication_plugin=mysql_native_password
# authentication_policy=mysql_native_password,caching_sha2_password

# 字符集配置
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'

# 表名不区分大小写 (0:区分, 1:不区分, 2:按文件系统)
lower_case_table_names=1

max_connections=1000
innodb_buffer_pool_size=256M

sql_mode=

[client]
default-character-set=utf8mb4
# default-auth=mysql_native_password

[mysql]
default-character-set=utf8mb4

[mysqladmin]
default-character-set=utf8mb4
  • lower_case_table_names (表名不区分大小写)设置,在 MySQL 8.0+ 中,必须在初次初始化时设置,如果已经有数据了再修改就会报错。因此,在首次启动容器前,必须准备好my.cnf文件。

运行容器

bash 复制代码
shell> docker start mysql1

命令行登录mysql

bash 复制代码
shell> docker exec -it mysql1 bash
root@a6f9e4a700a3:/# mysql -uroot -pmy-secret-pw
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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> exit
Bye

或者:

bash 复制代码
shell> docker exec -it mysql1 mysql -uroot -pmy-secret-pw
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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> exit
Bye

容器设置

容器随 docker 自动启动

设置容器的重启策略

bash 复制代码
shell> docker update --restart=always mysql1
  • 每次docker启动时,容器也会自动启动

容器设置IP

向网络中添加容器

bash 复制代码
shell> docker network connect --ip 172.19.0.2  mynetwork mysql1
  • docket ip : 172.19.0.2

mysql设置

修改密码

bash 复制代码
mysql> set password='123456';

设置mysql数据编码格式为utf8mb4

bash 复制代码
shell> vim /data/mysql1/conf.d/my.cnf
# 打开文件(没有该文件时,创建它)后,添加下面的配置。注意对应节点
[client]
default-character-set = utf8mb4

[mysql]
default-character-set = utf8mb4

[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect = 'SET NAMES utf8mb4'

重启mysql并查看设置

bash 复制代码
shell> docker exec -it mysql1 mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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 variables WHERE variable_name like 'character\_set\_%' OR variable_name like 'collation%';
+--------------------------+--------------------+
| Variable_name            | Value              |
+--------------------------+--------------------+
| character_set_client     | utf8mb4            |
| character_set_connection | utf8mb4            |
| character_set_database   | utf8mb4            |
| character_set_filesystem | binary             |
| character_set_results    | utf8mb4            |
| character_set_server     | utf8mb4            |
| character_set_system     | utf8               |
| collation_connection     | utf8mb4_unicode_ci |
| collation_database       | utf8mb4_unicode_ci |
| collation_server         | utf8mb4_unicode_ci |
+--------------------------+--------------------+
10 rows in set (0.01 sec)

mysql> exit
Bye

查看mysql表名不区分大小写

bash 复制代码
shell> docker exec -it mysql1 mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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 variables like '%table_names';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| lower_case_table_names | 1     |
+------------------------+-------+
1 row in set (0.00 sec)

mysql> exit
Bye

设置 mysql 时区

时区设置成东八区

bash 复制代码
shell> vim /data/mysql1/conf.d/my.cnf
-----------------------------
# 打开文件后,添加下面的配置。注意对应节点
[mysqld]
default-time_zone = '+8:00'

重启mysql并查看设置

bash 复制代码
shell> docker exec -it mysql1 mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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 variables like '%time_zone%';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | UTC    |
| time_zone        | +08:00 |
+------------------+--------+
2 rows in set (0.00 sec)

mysql> exit
Bye

远程访问授权

参考这里

参考

https://hub.docker.com/_/mysql

相关推荐
.Cnn1 分钟前
MySQL事务和Spring事务
数据库·后端·mysql·spring
comedate6 分钟前
[WSL2] 解决 WSL2 中 Docker 部署的 SearXNG 重启后,localhost 不能用的问题
docker·wsl2·searxng
ai产品老杨19 分钟前
突破异构算力与多协议壁垒:基于 Docker+边缘计算的企业级 AI 视频管理平台架构解析
人工智能·docker·边缘计算
DIY源码阁10 小时前
JavaSwing学生成绩管理系统 - MySQL版
java·数据库·mysql·eclipse
“码”力全开13 小时前
打破芯片与协议壁垒:基于 Docker + 边缘计算的 GB28181/RTSP 视频智能管理平台架构设计与源码交付方案
docker·音视频·边缘计算
不总是13 小时前
[2026最新] Windows 免安装版 MySQL 8 详细安装配置教程(ZIP 压缩包版)
数据库·windows·mysql
徒手猫14 小时前
MySQL 窗口函数完全指南
数据库·mysql
betazhou14 小时前
电科金仓数据库V9 MySQL兼容版本搭建一主一从体验
数据库·mysql·oracle·主从·高可用·kingbase·v9 mysql兼容版本
元宝骑士14 小时前
MySQL 8.0 递归 CTE:树形结构一键生成层级 Path 并更新回表
后端·mysql
wbs_scy16 小时前
MySQL 多表连接查询实战:内连接 + 外连接
数据库·mysql