Reputation: 353
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.
Upvotes: 35
Views: 124416
Reputation: 466
I faced the same issue while trying to dockerize my laravel application. I seemed to have this issue with mysql:8.0
but not with mysql:5.7
. However, I did need version 8. So I just switched to mariadb:10.4
instead, which solved this issue for me.
Upvotes: 0
Reputation: 13534
In the case of sudden error such that. i.e.š”everything had been worked fine before but after an update of Docker during stopping some container or something like that, the IP of the MySQL container may be changed and it may lead to such errors. In my case, 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' => 'yii\db\Connection',
'dsn' => 'mysql:host=172.19.0.3;dbname=db_name2',
'username' => 'db_user',
'password' => 'superSecret',
'charset' => 'utf8',
];
I got such error for both apps, after they were working fine. However, running docker inspect mysql
where mysql is the name of the mysql container shows in the network section:
...
"Gateway": "172.19.0.1",
"IPAddress": "172.19.0.2",
...
So changing the the host IP in the configs from 172.19.0.3
to 172.19.0.2
solves the issue in that case.
Upvotes: 0
Reputation: 1
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.
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.
Upvotes: 0
Reputation: 175
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.
Upvotes: 1
Reputation: 17520
I had this challenge because I am running 3 different containers with different IP Addresses
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
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
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
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
Upvotes: 38
Reputation: 1008
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.
To connect to the host from docker network
docker.for.mac.host.internal
Docker Networking Docker Compose Networking
Upvotes: 4
Reputation: 1468
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.
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.
Upvotes: 31
Reputation: 6765
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?
Upvotes: 8