请开启 JavaScript
Docker 快速安装使用 MySQL9.4 – 老迟笔记

Docker 快速安装使用 MySQL9.4

1、创建宿主机目录
cd ~/Desktop/code/docker

该目录专门用于宿主机 macbook 与 所有docker 容器的-v目录映射

mkdir -p mysqldata/{data,logs,conf,pems,conf.d}

这些目录后看创建容器时,会通过-v 参数指定宿主机与容器内的映射

2、自定义 my.cnf 文件
cd ~/Desktop/code/docker/mysqldata/conf
vi my.cnf
[mysqld]
server-id = 1
log-bin = mysql-bin
3、拉取官方 MySQL9.4镜像
docker pull mysql:9.4
4、创建容器
docker run --name mysql9.4 \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 3306:3306 \
-v ~/Desktop/code/docker/mysqldata/data:/var/lib/mysql \
-v ~/Desktop/code/docker/mysqldata/logs:/var/log/mysql \
-v ~/Desktop/code/docker/mysqldata/conf:/etc/mysql \
-v ~/Desktop/code/docker/mysqldata/conf.d:/etc/mysql/conf.d \
-v ~/Desktop/code/docker/mysqldata/pems:/etc/mysql/pems \
--restart unless-stopped \
-d mysql:9.4
5、进入容器
docker exec -it mysql9.4 bash
6、测试连接
bash-5.1# 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 9
Server version: 9.4.0 MySQL Community Server - GPL

Copyright (c) 2000, 2025, 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.003 sec)

mysql> 
7、宿主机navicat 连接 MySQL

ok,没问题,不用额外设置 root 表里 Host 字段就可以连接成功

注:宿主机如果修改配置文件,请直接通过重启容器进行加载