无法从本地连接到 mysql docker

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49019652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 22:52:39  来源:igfitidea点击:

not able to connect to mysql docker from local

mysqldocker

提问by Gaurang Shah

I am trying to connect to mysql database from docker image. However it's throwing errors.

我正在尝试从 docker 映像连接到 mysql 数据库。但是它抛出错误。

following is the docker image I am using. https://hub.docker.com/_/mysql/

以下是我正在使用的 docker 图像。 https://hub.docker.com/_/mysql/

And following is the command I have used to run the docker image.

以下是我用来运行 docker 镜像的命令。

docker run -p 3306:3306 --name mysql_80 -e MYSQL_ROOT_PASSWORD=password -d mysql:8

Following is the output of docker pscommand

以下是docker ps命令的输出

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                                                              NAMES
9f35d2e39476        mysql:8             "docker-entrypoint.s…"   5 minutes ago       Up 5 minutes        0.0.0.0:3306->3306/tcp

if I check the IP using docker inspect and ping that IP, it shows IP is not reachable.

如果我使用 docker inspect 检查 IP 并 ping 该 IP,则显示 IP 无法访问。

docker inspect 9f35d2e39476 | grep -i ipaddress

And if i try to connect using localhostand 127.0.0.1I am getting following error.

如果我尝试使用连接进行连接,localhost并且出现127.0.0.1以下错误。

Unable to load authentication plugin 'caching_sha2_password'.

无法加载身份验证插件“caching_sha2_password”。

回答by Ivan Beldad

First of all, be aware that you're using non stable software, so there can be major changes between releases and unexpected behaviour.

首先,请注意您使用的是不稳定的软件,因此版本和意外行为之间可能会发生重大变化。

Edit: Is not in development anymore, stable release launched April 19, 2018

编辑:不再开发,稳定版本于 2018 年 4 月 19 日发布

Secondly, you cannot ping directly your container, it's in other net, but you can easily use another container to ping him.

其次,你不能直接ping你的容器,它在其他网络中,但是你可以很容易地使用另一个容器来ping他。

mysql 8 uses caching_sha2_passwordas the default authentication plugin instead of mysql_native_password. More info here.

mysql 8caching_sha2_password用作默认身份验证插件而不是mysql_native_password. 更多信息在这里

Many mysql drivers haven't added support for caching_sha2_passwordyet.

许多 mysql 驱动程序尚未添加支持caching_sha2_password

If you're having problems with it, you can change to the old authentication plugin with something like this:

如果您遇到问题,可以使用以下内容更改为旧的身份验证插件:

docker run -p 3306:3306 --name mysql_80 -e MYSQL_ROOT_PASSWORD=password -d mysql:8 mysqld --default-authentication-plugin=mysql_native_password

docker run -p 3306:3306 --name mysql_80 -e MYSQL_ROOT_PASSWORD=password -d mysql:8 mysqld --default-authentication-plugin=mysql_native_password

回答by axelferreira

I had the same problem, but this didn't do it for me with a Docker container running mysql 8.X. I loged in the container

我遇到了同样的问题,但是对于运行 mysql 8.X 的 Docker 容器,这对我不起作用。我登录了容器

docker exec -it CONTAINER_ID bash

then log into mysql as root

然后以root身份登录mysql

mysql --user=root --password

Enter the password for root (Default is 'root') Finally Run:

输入 root 的密码(默认为“root”) 最后运行:

ALTER USER 'username' IDENTIFIED WITH mysql_native_password BY 'password';

You're all set.

你都准备好了。

This has already been answered here: post

这已经在这里得到了回答:post

回答by psychemedia

I found a fix herewhen firing up from docker-compose:

从 docker-compose 启动时,我在这里找到了一个修复:

services:

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

That is, revert from the new MySQL password/auth mechanism when starting MySQL.

也就是说,在启动 MySQL 时从新的 MySQL 密码/身份验证机制恢复。

回答by Rohan

In case you are trying to connect to MySQL using the terminal itself, you might have a buggy build. But if you are trying to connect to MySQL using a GUI client, for example, Sequel Pro, it might not support the new auth feature with MySQL 8.

如果您尝试使用终端本身连接到 MySQL,则您的构建可能有问题。但是,如果您尝试使用 GUI 客户端(例如 Sequel Pro)连接到 MySQL,它可能不支持 MySQL 8 的新身份验证功能。

As a workaround for this, you start your docker container with --default-authentication-plugin=mysql_native_passwordcommand at the end and it will default the MySQL to use the old authentication:

作为一种解决方法,您--default-authentication-plugin=mysql_native_password在末尾使用命令启动 docker 容器,它将默认 MySQL 使用旧的身份验证:

docker run -p 3306:3306 --name mysql_80 -e MYSQL_ROOT_PASSWORD=password -d mysql:8 --default-authentication-plugin=mysql_native_password

