Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mamp

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}


2 Answers

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

like image 189
Adrian Enriquez Avatar answered Sep 12 '25 17:09

Adrian Enriquez


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

like image 39
hhwithgroups Avatar answered Sep 12 '25 16:09

hhwithgroups