Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLSTATE[HY000] [2002] Connection refused #1323

Closed
omag0 opened this issue Dec 28, 2017 · 13 comments
Closed

SQLSTATE[HY000] [2002] Connection refused #1323

omag0 opened this issue Dec 28, 2017 · 13 comments
Labels

Comments

@omag0
Copy link

omag0 commented Dec 28, 2017

  • System info ( Linux):

Hello guys. Have a problem, and need help.

I clone laradock in /var/www/docker/laradock
than i make docker-compose up -d apache2 mysql

and in /var/www/docker i run laravel new project.

my laradock .env
MYSQL_VERSION=8.0
MYSQL_DATABASE=AriesDB
MYSQL_USER=default
MYSQL_PASSWORD=secret
MYSQL_PORT=3307
MYSQL_ROOT_PASSWORD=root
MYSQL_ENTRYPOINT_INITDB=./mysql/docker-entrypoint-initdb.d

my laravel .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3307
DB_DATABASE=AriesDB
DB_USERNAME=root
DB_PASSWORD=root

when i do "php artisan migrate" and "php artisan db:seed" everything works ok
buy when i run my browser and put my login and password, show this messenger SQLSTATE[HY000] [2002] Connection refused (SQL: select * from users where email = rodrigo@com.br limit 1).

if i do docker-compose exec workspace bash, and try "php artisan migrate" show this messenger: SQLSTATE[HY000] [2002] Connection refused

I tryied change DB_HOST=127.0.0.1 to DB_HOST=mysql but nothing changed.
Can somebody help me?

@omag0
Copy link
Author

omag0 commented Dec 28, 2017 via email

@harrysbaraini
Copy link

harrysbaraini commented Dec 28, 2017

Use DB_HOST=mysql instead of 127.0.0.1. When running outside of workspace container, the MySQL container is accessible on 127.0.0.1, but when you're in the workspace container, 127.001 is relative to the container itself, so container will try to connect to its own localhost.

as @InTheDeepEnd said below, your .env should look like this:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=username
DB_PASSWORD=password

If you want to run php artisan commands outside of the workspace container, you can set DB_HOST=your docker ip, but I don't recommend so, because your environment may be different of the containers, e.g., have some PHP library missing.

@ed-fruty
Copy link
Contributor

@omag0 when you want to use cli commands, like artisan you must enter into the workspace container, like

cd /var/www/docker/laradock
docker-compose exec --user=laradock workspace bash

# here you can use cli
php artisan migrate

@bradleyess
Copy link

bradleyess commented Dec 30, 2017

@harrysbaraini answer is correct.

In simpler terms, your laravel .env should look like this :

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=username
DB_PASSWORD=password

@rafa-acioly
Copy link

I'm facing this problem as well, but i've already config. my .env file:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=mydb
DB_USERNAME=root
DB_PASSWORD=root

PDOException::("SQLSTATE[HY000] [1130] Host '172.20.0.3' is not allowed to connect to this MySQL server")
/var/www/ftimesheet/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php : 68

PDO::__construct("mysql:host=mysql;port=3306;dbname=mydb", "root", "root", [])
/var/www/ftimesheet/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php : 68

@danaia
Copy link

danaia commented Feb 24, 2018

I am getting the exact same error as @rafa-acioly with the same .env file. This has been running fine for a few months. Made no changes to the Laradock files Getting this error from a clean install and running from a Laradock fork which has not failed me until now.

@tpraxl
Copy link

tpraxl commented Mar 17, 2018

Just had the same problem. Turned out that this was due to a misconfiguration.

I think I'll post a complete example, might help others to understand the issue.

Given you have the following section in your laradock .env

MYSQL_VERSION=latest
MYSQL_DATABASE=default
MYSQL_USER=default
MYSQL_PASSWORD=secret
MYSQL_PORT=3307
MYSQL_ROOT_PASSWORD=root
MYSQL_ENTRYPOINT_INITDB=./mysql/docker-entrypoint-initdb.d

Then you need the following laravel .env

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=default
DB_USERNAME=default
DB_PASSWORD=secret

Then you can docker-compose exec --user=laradock workspace bash and

php artisan migrate:install
php artisan migrate

Wondering why DB_PORT=3306 is configured in laravel's .env?

Actually, the port you specified in laradock's .env is the port mapped to the host. It should not matter for container communication.

Guessing what to do if you wanted to change the port for containers

I guess, if you wanted to change the mysql port for container communication, you would need to change mysql/my.cnf

[mysql]
port=3309

And you might need to change the mysql/Dockerfile to EXPOSE 3309 then. You might also need to adapt the docker-compose.yml to this:

ports:
        - "${MYSQL_PORT}:3309"

@NameIvan
Copy link

Use container name of mysql in DB_HOST. Like this DB_HOST=laravellocal_mysql_1

@oliver-com
Copy link

oliver-com commented Jul 12, 2019

If u have changed the MYSQL_PORT in laradock .env , u may changed the DB_PORT in laravel .env too.
So, just keep the DB_PORT=3306 in laravel .env. I have solved it in this way.

image

@stale
Copy link

stale bot commented Feb 2, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Stale label Feb 2, 2020
@stale
Copy link

stale bot commented Feb 23, 2020

Hi again 👋 we would like to inform you that this issue has been automatically closed 🔒 because it had not recent activity during the stale period. We really really appreciate your contributions, and looking forward for more in the future 🎈.

@phitran35
Copy link

phitran35 commented Jun 25, 2020

Use DB_HOST=mysql instead of 127.0.0.1. When running outside of workspace container, the MySQL container is accessible on 127.0.0.1, but when you're in the workspace container, 127.001 is relative to the container itself, so container will try to connect to its own localhost.

as @InTheDeepEnd said below, your .env should look like this:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=username
DB_PASSWORD=password

If you want to run php artisan commands outside of the workspace container, you can set DB_HOST=your docker ip, but I don't recommend so, because your environment may be different of the containers, e.g., have some PHP library missing.

I have to say thanks to. you. Love it
I did try to use 172.0.0.1 as usual to connect MySQL

@Dazeh
Copy link

Dazeh commented Nov 18, 2020

Anyone still facing this issue - If you ran php artisan config:cache on your local machine (in my case windows), you instead need to run the artisan command from within your workspace container as this is creating a config file which has your windows paths instead of the containers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests