15

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.

3
  • 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. Commented May 5, 2017 at 12:30
  • Ah so easy that's it thank you! Commented May 5, 2017 at 12:56
  • I am using Mamp in Mac, I have same this issue. How can I resolved it? Thanks Commented Jan 28, 2018 at 9:25

9 Answers 9

34

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)

Sign up to request clarification or add additional context in comments.

2 Comments

any idea about that??
Thanks Marcial! (This is a bit of an epic FAIL on the part of the laravel developers!!)
8

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

1 Comment

This also worked for me when using XAMPP VM version on MacOS. Thanks!
7

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.

2 Comments

Thank you, I've tried several times but the error continues, not realizing that it's a virtual machine version.
Great answer there .But can't something be done about XAMPP-VM for OS X to fix this issue with Laravel ?
6

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=""

Comments

2

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.

Comments

0

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

Comments

0

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

Comments

0

I had a similar problem. Only it was using "ddev". It turned out that mysql responds to a completely different IP instead of 127.0.0.1 or locahost.

  1. console: ddev ssh // If using ddev.

  2. Login to mysql: mysql -y USERNAME -p

  3. Runs this "SELECT SUBSTRING_INDEX(USER(), '@', -1) AS ip, @@hostname as hostname, @@port as port, DATABASE() as current_database;" It displays a table with data. The IP may not be the one to connect to. Must take is hostname.

  4. ping "hostname" from the previous table. And then try to put the obtained IP in the .env file. Or "hostname". It also fits.

Now connection works.

Comments

-3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.