25

I have put PushChatServer dir in htdocs folder and create database puschat try to run @"http://localhost/PushChatServer/api/test/database.php"

Then I got following exception.

I want do same thing to explain this link http://www.raywenderlich.com/32963/apple-push-notification-services-in-ios-6-tutorial-part-2

I have done all that but I got this exception

Could not connect to the database. Reason: exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /Applications/MAMP/htdocs/PushChatServer/api/test/database.php:17 Stack trace: #0 /Applications/MAMP/htdocs/PushChatServer/api/test/database.php(17): PDO->__construct('mysql:host=loca...', 'root', 'root', Array) #1 {main}

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Hope someone can answer it, because from my point of view the problem should be better described... – ZygD Apr 17 '15 at 10:26
  • I guess you have wrong credential for your mysql defined (mysql hostname, user, password) – Marki555 Apr 17 '15 at 10:34
  • where can I check mysql credential? @Marki555 –  Apr 17 '15 at 10:35
  • possible duplicate of [Error on creating connection to PDO in PHP](http://stackoverflow.com/questions/1435445/error-on-creating-connection-to-pdo-in-php) – David Ansermot Apr 17 '15 at 10:35
  • Look at documentation of that "PushChatServer". Usually there is some file like `config.php`. Right now it is using default values (localhost and empty user and password), that's why it is not working for you – Marki555 Apr 17 '15 at 10:37
  • @Marki555 actually I have done same thing as per this link http://www.raywenderlich.com/32963/apple-push-notification-services-in-ios-6-tutorial-part-2 –  Apr 17 '15 at 10:45
  • So you have MySQL running on your host as per the tutorial, you have set the pushchat database and database user via phpmyadmin and it still doesn't work? – Marki555 Apr 17 '15 at 10:47
  • yes MySql running. all things work expected. except database connection @Marki555 –  Apr 17 '15 at 10:50
  • 1
    Possible duplicate of [PHP - MySQL connection not working: 2002 No such file or directory](http://stackoverflow.com/questions/1676688/php-mysql-connection-not-working-2002-no-such-file-or-directory) – kenorb Nov 13 '15 at 12:21

10 Answers10

54

You need to change host from localhost to 127.0.0.1

Laravel 4: In your app/config/database.php try changing host from localhost to 127.0.0.1

Laravel 5: In the .env file, change DB_HOST from localhost to 127.0.0.1

Source: PDOException SQLSTATE[HY000] [2002] No such file or directory

Adrian Enriquez
  • 8,175
  • 7
  • 45
  • 64
27

Quick test (run in shell):

php -r "new PDO('mysql:host=localhost;dbname=test', 'username', 'password');"

SQLSTATE[HY000] [2002] No such file or directory means php cannot find the mysql.default_socket file. Fix it by modifying php.ini file. On Mac it is mysql.default_socket = /tmp/mysql.sock (See MySQL connection not working: 2002 No such file or directory)

SQLSTATE[HY000] [1044] Access denied for user 'username'@'localhost' CONGRATULATION! You have the correct mysql.default_socket setting now. Fix your dbname/username/password.

Also see Error on creating connection to PDO in PHP

jdhildeb
  • 3,322
  • 3
  • 17
  • 25
hhwithgroups
  • 291
  • 3
  • 4
  • Thank you! This worked for me. I'm using PHP 7.1.9 on Mac OS X 10.10 running mysql 5.7 server from macport. For macport's mysql, mysql.sock lives at /opt/local/var/run/mysql57/mysqld.sock. I encountered this error trying to run 'php artisan migrate:install' using laravel 5.5. – RJ Cole Oct 03 '17 at 16:13
  • hello community , i am getting similar error even though i am not using laravel can you look at the problem here it is https://stackoverflow.com/questions/60796332/this-php-code-works-in-localhost-but-not-on-server – Kiran Patel Mar 22 '20 at 05:58
7

I had the same error using PHP 5.6.30 (macOS Sierra) with the simple PDO code like:

$pdo = new PDO('mysql:host=localhost;dbname=db_php', 'root', '');

And I received the same error message. To fix, I changed "localhost" for IP (loopback) "127.0.0.1" in my code:

$pdo = new PDO('mysql:host=127.0.0.1;dbname=db_php', 'root', '');

To test the connection:

$ php -r "new PDO('mysql:host=127.0.0.1;dbname=db_php', 'root', '');"

This work's for me!

Isa Souza
  • 1,621
  • 1
  • 12
  • 12
4

Restart your database and local web server:

sudo service mysqld restart

It should work!

tfantina
  • 788
  • 11
  • 37
RAWGIT
  • 99
  • 1
  • 3
  • This has been down-voted presumably because it's not a very 'technical' answer, but it worked for me, so an up-vote seems only fair. – Mere Development Feb 23 '17 at 09:47
3

I'm not sure if this will work for you; but I use CakePHP, and I get this error whenever I forget to put the port number at the end of the 'host'.

Hope this helps!

Before

'test' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'username' => 'root',
        'password' => 'root',
        'database' => 'mylogin',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
        'quoteIdentifiers' => false,
        'log' => false,
        //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
        'url' => env('DATABASE_TEST_URL', null),
    ]

After

'test' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost:8080',
        'username' => 'root',
        'password' => 'root',
        'database' => 'mylogin',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
        'quoteIdentifiers' => false,
        'log' => false,
        //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
        'url' => env('DATABASE_TEST_URL', null),
    ]
3

PDO treats localhost very particularly:

From http://php.net/manual/en/ref.pdo-mysql.connection.php:

Note: Unix only: When the host name is set to "localhost", then the connection to the server is made thru a domain socket. If PDO_MYSQL is compiled against libmysqlclient then the location of the socket file is at libmysqlclient's compiled in location. If PDO_MYSQL is compiled against mysqlnd a default socket can be set thru the pdo_mysql.default_socket setting.

That why PDO and mysql_connect will give different behavior for localhost.

So, if you want to use a TCP connection with PDO, never use localhost but 127.0.0.1.

Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124
3

I had the same error for Mysql PDO, this code works for me!

<?php
$dsn = 'mysql:host=127.0.0.1;dbname=testdb';
$username = 'username';
$password = 'password';
$options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
); 

$dbh = new PDO($dsn, $username, $password, $options);
?>

got this code from : http://php.net/manual/en/ref.pdo-mysql.connection.php

Kuhan
  • 495
  • 7
  • 17
3

Run the following command in shell, I think it will help.

php artisan cache:clear
php artisan config:clear 
php artisan view:clear
Tauhed
  • 109
  • 6
1

On Mac OSX/MAMP you may find a mysql.sock.lock file in your /Applications/MAMP/tmp/mysql folder.

You can safely remove the file mysql.sock.lock and you should be in good shape.

J Harvey
  • 61
  • 2
-1

Just ran into this same issue and the problem is the password. In the tutorial the author lists the password as:

"d]682\#%yl1nb3"

So as per the suggestion given by @Marki555, I looked in the config file - api_config.php. and the password listed there is:

"d]682\#%yI1nb3"

The upper case 'I' is what caused the issue because the password you set for user on the db has the password with the lowercase l but it really is looking for the uppercase I. After I changed the pushchat user's password to have an uppercase i, it worked!

mattyb
  • 1,011
  • 2
  • 10
  • 26