9
votes

When I use 'php artisan migrate' I get the following error message:

[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations)

[PDOException]
SQLSTATE[HY000] [2002] Connection refused

I've installed Laravel on a mac with XAMPP and have the following settings:

database.php

'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

I've tried several solutions I could find online, but none have worked so far.

8
If you've installed with XAMPP, those are most likely not the correct database settings. You will have to figure out what those are and place them in your .env file.user1669496
Ah so easy that's it thank you!Femke
I am using Mamp in Mac, I have same this issue. How can I resolved it? Thankshuykon225

8 Answers

27
votes

This is really anoying, but changing DB_HOST=127.0.0.1 to DB_HOST=localhost solves the problem. Give it a try (obviously your file permission must be the right one)

7
votes

The solution for me was different than anywhere else I found online.

I was unknowingly using the VM (virtual machine) version of XAMPP on Mac, which functions differently than the normal version. VM XAMPP interface looks like this.

If you are using the VM XAMPP, uninstall it and install the correct XAMPP version here.

Once I installed the new version php artisan migrate worked.

5
votes

First create your database. Read more about it here: http://www.complete-concrete-concise.com/web-tools/creating-a-mysql-database-using-xampp

Let's say your new database is named: my_db.

Use this in your .env:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_db
DB_USERNAME=root
DB_PASSWORD=""
4
votes

If someone is experiencing this when using Docker, I had a multi-stage build with first container running the dependency installation and the second one just being the runtime. What I didn't realise is that the installation with Laravel scripts generate a cached config (bootstrap/cache/config.php) which is used instead of the config/database.php file.

Adding the following to the Dockerfile as the last step did the trick:

RUN php artisan config:clear
2
votes

I was using a Vagrant machine to run the whole thing, but I was mistakenly running command on my own machine.Thought this might help someone.

0
votes

Hi you don't have DB_SOCKET= /path/to/socket in env.file while you have unix_socket => env('DB_SOCKET', '') in database.php file.

You could get the /path/to/socket by $ mysql_config --socket

0
votes

if you are using MAMP then in your .env file :

DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=root
DB_PASSWORD=root
-3
votes

This is coming late but it might help someone. I had the same error, it turned out it was a typo in my .env file. Instead of DB_HOST, it was B_HOST. In your case it might be some other env key. Just look closer and you will discover the you have a malformed env file.