Docker MYSQL'[2002]连接被拒绝' [英] Docker MYSQL '[2002] Connection refused'
问题描述
我是第一次尝试Docker.建立了LEMP堆栈并开始运行,但是我无法连接到MYSQL数据库.不在我的Symfony应用程序上,不在PHPMyAdmin上.应用程序返回以下错误代码:
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:
驱动程序中发生异常:SQLSTATE [HY000] [2002]连接被拒绝
An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused
这是我的docker-compose.yml:
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:
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 URL: https://github.com/MolengraafFrank/DockerSymfony
有人可以帮我吗?谢谢您的时间.
Could someone help me out? Thank you for your time.
推荐答案
"[2002]连接被拒绝"意味着您可以访问数据库服务器,但用户无权访问(在您的情况下为admin) .默认情况下,mariadb具有root用户,其密码由MYSQL_ROOT_PASSWORD给出,并且该用户可以从任何服务器(%)连接.
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 (%).
如果要使用过度登录数据库,则必须在数据库服务器中创建该登录名,并有权从选定位置对数据库进行正确的授予.
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.
此处的问题是您已将数据库服务器命名为"mysql"(docker-compose文件中的服务名称).但是默认情况下,phpmyadmin尝试连接到名为"db"的数据库服务器.在phpmyadmin服务的environment部分下添加PMA_HOST: mysql
将解决此问题.
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: mysql
under the environment section of the phpmyadmin service will resolve this problem.
我认为,如果使用默认配置(用root连接到数据库服务器),则MYSQL_USERNAME和PMA_ARBITRARY没用
I think that MYSQL_USERNAME and PMA_ARBITRARY are useless if you work with default configuration (connection with root to your databases server)
这篇关于Docker MYSQL'[2002]连接被拒绝'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!