I have a simple connection in my laravel route (just for testing), but it Gives me following error:
Could not connect to the database. Please check your configuration. error:PDOException: SQLSTATE[HY000] [2002] No such file or directory in
Here is the route:
Route::get('testcon', function(){
try {
DB::connection()->getPdo();
} catch (\Exception $e) {
die("Could not connect to the database. Please check your configuration. error:" . $e );
}
});
When I do a manual SQL connect it works all fine:
Route::get('testcon', function(){
$db = new PDO('mysql:host=127.0.0.1;dbname=laravelapp;charset=utf8mb4', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$stmt = $db->query("SELECT * FROM users");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo '<pre>';
print_r($results);
echo '</pre>';
});
Here is my .env file for the database settings:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=
DB_DATABASE=laravelapp
DB_USERNAME=root
DB_PASSWORD=
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
What on earth can be wrong? I have tried to change back to localhost as host, removed the socket, cleared the laravel cache, change to another mysql app (I am using MAMP) but laravel unfortunately keeps can't connecting to the database.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community