English 中文(简体)
Docker MYSQL[2002] 拒绝联系
原标题:Docker MYSQL [2002] Connection refused

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:

司机有例外:KallSTATE[HY000] [2002] 拒绝联系

这是我的cker。

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:

    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

谁能帮助我? 感谢你时间。

最佳回答

[2002年]链接拒绝,意味着你可以进入数据库服务器,但用户(在您的个案中)没有适当的接入。 海运局有一个根基用户,其密码由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.

www.un.org/Depts/DGACM/index_spanish.htm 这里的问题是,你把你的数据库服务器命名为我的仪表(在cker-comp中的名称)。 但是,由于违约,Sphpmyadmin试图与一个名为db的数据库服务器连接起来。 添加<条码>PMA_HOST: mysql,在phpmyadmin服务的环境部分之下,将解决这一问题。


I think that MYSQL_USERNAME and PMA_ARBITRARY are useless if you work with default configuration (connection with root to your databases server)
问题回答

我面临这一挑战,因为我拥有3个拥有不同IP地址的不同集装箱。

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

因此,我通过编辑我的Laravel .env 案卷确定。

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

集装箱 Ip Address. 您的cker客

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

mysql:
  image: mariadb
  ports:
    - 3306:3306

你可以尝试:

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 specified in docker-compose.yml

我曾设法利用我sql的指挥线工具,将我与我的手脚相连接,这是使用的指令I——mysql -u fundamental -p -h 1270.0.1,并进入行政密码。 这是否足以解决你的问题?

In my case I was running mysql in a docker container whose port was mapped to the host mac (3306:3306). I tried connecting to this database from a phpmyadmin docker container using 127.0.0.1 . But it won t work because the localhost on the phpmyadmin docker container does not have the required mysql running.

连接cker网的东道方

docker.for.mac.host.internal

Docker Networking Docker Compose Networking

就我而言,问题在于portmap

在我的案件中,当我将其改为3307:3306时,我也可与非行联系。

If you want to know why your connexion failed, you can use in your terminal php artisan tinker and then like

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.

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

   SymfonyComponentProcessExceptionProcessFailedException 

  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
      IlluminateFoundationConsoleKernel::handle(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))

这不会给你回答什么是错的,但至少你可以理解为什么你们的愤怒是错误的。

⚠ This answer is an additional hint for a special case!

如果出现突然错误,情况就是如此。 i.e.?everything在<>but after <>em> an update of 拦截一些集装箱或类似集装箱,IP >MySQL集装箱可能发生变化,并可能导致这种错误。 在我的案件中,Laravel .env

DB_CONNECTION=mysql
DB_HOST=172.19.0.3
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=db_user
DB_PASSWORD=superSecret

and for Yii2 app config/db.php

return [
     class  =>  yiidbConnection ,
     dsn  =>  mysql:host=172.19.0.3;dbname=db_name2 ,
     username  =>  db_user ,
     password  =>  superSecret ,
     charset  =>  utf8 ,
];

我在两处服完罚款后,都会发现这种错误。 但是,运行<代码>docker 检查了我的sql,其中我的sql是网络部分的神秘集装箱的名称:

...
"Gateway": "172.19.0.1",
"IPAddress": "172.19.0.2",
...

因此,从172.19.0.3改为172.19.0.2。 在该案中解决这一问题。

you need to link the phpfpm container to mysql.

添加<条码>DB_READ_HOST=db 解决了这一问题。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...