docker_安装mysql

569 阅读4分钟

m1芯片mac使用docker安装mysql最新版本(8.4.0),并使用Sequel Pro进行连接

环境说明

  • 机器:MacBook Pro 2021款 Apple M1 Pro芯片
  • 系统: macOS sonoma 14.3

mac安装

1. 拉取镜像

docker pull mysql:latest

2. 运行容器

docker run \
--name mysql8 \
--privileged=true \
-d \
-p 3306:3306 \
--restart unless-stopped \
-v ~/mydata/mysql/log:/var/log/mysql \
-v ~/mydata/mysql/data:/var/lib/mysql \
-v ~/mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
mysql

可能存在问题

2022-09-10 03:11:40+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.30-1.el8 started.
2022-09-10 03:11:40+00:00 [ERROR] [Entrypoint]: mysqld failed while attempting to check config
	command was: mysqld --verbose --help --log-bin-index=/tmp/tmp.CmvRUgG1S9
	mysqld: Can't read dir of '/etc/mysql/conf.d/' (OS errno 2 - No such file or directory)
mysqld: [ERROR] Stopped processing the 'includedir' directive in file /etc/my.cnf at line 36.
mysqld: [ERROR] Fatal error in defaults handling. Program aborted!
2022-09-10 03:11:41+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.30-1.el8 started.
2022-09-10 03:11:41+00:00 [ERROR] [Entrypoint]: mysqld failed while attempting to check config
	command was: mysqld --verbose --help --log-bin-index=/tmp/tmp.LK43tIRMRc
	mysqld: Can't read dir of '/etc/mysql/conf.d/' (OS errno 2 - No such file or directory)
mysqld: [ERROR] Stopped processing the 'includedir' directive in file /etc/my.cnf at line 36.
mysqld: [ERROR] Fatal error in defaults handling. Program aborted!
2022-09-10 03:11:41+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.30-1.el8 started.
2022-09-10 03:11:41+00:00 [ERROR] [Entrypoint]: mysqld failed while attempting to check config
	command was: mysqld --verbose --help --log-bin-index=/tmp/tmp.NHeIZiSjiL
	mysqld: Can't read dir of '/etc/mysql/conf.d/' (OS errno 2 - No such file or directory)
mysqld: [ERROR] Stopped processing the 'includedir' directive in file /etc/my.cnf at line 36.
mysqld: [ERROR] Fatal error in defaults handling. Program aborted!
2022-09-10 03:11:42+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.30-1.el8 started.
2022-09-10 03:11:42+00:00 [ERROR] [Entrypoint]: mysqld failed while attempting to check config
	command was: mysqld --verbose --help --log-bin-index=/tmp/tmp.pq5ARb8p8b
	mysqld: Can't read dir of '/etc/mysql/conf.d/' (OS errno 2 - No such file or directory)
mysqld: [ERROR] Stopped processing the 'includedir' directive in file /etc/my.cnf at line 36.
mysqld: [ERROR] Fatal error in defaults handling. Program aborted!
2022-09-10 03:11:43+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.30-1.el8 started.
2022-09-10 03:11:43+00:00 [ERROR] [Entrypoint]: mysqld failed while attempting to check config
	command was: mysqld --verbose --help --log-bin-index=/tmp/tmp.7Y0xVM3e49
	mysqld: Can't read dir of '/etc/mysql/conf.d/' (OS errno 2 - No such file or directory)
mysqld: [ERROR] Stopped processing the 'includedir' directive in file /etc/my.cnf at line 36.
mysqld: [ERROR] Fatal error in defaults handling. Program aborted!
2022-09-10 03:11:45+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.30-1.el8 started.
2022-09-10 03:11:45+00:00 [ERROR] [Entrypoint]: mysqld failed while attempting to check config
	command was: mysqld --verbose --help --log-bin-index=/tmp/tmp.hImGI13dX6
	mysqld: Can't read dir of '/etc/mysql/conf.d/' (OS errno 2 - No such file or directory)
mysqld: [ERROR] Stopped processing the 'includedir' directive in file /etc/my.cnf at line 36.
mysqld: [ERROR] Fatal error in defaults handling. Program aborted!
2022-09-10 03:11:48+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.30-1.el8 started.
2022-09-10 03:11:48+00:00 [ERROR] [Entrypoint]: mysqld failed while attempting to check config
	command was: mysqld --verbose --help --log-bin-index=/tmp/tmp.X9rLA4bltB
	mysqld: Can't read dir of '/etc/mysql/conf.d/' (OS errno 2 - No such file or directory)
mysqld: [ERROR] Stopped processing the 'includedir' directive in file /etc/my.cnf at line 36.
mysqld: [ERROR] Fatal error in defaults handling. Program aborted!

mysqld: Can't read dir of '/etc/mysql/conf.d/' (OS errno 2 - No such file or directory)报错。

  • 原因: mysql容器的/etc/mysql目录挂载到宿主机的 /mydata/mysql/conf目录,由于宿主机的 /mydata/mysql/conf是空的,所以找不到/etc/mysql/conf.d中conf.d这个目录,导致容器创建失败。
  • 解决办法: 手动创建改目录
mkdir ~/mydata/mysql/conf/conf.d

3. 进入终端

docker exec -it mysql8 bin/bash

4. 复制my.cnf文件到挂载的conf目录中

mysql高版本,my.cnf放置在/etc/my.cnf中。将其从容器中复制到挂载目录中

docker cp mysql8:/etc/my.cnf ~/mydata/mysql/conf/conf.d

5. 通过数据库管理工具连接mysql

Sequel Pro这个工具为例

image.png

  • 点击Test connection 进行测试连接
  • 点击Add to Favorites 加入收藏

可能存在报错

image.png

原因

MySQL 8.4默认使用caching_sha2_password差劲进行身份验证。Sequel Pro暂不支持这种验证方式。需要对验证方式进行修改。修改为mysql_native_password

MySQL 8.4(截至 2024 年的最新 LTS 版本)中还让引入的一个主要变化是,默认情况下不再启用 “MySQL Native Password” 插件。需要对mysql_native_password进行开启。

解决办法

1.修改文件my.cnf,开启mysql_native_password插件

vim ~/mydata/mysql/conf/conf.d/my.cnf

添加以下内容到 mysqld配置下

[mysqld]
....其他配置


# Enable mysql_native_password plugin
mysql_native_password=ON
  • 启动mysql_native_password插件

重启mysql

docker restart mysql8

2.更改用户身份验证方式为mysql_native_password

进入mysql容器

docker exec -it mysql8 /bin/bash

登陆mysql

mysql -u root -p

修改用户身份验证权限

alter user 'root'@'%' identified with mysql_native_password by '{password}';
  • {password}占位符中,填写你的密码

命令格式

alter user '{userName}'@'{host}' identified with mysql_native_password by '{password}';

3.使用Sequel Pro重新进行连接