简体   繁体   English

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

[英]Docker MYSQL '[2002] Connection refused'

I was trying out Docker for the first time.我是第一次尝试 Docker。 Got a LEMP stack up and running, but I can't connect to the MYSQL Database.启动并运行了 LEMP 堆栈,但我无法连接到 MYSQL 数据库。 Not on my Symfony application, not on PHPMyAdmin.不在我的 Symfony 应用程序上,不在 PHPMyAdmin 上。 The applications are returning the following error code:应用程序返回以下错误代码:

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.感谢您的时间。

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). “[2002] 连接被拒绝”意味着您可以访问数据库服务器,但您没有用户的正确访问权限(在您的情况下为管理员)。 By default mariadb have a root user with the password given by MYSQL_ROOT_PASSWORD and this user can connect from any server (%).默认情况下,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).这里的问题是您将数据库服务器命名为“mysql”(docker-compose 文件中的服务名称)。 But by default phpmyadmin tries to connect to a database server named 'db'.但默认情况下,phpmyadmin 会尝试连接到名为“db”的数据库服务器。 Adding PMA_HOST: mysql under the environment section of the phpmyadmin service will resolve this problem.在phpmyadmin服务的环境部分下添加PMA_HOST: mysql就可以解决这个问题。


I think that MYSQL_USERNAME and PMA_ARBITRARY are useless if you work with default configuration (connection with root to your databases server) 我认为 MYSQL_USERNAME 和 PMA_ARBITRARY 如果您使用默认配置(与 root 连接到您的数据库服务器)是无用的

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.获取你的容器IP地址。 From your docker host从您的 docker 主机

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

UPDATED: For latest docker versions and based on the services name, "mysql", in your docker-compose.yml更新:对于最新的 docker 版本并基于您的 docker-compose.yml 中的服务名称“mysql”

mysql:
  image: mariadb
  ports:
    - 3306:3306

You can try this:你可以试试这个:

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

DB_HOST is the name of MySQL service name defined in docker-compose.yml DB_HOST 是 docker-compose.yml 中定义的 MySQL 服务名称

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.我已经设法使用 mysql 命令行工具连接到 mysql 实例,这是我使用的命令 - mysql -u root -p -h 127.0.0.1 ,并输入管理员密码。 Is that a sufficient solution for you?这对你来说是一个足够的解决方案吗?

In my case I was running mysql in a docker container whose port was mapped to the host mac ( 3306:3306 ).就我而言,我在端口映射到主机 mac ( 3306:3306 ) 的 docker 容器中运行mysql I tried connecting to this database from a phpmyadmin docker container using 127.0.0.1 .我尝试使用 127.0.0.1 从phpmyadmin docker 容器连接到这个数据库。 But it won't work because the localhost on the phpmyadmin docker container does not have the required mysql running.但它不起作用,因为phpmyadmin docker 容器上的localhost没有运行所需的mysql

To connect to the host from docker network从 docker 网络连接到主机

docker.for.mac.host.internal

Docker Networking Docker Compose Networking Docker 网络Docker Compose 网络

For my instance, the issue was with port mapping somehow.就我而言,问题出在端口映射上

In my case it was 3307:3306 , as soon as I changed it to 3307 on the right side as well, I could connect to the DB instance.就我而言,它是3307:3306 ,只要我在右侧将其更改为3307 ,就可以连接到数据库实例。

添加DB_READ_HOST=db为我解决了这个问题

If you want to know why your connexion failed, you can use in your terminal php artisan tinker and then like如果你想知道你的连接失败的原因,你可以在你的终端中使用 php artisan tinker然后像

DB::connection()->getPdo();

Unfortunately this will only give you a part of the error.不幸的是,这只会给你一部分错误。 Quit tinker and then use this command php artisan db will give you more information like database type, host, port, ad user about the issue.退出 tinker ,然后使用此命令php artisan db将为您提供有关该问题的更多信息,例如数据库类型、主机、端口、广告用户。

Like this one像这个

ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (61)

   Symfony\Component\Process\Exception\ProcessFailedException 

  The command "'mysql' '--host=127.0.0.1' '--port=3306' '--user=root' '--default-character-set=utf8mb4' 'laravel'" failed.

Exit Code: 1(General error)

Working directory: /Users/Dev/Documents/www/laravel_docker/src/addressAPI

Output:
================


Error Output:
================

  at vendor/symfony/process/Process.php:270
    266▕      */
    267▕     public function mustRun(callable $callback = null, array $env = []): self
    268▕     {
    269▕         if (0 !== $this->run($callback, $env)) {
  ➜ 270▕             throw new ProcessFailedException($this);
    271▕         }
    272▕ 
    273▕         return $this;
    274▕     }

      +14 vendor frames 
  15  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

This will not give you the answer on what is wrong, but at least you can understand why your config is wrong.这不会给你什么是错误的答案,但至少你可以理解为什么你的配置是错误的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM