4

I am using Vagrant with fresh laravel 5.4 installed. After I run auth I have ran the migrate to migrate the MySQL tables. All of these process successful but when I try to login or register to test the system I am getting error:

SQLSTATE[HY000] [2002] Connection refused.

But my database working perfect since I already able to migrate the tables. Whats possible solution could be? check attached image (I'm using Ubuntu 16.04)

Connection refused.

Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
Toyler Lainf
  • 43
  • 1
  • 1
  • 3
  • Did you also check if the tables are really created in phpmyadmin? (if you're running that) and is the mysql server on the same server as the code? – Arnold Feb 22 '17 at 11:57
  • yea i am using phpstrom (built in database manager) and i have configured the same db with it. so i have checked all tables has created successfully – Toyler Lainf Feb 22 '17 at 12:01
  • please share your .env file code – Sagar Arora Feb 22 '17 at 12:02
  • Check screenshot from here- https://expirebox.com/download/741543100157daa4d14c2b49bcb8ccdf.html – Toyler Lainf Feb 22 '17 at 12:08
  • Can you verify whether MySQL is listening on port 33060? Since the default port is 3306. Laravel Homestead uses the former by default so it is possible that the issue has something to do with that. Or are you using Homestead? – Diederik Feb 22 '17 at 12:17
  • yes i am using Homestead – Toyler Lainf Feb 22 '17 at 12:29
  • Does this answer your question? [SQLSTATE\[HY000\] \[2002\] Connection refused within Laravel homestead](https://stackoverflow.com/questions/35394230/sqlstatehy000-2002-connection-refused-within-laravel-homestead) – shmuels Aug 24 '20 at 20:25

3 Answers3

20

Try changing your DB_HOST from 127.0.0.1 to localhost. and if still not working try using the default mysql port to 3306

Onix
  • 2,129
  • 2
  • 17
  • 26
0

if you use laravel maybe your PDO PHP Extension not set. in your php.ini uncomment extension=php_pdo_mysql.dll

if you had no access to php.ini do this: add this to config/database.php in mysql section

'options' => [PDO::ATTR_EMULATE_PREPARES => true],

your code should be like this:

'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        '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',
        'options' => [PDO::ATTR_EMULATE_PREPARES => true],
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],
josef
  • 872
  • 9
  • 8
0

If you are trying to run php artisan migrate from host machine you need to set in .env conf file DB_PORT=33060 as that is a port vagrant/Homestead is listening on. But when i change that my application stop working. So i suggest ssh to vagrant and run php artisan migrate from VM and you will have no problems.

Disljenko
  • 1
  • 1