mac无法访问mac上起的mysql容器

----------Mac OSX 同时被 2 个专栏收录
11 篇文章 0 订阅
5 篇文章 0 订阅

mac无法访问mac上起的mysql容器

前言

  • 背景
  • 问题描述
  • 问题解决过程
  • 解决方案
  • tips
  • 参考

背景

# 系统信息
sw_vers -productVersion;system_profiler SPSoftwareDataType
11.2.1
Software:

    System Software Overview:

      System Version: macOS 11.2.1 (20D74)
      Kernel Version: Darwin 20.3.0
      Boot Volume: MacOS
      Boot Mode: Normal
      Computer Name: Xiaojue’s MacBook Pro
      User Name: Xiaojue Guan (xiaojueguan)
      Secure Virtual Memory: Enabled
      System Integrity Protection: Enabled
      Time since boot: 13:28
# docker信息
docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
  scan: Docker Scan (Docker Inc., v0.5.0)

Server:
 Containers: 1
  Running: 1
  Paused: 0
  Stopped: 0
 Images: 1
 Server Version: 20.10.5
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 4.19.121-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 6
 Total Memory: 3.846GiB
 Name: docker-desktop
 ID: X32B:UL74:MRUW:QXUS:7U7U:GNGB:6DIV:ITTJ:LID2:KFMF:HGPW:O24Z
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: gateway.docker.internal:3128
 HTTPS Proxy: gateway.docker.internal:3129
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

问题描述

我尝试通过如下命令去尝试建立一个mysql容器用于开发xblog

 docker run --name mysql -e MYSQL_ROOT_PASSWORD=xblog -d mysql -p 3306:3306

然后尝试使用sequel pro去尝试连接,发现没办法连接上.

问题解决过程

查看容器是否正常,使用如下命令

docker ps

发现mysql的容器并没有起来

CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

查看容器日志:

docker logs mysql

发现

2021-04-03 14:18:25+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
2021-04-03 14:18:25+00:00 [ERROR] [Entrypoint]: mysqld failed while attempting to check config
	command was: mysqld -p 3306:3306 --verbose --help --log-bin-index=/tmp/tmp.JRWW2rxBwb
	Enter password: mysqld: Can not perform keyring migration : Invalid --keyring-migration-source option.
2021-04-03T14:18:25.189797Z 0 [ERROR] [MY-011084] [Server] Keyring migration failed.
2021-04-03T14:18:25.191650Z 0 [ERROR] [MY-010119] [Server] Aborting

看报错, 应该是docker在解析参数的时候出现了问题,把-p 3306:3306传到了容器的启动参数中,但是容器本身的启动参数中不需要这个参数.

刚开始对于这个猜测不是很确定,但是后面查看了docker网络docker网络的文档确信了我这个猜测.
By default, when you create or run a container using docker create or docker run, it does not publish any of its ports to the outside world. To make a port available to services outside of Docker, or to Docker containers which are not connected to the container’s network, use the --publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world. Here are some examples.
从文档上看docker是有将容器的端口暴露到docker所在的host这样的能力的.
发现个槽点, 大家对于上面的文档好像满意和不满意各占一半😂, 但是它帮忙确定了我的猜想,果断一个👍.如果你点了👎能说说你的原因吗😄
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UtSvZ95s-1617518099927)(:storage/7de1559b-d11d-449d-8faa-1c94492ddb25/55486592.png)]
尝试调整下尝试位置变成如下的样子

docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=xblog -d mysql

这一回容器起来了, 通过在本地host执行lsof -i :3306也可以发现3306端口被投射出来了.

lsof -i :3306
COMMAND    PID        USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
com.docke 2053 xiaojueguan   21u  IPv6 0xd72a31d915fbded3      0t0  TCP *:mysql (LISTEN)

此时使用sequel pro去尝试连接,但还是不行.产生如下的报错
Unable to connect to host 127.0.0.1, or the request timed out.

Be sure that the address is correct and that you have the necessary privileges, or try increasing the connection timeout (currently 10 seconds).

