Reputation: 139
I just created a fresh Laravel 5 application and wrote some migrations. Whenever I tried to run php artisan migrate
, I encountered the following error
[PDOException]
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '
/var/lib/mysql/mysql.sock' (13)
My database configuration is correct
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'), //I also tried 127.0.0.1
'database' => env('DB_DATABASE', 'mydb'),
'username' => env('DB_USERNAME', 'myusername'),
'password' => env('DB_PASSWORD', 'mypassword'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
I am using LAMP stack, could this be the problem for Laravel 5?
Upvotes: 1
Views: 306
Reputation: 44526
Since the config is using env()
you need to set the values in the .env
file found in the root of your project. The values you are passing as the second parameter are defaults, which will only be used when the specific key is not found in the .env
file. So the database portion of .env
would look like this:
DB_HOST=localhost
DB_DATABASE=mydb
DB_USERNAME=myusername
DB_PASSWORD=mypassword
You can read more on how Laravel handles enviroments in the Configuration Docs.
Upvotes: 0
Reputation: 36
You can modify the database config (database.php
) to this
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost,
'database' => 'YOUR DB NAME',
'username' => 'YOUR DB USERNAME',
'password' => 'YOUR DB PASSWORD,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Or you can set the up the .env
file
Every Laravel
app now ships with a default .env.example
file, which at the moment looks like this:
APP_ENV=local
APP_KEY=SomeRandomString
DB_DATABASE=your_db_name
DB_USERNAME=your_username
DB_PASSWORD=your_db_password
In order to use this file, just copy it and name the copy (new file) .env
and enter your database details.
The .env
file should be in the root folder like .env.example
.
Note:- don't rename the .env.example
file....always create a new .env
The env('DB_DATABASE', 'default_value')
takes the value of DB_DATABASE
from .env
file if not found selects the default 'default_value'.
Upvotes: 1