Docker MYSQL '[2002] Connection refused'

匿名 (未验证) 提交于 2019-12-03 01:22:02

问题:

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:

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

This is my 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:

    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

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

回答1:

The '[2002] Connection refused' means you can reach the database server, but you don't have right access for the user (in you 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 (%).

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

An over problem is that you maned you databases sever 'mysql' and by default phpmyadmin try to connect to 'db'. So you have to add

      PMA_HOST: mysql 

to you Phpmyadmin environment. I think that MYSQL_USERNAME and PMA_ARBITRARY are useless if you work with default configuration (connection with root to your databases server)



回答2:

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?



回答3:

you need to link the phpfpm container to mysql.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!