docker run -p 3306:3306 --name mysql_80 -e MYSQL_ROOT_PASSWORD=password -d mysql:8 --default-authentication-plugin=mysql_native_password

回答by MatejC

Answer from psychemedia almost worked for me (fresh install on win 10). Also one very important thing I had to do, because this was not my first try. If you have attached data folder, in my example mysql volume before, empty/delete content of this folder. My working docker-compose file:

来自 psychemedia 的回答几乎对我有用(在 win 10 上全新安装)。还有一件非常重要的事情我必须做,因为这不是我第一次尝试。如果您附加了数据文件夹,在我之前的示例 mysql 卷中,清空/删除此文件夹的内容。我的工作 docker-compose 文件:

version: '3' services:
    mysql-development:
    image: mysql:8.0.19
    container_name: mysql
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: XXX
      MYSQL_DATABASE: YYY
      MYSQL_USER: ZZZ
      MYSQL_PASSWORD: WWW
    ports:
      - "3306:3306"
      - "33060:33060"
    working_dir: /var/lib/mysql
    volumes:
      - "./mysql:/var/lib/mysql:rw"
  • Update environment variables in docker-compose file.
  • Make folder for mysql data (volumes): mkdir mysql
  • Check syntax: docker-compose.exe config
  • Build it: docker-compose.exe build
  • Run it: docker-compose.exe up
  • 更新 docker-compose 文件中的环境变量。
  • 为 mysql 数据(卷)创建文件夹:mkdir mysql
  • 检查语法:docker-compose.exe config
  • 构建它:docker-compose.exe 构建
  • 运行它:docker-compose.exe up

回答by Chris Clark

This is probably considered solved, but I wanted to document what it took for me to overcome this. I was having a similar problem, and reverting back to the old password standard was not a solution. I needed to use the caching_sha2_password, so none of the above worked for me and I had to use this command:

这可能被认为已解决,但我想记录我克服这个问题所花费的时间。我遇到了类似的问题,恢复到旧密码标准不是解决方案。我需要使用 caching_sha2_password,所以以上都不适合我,我不得不使用这个命令:

docker run -it --rm mysql mysql -h 172.31.116.20 -p -P6603

docker run -it --rm mysql mysql -h 172.31.116.20 -p -P6603

Where the 172.31.116.20 is my local IP address where the container is running and -P6603 is the port it's running on.

其中 172.31.116.20 是我运行容器的本地 IP 地址,-P6603 是它运行的端口。

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
5092251b3dbf        mysql               "docker-entrypoint..."   16 minutes ago      Up 16 minutes       33060/tcp, 0.0.0.0:6603->3306/tcp   test1-mysql

I found this solution on the Docker site for the MySQL container: https://hub.docker.com/_/mysql/

我在 MySQL 容器的 Docker 站点上找到了这个解决方案:https: //hub.docker.com/_/mysql/

It's in the "Connect to MySQL from the MySQL command line client" section.

它位于“从 MySQL 命令行客户端连接到 MySQL”部分。

回答by Casey McMullen

If you want to use MySQL 8.0 and not get the "caching_sha2_password plugin" error, then check out a couple blog posts I wrote on how to setup MySQL 8.0 in Docker with persistent data, as well as a post on how to run your MySQL 8.0 container with mysql_native_password.

如果您想使用 MySQL 8.0 并且没有收到“caching_sha2_password 插件”错误,请查看我写的关于如何在 Docker 中使用持久数据设置 MySQL 8.0 的几篇博客文章,以及有关如何运行 MySQL 8.0 的文章带有 mysql_native_password 的容器。

In short, you can create a local "my.cnf" config file:

简而言之,您可以创建一个本地“my.cnf”配置文件:

$ sudo nano /usr/local/opt/mysql/config/my.cnf`

Add the necessary config statement to it:

向其中添加必要的配置语句:

[mysqld]
default-authentication-plugin=mysql_native_password

And then include that file as a volume bind in your "docker run" statement:

然后将该文件作为卷绑定包含在“docker run”语句中:

$ docker run --restart always --name mysql8.0 - 
v/usr/local/opt/mysql/8.0:/var/lib/mysql -v 
/usr/local/opt/mysql/config:/etc/mysql/conf.d -p 3306:3306 -d -e 
MYSQL_ROOT_PASSWORD=your_password mysql:8.0

You can read more detail on these steps here:

您可以在此处阅读有关这些步骤的更多详细信息:

https://medium.com/@crmcmullen/how-to-run-mysql-in-a-docker-container-on-macos-with-persistent-local-data-58b89aec496a

https://medium.com/@crmcmullen/how-to-run-mysql-in-a-docker-container-on-macos-with-persistent-local-data-58b89aec496a

https://medium.com/@crmcmullen/how-to-run-mysql-8-0-with-native-password-authentication-502de5bac661

https://medium.com/@crmcmullen/how-to-run-mysql-8-0-with-native-password-authentication-502de5bac661