0

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

I am getting this error when trying to do "php artisan migrate". I have checked all my settings for the port and everything. My problem is the same as in this question:

Connection MySQL with PDO PHP

But I see it wasn't resolved. I even tried using this script:

https://ardamis.com/2008/05/26/a-php-script-for-testing-a-mysql-database-connection/

But no luck there either. Just a blank page.

Any ideas?

Community
  • 1
  • 1
robk27
  • 683
  • 1
  • 12
  • 23
  • I added 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock', to the database.php file with no luck either. But I went into that folder on my computer and I do not have that file. Only mysql.pid. Can I add a mysql.sock file or is it somewhere else? – robk27 Jul 20 '16 at 19:35
  • 1
    Is mysql running? Can you run `mysql` on the command line? If it gives you access denied, try `mysql -u -p`. If it gives you `Can't connect to local MySQL server through socket`, then mysql is not running. – aynber Jul 20 '16 at 19:44
  • Yes it is running. I can also access phpmyadmin and create databases and tables myself – robk27 Jul 20 '16 at 19:45

3 Answers3

0

The trick there may be that you are using "127.0.0.1" instead of "localhost". I have had problems like that, especially when creating the user for "localhost" and then trying to connect to the IP.

This is how my PDO connect looks like:

$hostname = 'localhost';
$username = '';
$password = '';
try {
    $dbh = new PDO("mysql:host=$hostname;dbname=DB", $username, $password);

On a side note, the file issues that Damien was having may not apply to your issue, as he describes two problems in his question, and when attempting "localhost" instead of the IP, he got past the first error.

  • Thanks Fernando. I think that was the original problem but I had changed it to localhost with no luck still. I just restarted the Apache servers and that error went away but am getting a different error now where it cannot find the file or directory. I think this has to do with the mysql.sock file but not sure how to resolve it yet. – robk27 Jul 20 '16 at 22:18
0

Ok not sure how this is fixed but it is now working. I changed it from "127.0.0.1" to "localhost" and the original error went away. But I got another error:

 [PDOException]                                    

SQLSTATE[HY000] [2002] No such file or directory

Reading through other threads, I tried changing it back to 127.0.0.1 and restarted the Apache servers and now it is working. Thanks for your help!

robk27
  • 683
  • 1
  • 12
  • 23
-1

I have also same problem while using laravel framework all you need to do is closing all apache server and mysql and also close laravel and first start your apache server and mysql and then via console php artisan serve and see the magic problem is solved

php_fever
  • 1
  • 1