PDOException: SQLSTATE[HY000] [2002] No such file or directory
Asked Answered
P

11

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}

Postdiluvian answered 17/4, 2015 at 9:32 Comment(9)
Hope someone can answer it, because from my point of view the problem should be better described...Nitaniter
I guess you have wrong credential for your mysql defined (mysql hostname, user, password)Jenninejennings
where can I check mysql credential? @JenninejenningsPostdiluvian
possible duplicate of Error on creating connection to PDO in PHPEpisode
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 youJenninejennings
@Jenninejennings actually I have done same thing as per this link raywenderlich.com/32963/…Postdiluvian
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?Jenninejennings
yes MySql running. all things work expected. except database connection @JenninejenningsPostdiluvian
Possible duplicate of PHP - MySQL connection not working: 2002 No such file or directoryParaffin
J
55

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

Jeffrey answered 23/10, 2015 at 13:24 Comment(1)
For whatever weird reason this worked for me - nextcloud on MySQL.Pteropod
C
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

Conundrum answered 28/3, 2016 at 14:58 Comment(2)
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.Titulary
hello community , i am getting similar error even though i am not using laravel can you look at the problem here it is #60796832Titmouse
D
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!

Doley answered 11/6, 2017 at 14:10 Comment(0)
J
4

Restart your database and local web server:

sudo service mysqld restart

It should work!

Jealousy answered 1/7, 2015 at 8:35 Comment(1)
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.Syzran
R
4

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.

Rasbora answered 27/3, 2018 at 9:0 Comment(0)
M
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),
    ]
Martinic answered 11/1, 2017 at 11:49 Comment(0)
K
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

Kennedy answered 28/3, 2018 at 5:42 Comment(0)
H
3

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

php artisan cache:clear
php artisan config:clear 
php artisan view:clear
Harmful answered 29/10, 2019 at 3:59 Comment(1)
I really didn't expect to solution to be that easy, but sir this three lines solved my problemSchmid
L
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.

Lagging answered 15/6, 2020 at 15:59 Comment(0)
R
0

Happy to report PDO socket can now be adjusted directly in /etc/php.ini, even though older sources indicate it can be specified only when compiling php/pdo:

[Pdo_mysql]
pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock

[MySQLi]
mysqli.default_socket = /var/run/mysqld/mysqld.sock

[MySQL]
mysql.default_socket = /var/run/mysqld/mysqld.sock

Source PHP set MySQL socket location

Riggs answered 14/11, 2023 at 20:39 Comment(0)
P
-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!

Peavy answered 19/3, 2016 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.