php Docker MYSQL '[2002] 连接被拒绝'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40561433/
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-26 02:11:46  来源:igfitidea点击:

Docker MYSQL '[2002] Connection refused'

phpmysqlsymfonydocker

提问by Frank Millc

I was trying out Docker for the first time. Got a LEMP stack up and running, but I can't connect to the MYSQL Database. Not on my Symfony application, not on PHPMyAdmin. The applications are returning the following error code:

我是第一次尝试 Docker。启动并运行了 LEMP 堆栈,但我无法连接到 MYSQL 数据库。不在我的 Symfony 应用程序上,不在 PHPMyAdmin 上。应用程序返回以下错误代码:

An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused

驱动程序中发生异常:SQLSTATE[HY000] [2002] 连接被拒绝

This is my docker-compose.yml:

这是我的 docker-compose.yml:

nginx:
    image: tutum/nginx
    ports:
        - "80:80"
    links:
        - phpfpm
    volumes:
        - ./nginx/default:/etc/nginx/sites-available/default
        - ./nginx/default:/etc/nginx/sites-enabled/default

        - ./logs/nginx-error.log:/var/log/nginx/error.log
        - ./logs/nginx-access.log:/var/log/nginx/access.log
phpfpm:
    build: phpfpm/
    ports:
        - "9000:9000"
    volumes:
        - ./public:/usr/share/nginx/html
mysql:
  image: mariadb
  ports:
    - 3306:3306
  environment:
    MYSQL_ROOT_PASSWORD: admin
phpmyadmin:
  image: phpmyadmin/phpmyadmin
  restart: always
  links:
    - mysql
  ports:
    - 8183:80
  environment:
    MYSQL_USERNAME: admin
    MYSQL_ROOT_PASSWORD: admin
    PMA_ARBITRARY: 1

Dockerfile PHPFPM:

Dockerfile PHPFPM:

    FROM php:fpm

RUN docker-php-ext-enable opcache
RUN apt-get update \
  && apt-get install -y --no-install-recommends libpq-dev \
  && docker-php-ext-install mysqli pdo_pgsql pdo_mysql

GitHub URL: https://github.com/MolengraafFrank/DockerSymfony

GitHub 网址:https: //github.com/MolengraafFrank/DockerSymfony

Could someone help me out? Thank you for your time.

有人可以帮我吗?感谢您的时间。

回答by JL M

The '[2002] Connection refused' means you can reach the database server, but you don't have right access for the user (in your case admin). By default mariadb have a root user with the password given by MYSQL_ROOT_PASSWORD and this user can connect from any server (%).

“[2002] 连接被拒绝”意味着您可以访问数据库服务器,但您没有用户的正确访问权限(在您的情况下为管理员)。默认情况下,mariadb 有一个 root 用户,其密码由 MYSQL_ROOT_PASSWORD 给出,该用户可以从任何服务器 (%) 进行连接。

If you want use an over login to your databases, you have to create it in the databases server with the right granting on databases from chosen locations.

如果您想对您的数据库使用过度登录,您必须在数据库服务器中创建它,并从所选位置对数据库进行正确授权。

The problem here is that you have named your database server as 'mysql' (service name in the docker-compose file). But by default phpmyadmin tries to connect to a database server named 'db'. Adding PMA_HOST: mysqlunder the environment section of the phpmyadmin service will resolve this problem.

这里的问题是您将数据库服务器命名为“mysql”(docker-compose 文件中的服务名称)。但默认情况下,phpmyadmin 会尝试连接到名为“db”的数据库服务器。PMA_HOST: mysql在 phpmyadmin 服务的 environment 部分下添加将解决此问题。



我认为 MYSQL_USERNAME 和 PMA_ARBITRARY 如果您使用默认配置(与 root 连接到您的数据库服务器)是无用的

回答by Emeka Mbah

I had this challenge because I am running 3 different containers with different IP Addresses

我遇到了这个挑战,因为我正在运行 3 个具有不同 IP 地址的不同容器

db - 172.18.0.3 - container for MYSQL
app - 172.18.0.2 - container for Laravel app

so I fixed it by editing my Laravel .env file

所以我通过编辑我的 Laravel .env 文件来修复它

DB_CONNECTION=mysql
DB_HOST=172.18.0.3
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=username
...

To get your containers Ip addresses. From your docker host

获取你的容器IP地址。从您的 docker 主机

docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

回答by Yaron Idan

I've managed to connect to the mysql instance using mysql command line tool, this is the command I used - mysql -u root -p -h 127.0.0.1, and the entering the admin password. Is that a sufficient solution for you?

我已经设法使用 mysql 命令行工具连接到 mysql 实例,这是我使用的命令 - mysql -u root -p -h 127.0.0.1,并输入管理员密码。这对你来说是一个足够的解决方案吗?

回答by joseph

In my case I was running mysqlin a docker container whose port was mapped to the host mac (3306:3306). I tried connecting to this database from a phpmyadmindocker container using 127.0.0.1 . But it won't work because the localhoston the phpmyadmindocker container does not have the required mysqlrunning.

就我而言,我mysql在一个 docker 容器中运行,其端口映射到主机 mac ( 3306:3306)。我尝试phpmyadmin使用 127.0.0.1从docker 容器连接到这个数据库。但它不会工作,因为localhost在上phpmyadmin泊坞窗容器不具有所需的mysql运行。

To connect to the host from docker network

从 docker 网络连接到主机

docker.for.mac.host.internal

Docker NetworkingDocker Compose Networking

Docker 网络Docker Compose 网络

回答by edlerd

you need to link the phpfpm container to mysql.

您需要将 phpfpm 容器链接到 mysql。