MySQL said: Authentication plugin ‘caching_sha2_password’ cannot be loaded: dlopen(/usr/local/lib/plugin/caching_sha2_password.so, 2): image not found

搜索了下发现这个答案Sequel Pro and MySQL connection failed - Stack Overflow好像这个报错的信息是mysql使用了sequel不支持的认证方式.由于这个问题中的mysql是跑在本地host上的服务,而不是像我的那样跑在容器里面的,所以这个问题的答案并不能解决我的问题.此外我想了解下我容器里面的mysql是不是真的使用了caching_sha2_password这个方法.
通过搜索我发现mysql的官方文档MySQL :: MySQL 8.0 Reference Manual :: 6.4.1.2 Caching SHA-2 Pluggable Authentication说mysql8.0中
caching_sha2_password是默认使用的认证方法,跑到我的容器中执行如下命令

 mysql -V

发现我的mysql是8.0的版本.
现在的问题便是sequel pro尝试去连接容器里面的mysql服务,但是mysql服务默认开启了Caching SHA-2 Pluggable Authentication,但是sequel pro不支持这个功能.
所以解决这个方法有两个:

  1. 修改mysql server
    通过如下方式:
    • 使用如下yml文件通过docker stack deploy or docker-compose重新创建mysql容器
# Use root/example as user/password credentials
version: '3.1'

services:

db:
  image: mysql
  command: --default-authentication-plugin=mysql_native_password
  restart: always
  environment:
    MYSQL_ROOT_PASSWORD: xblog

adminer:
  image: adminer
  restart: always
  ports:
    - 8080:8080
  • 通过增加参数–default-authentication-plugin重新起一个容器
docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=xblog -d mysql --default-authentication-plugin=mysql_native_password
  1. 使sequel pro具备这个Caching SHA-2的能力
    根据Docs当前sequel pro还没有对于mysql大于5的版本有很好的支持.虽然社区有issue跟踪这个问题但是目前好像还没有解决方法.

解决方案

总结:
所以解决这个问题有下面几个方法

  1. 使用mysql5及以下的image创建容器
  2. 通过改变mysqld的启动参数改变默认的认证方法
  3. 修改mysql server
    通过如下方式:
    • 使用如下yml文件通过docker stack deploy or docker-compose重新创建mysql容器
# Use root/example as user/password credentials
version: '3.1'

services:

db:
  image: mysql
  command: --default-authentication-plugin=mysql_native_password
  restart: always
  environment:
    MYSQL_ROOT_PASSWORD: xblog

adminer:
  image: adminer
  restart: always
  ports:
    - 8080:8080
  • 通过增加参数–default-authentication-plugin重新起一个容器
docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=xblog -d mysql --default-authentication-plugin=mysql_native_password
  1. 等待Cannot connect to MySQL 8.0.4 (caching_sha2_password plugin is missing) · Issue #3037 · sequelpro/sequelpro · GitHub这个issue被解决.但是目前sequelpro社区好像不会再有update了根据Future of Sequel Pro (06/2020) · Issue #3705 · sequelpro/sequelpro · GitHub
  2. 使用GitHub - Sequel-Ace/Sequel-Ace: MySQL/MariaDB database management for macOS

tips

参考

Cannot connect to MySQL 8.0.4 (caching_sha2_password plugin is missing) · Issue #3037 · sequelpro/sequelpro · GitHub
Docs
GitHub - sequelpro/sequelpro: MySQL/MariaDB database management for macOS
Docker Hub
MySQL :: MySQL 5.7 Reference Manual :: 6.4.1.6 Client-Side Cleartext Pluggable Authentication
MySQL :: MySQL 5.7 Reference Manual :: 4.9 Environment Variables
Container networking | Docker Documentation
Sequel Pro and MySQL connection failed - Stack Overflow
GitHub - Sequel-Ace/Sequel-Ace: MySQL/MariaDB database management for macOS
Future of Sequel Pro (06/2020) · Issue #3705 · sequelpro/sequelpro · GitHub

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值