SQLSTATE[HY000] [2002] Connection refused within Laravel homestead
PhpLaravel 5MigrationHomesteadLaravel ArtisanPhp Problem Overview
Using Mac OS X and Homestead 2.2.1 with Laravel 5.2.
In terminal (within homestead in my project folder) I can do php artisan to see all the available commands. When I try to run php artisan migrate I get a connection error:
SQLSTATE[HY000] [2002] Connection refused
I have setup a Laravel project with these .env settings
DB_HOST=127.0.0.1
DB_DATABASE=tcv
DB_USERNAME=homestead
DB_PASSWORD=secret
I have also tried localhost for DB_HOST and root for DB_USERNAME and DB_PASSWORD. And all possible variations of these put together!
In Sequel Pro (db management application) I CAN connect with these settings
Host 127.0.0.1
Username homestead
Password secret
Database tcv
Port 33060
But this database is obviously empty, because I cant migrate to it from terminal ...
As far as I can make out it is a configuration issue, since I can connect to it with Sequel Pro. But I have honestly no freaking idea what is setup wrong.
Thanks for the help !!
EDIT
For some reason I get the same SQLSTATE[HY000] [2002] Connection refused
error when moving my project to MAMP and running php artisan migrate.
Now I am completely lost ...
Php Solutions
Solution 1 - Php
I just ran into this and found that changing this in the .env file from 127.0.0.1
to localhost
fixed it.
DB_HOST=localhost
Solution 2 - Php
Problem
In Laravel you have config/database.php
where all the setup for the connection is located. You also have a .env
file in the root directory in your project (which everyone uses for timesaving). This contains variables that you can use for the entire project.
On a standard L5 project the MySql section of config/database.php
looks like this:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
Notice there is no port set!
Although in my .env
file I had set DB_PORT=33060
. But that value (3306)
was never read into the config/database.php
.
So don't be a dumbass like myself and forget to check the database.php
file.
FIX
Simply add
'port' => env('DB_PORT', 3306),
to your config/database.php and set that value in .env like this DB_PORT=3306
Solution 3 - Php
If you are using MAMP on a mac OS add the following line to your mysql database config file
'unix_socket' => env('DB_SOCKET', ''),
and on your .env file add
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
Solution 4 - Php
Use localhost
instead of 127.0.0.1
(in your .env file), then run command:
php artisan config:cache
Solution 5 - Php
PROBLEM SOLVED
I had the same problem and fixed just by converting DB_HOST constant in the .env File FROM 127.0.0.1
into localhost *JUST DO THIS AS GIVEN IN THE BELOW LINE* DB_HOST = localhost
.
No need to change anything into config/database.php
Don't forget to run
php artisan config:clear
Solution 6 - Php
Another solution for anyone else who has a problem. I had all the settings correct but for some reason my changes weren't updated. Laravel actually caches the config file (which I find completely stupid).
Here was my solution after updating the configs:
php artisan config:clear
Solution 7 - Php
I was having this problem. When connecting with Sequel Pro, I need to use 33060
as the port, but in the .env file it has to be 3306
. I had 33060
in the .env file. Changed it to 3306
and it worked.
Solution 8 - Php
If you have your backend and database started on docker
Instead of putting localhost or 127.0.0.1 as DB_HOST put the name of the registered service that indicates your database service in the docker-compose file.
In my case for example I replaced 127.0.0.1 with db because in my docker-compose file I had defined the name of the service for the database as db
My docker-compose looks something like that
services:
db: <------ This is the name of the DB_HOST
container_name: admin_db
image:mysql:5.7.22
.
.
.
Solution 9 - Php
This is a simple fix. Your mysql database has lost its connection to the server. If you are running a local server run this in your terminal:
mysqld
That will reconnect your database. Then (if you are using homebrew) run:
brew services start mysql
This will automatically connect your database on login.
Solution 10 - Php
In my case, I was having the same error using docker and the trick was in setting in the .env
file this DB_HOST=db
where db is the name of the container running the database server.
Solution 11 - Php
I had the same problem, try this works
DB_HOST=localhost
Solution 12 - Php
It's possible that your mysql
hasn't started or is not to the port 3306
Solution 13 - Php
In my case this error appeared out of blue. While staring at that mysterious error I've realized, that I was trying to run the command outside of vm...
Solution 14 - Php
After you put all configuration on .env file, if you already run php artisan serve
, RESTART IT.
Solution 15 - Php
For me enclosing the credentials in quotes did the trick
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE='zzz'
DB_USERNAME='yyy'
DB_PASSWORD='XXX'
Solution 16 - Php
When Laravel connects to mysql on Homestad box and running commands on local environment:
DB_HOST=127.0.0.1
the app needs DB_PORT=3306
the migrations need DB_PORT=33060
otherwise gives Connection refused
phpunit needs DB_PORT=33060
otherwise gives Connection refused
DB_HOST=localhost
the app works with both DB_PORT=3306
or DB_PORT=33060
migrations cannot run
phpunit cannot run
Have in mind that phpunit takes its values from the .env.testing
file, if exists, otherwise from the phpunit.xml
file on the root folder of the app
Solution 17 - Php
Me, Im using vagrant, yet im executing php artisan outside the box so basically it doesn't have permission
Solution 18 - Php
I could be because you might have not restarted PHP artisan since long
So After making DB changes and config:clear Tinker works fine
But to make browser refect the new DB connection you need to re-run
> php artisan serve
Solution 19 - Php
- terminate sqld from task manager
- stop MySQL from xampp control panel and restart it
if it still throw the same problem, from root folder run
php artisan cache:clear
php artisan config:cache
php artisan serve
3. if you still have the problem, check whether you are using multiple copies of sql server, for example through Ubuntu app, if so stop the MySQL server in ubuntu
sudo service MySQL stop
Solution 20 - Php
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=8080
DB_DATABASE=flap_safety
DB_USERNAME=root
DB_PASSWORD=mysql
the above given is my .env
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'mysql'),
// 'port' => env('DB_PORT', '8080'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', 'mysql'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
the above given is my database.php file. i just commented out port from database.php and it worked for me.
Solution 21 - Php
If you are using Homestead
then you should be running it with the default mysql port. So instead of using DB_PORT=33060
, you should be using DB_PORT=3306
in your .env file. Also, remember to run your php artisan migrate
commands in your homestead installation and everything should be ok.
Hope that helps.
Solution 22 - Php
The only thing that solved it for me was to put the connection details in config/database.php instead of the .env file. Hope this helps
Solution 23 - Php
I had a similar problem and no suggestions placed here helped me. This what has fixed my problem was to set the application name and database hostname with the same value. In my case, 127.0.0.1 works correctly.
APP_URL=127.0.0.1
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=zzz
DB_USERNAME=yyy
DB_PASSWORD=XXX
Solution 24 - Php
Okay update it seems the host is not the issue but actually the port.
So its actually the port 3306 for the browser testing and yet for terminal and Sequel Pro 33060. Could it have something to do with adding it in the Homestead.yaml and set it here?
On reading the setup in relation to Laravel it says:
MySQL: 33060 → Forwards To 3306
Solution 25 - Php
I encountered
> SQLSTATE[HY000] [2002] Connection refused (SQL: select * from > projects)
The cause was that I had not started the MySQL server.
It may help to check that the MySQL is running as the first step in troubleshooting the error.
Solution 26 - Php
I found that my server just needed to be restarted and "bam" it was fixed.
Solution 27 - Php
If you are using Homestead like I do, do not forget to ssh into it before running artisan test for example.
Solution 28 - Php
I don't think it even reads config/database.php. As soon as edited my entries into .env file it started working.
Solution 29 - Php
If you have any kind of proxy
or vpn
, turn them off and close them . it was my problem .
Solution 30 - Php
On ubuntu/linux you can try to restart mysql with this command: sudo service mysql restart