Femke
Femke

Reputation: 151

Database Laravel php artisan migrate connection refused

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.

Upvotes: 15

Views: 47733

Answers (9)

Andris Briedis
Andris Briedis

Reputation: 433

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.

Upvotes: 0

Jan Richter
Jan Richter

Reputation: 2086

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

Upvotes: 8

Guy
Guy

Reputation: 1332

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.

Upvotes: 2

Saurabh Mistry
Saurabh Mistry

Reputation: 13669

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

Upvotes: 0

Marcial Soto
Marcial Soto

Reputation: 440

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)

Upvotes: 34

philyawj
philyawj

Reputation: 151

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.

Upvotes: 7

Victor Ighalo
Victor Ighalo

Reputation: 151

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.

Upvotes: -3

YokoH
YokoH

Reputation: 9

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

Upvotes: 0

crabbly
crabbly

Reputation: 5186

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

Upvotes: 6

Related Questions