Artisan migrate in Laravel 5 throwing PDOException - laravel

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?

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'.

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.

Related

Laravel Issue with Artisan when using different computer on NAS to run terminal

I have my code on a local NAS at home. When I run
php artisan serve
on terminal in my desktop computer, the application starts up no problem.
However, when I am on my laptop, and I want to run the server, the following part just runs endlessly and doesn't proceed:..
Starting Laravel development server: http://127.0.0.1:8000
The reason I'm asking here is I am having a hard time knowing how to troubleshoot this issue.
The error I'm receiving when I try to load the app URL from my laptop while the terminal is still in 'starting' mode is..
SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from `sessions` where `id` = ZbOqp13BZNRquGtmG0Vi2XNHGWi3WDMJxW4ZYYCK limit 1)```
Also, my database.php in the config folder under mysql looks like this:
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '33060'),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],```
I don't see this issue mentioned on SO previously, and would appreciate ideas on how to troubleshoot this. Ideally I should be able to connect to the server fine even if I am on another computer in the same network.
Troubleshooting tips appreciated.
My .env for the database is:
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret```

How to solve "could not find driver" error in laravel?

I am getting database connection error in laravel. I am requesting to check my code.
I already tried to remove semicolon(;) in php.ini file
route.php:
Route::get('/Insert', 'UserController#insert');
UserController.php:
public function insert(Request $request)
{
$result=DB::insert("insert into test (id, name, email) values(?,?,?)",['1','Nirav','n#gmail.com']);
$result=DB::select("select * from test");
print_r($result);
}
database.php:
DATABASE NAME : data
TABLE NAME : test
'default' => env('DB_CONNECTION', 'mysql'),
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'database' => env('DB_DATABASE', "data"),
'username' => env('DB_USERNAME', "root"),
'password' => env('DB_PASSWORD', ""),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
],
.env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=data
DB_USERNAME=root
DB_PASSWORD=
I am inserting record in database.
you need install PDO_MYSQL driver for your PHP
Go to wamp64/www/bin/php/php* (where * is the php version you are using)
Edit file php and uncomment this line by removing the semicolon:
;extension=pdo_pgsql to extension=pdo_pgsql
Save and restart your Wamp/xampp server
check your .env and config/database again.
If you are on windows, and your php folder is not in your PATH, you have set the absolute directory in your php.ini
for example:
extension_dir = "C:/php7/ext"
and uncomment
extension=php_mysqli.dll
extension=php_pdo_mysql.dll
Restart apache2.4 and it should work.
I hope it helps.

SQLSTATE[28000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES)

I'm on a Ubuntu 15.10 Yosemite using Laravel 5.2.*
Here is my .env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Pastry
DB_USERNAME=root
DB_PASSWORD=243320
app\config\database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'Pastry'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', '243320'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
emember when I got the message:
SQLSTATE[28000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES)
Help me please!
First glance conclusion: The app you're trying to load in the browser is not using the info in the .env and database.php files you're looking at.
Are you accessing the correct URL? Aren't you trying to access a vanilla Laravel installation in a different folder? A fresh install of 5.2 has homestead as username.
Check your url
Check your virtualHost directory. Is the correct path used?
Thank you for your guidance.
I do not have access to the database.
But the construction of migrations was run properly.
But I can not run it for a test run:
public function index()
{
$com = new Categorys();
$com->name = 'ali';
$com->ename = 'hi';
$com->img = 'sdjf';
$com->save();
// return View('category.index');
}

vendor/config.php has database settings, how do I regenerate this?

I have a Laravel 5 project that I copied to a new folder. I changed the database settings in the .env file. But now I am concluding that database settings are also set in vendor/config.php. What command must I run to have this regenerated? I already tried composer dump-autoload and composer update without success
The default database config is in config/database.php and looks in .env for its settings (with some fallback defaults). For example, the default MySQL connection:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Laravel does not have a vendor/config.php file.

problems with database connection in laravel 5

I use the command php artisan migrate to migrate my database connection but I still get the same error and I checked everything, nothing wrong. I used the same connection that I always use in Laravel 4.2
Here is the message I get on my console:
exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES)' in C:\xampp\htdocs\Projects\blog\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:47
You need to change the values in your .env file, located in the root folder of your project.
If there is no .env file, copy the .env.example file to .env.
Laravel uses this file to protect your passwords. The values as you are setting those are only used if there is no configuration available in the .env file, homestead is the standard user there.
Take a look at your config/database.php and .env file.Maybe your database information is different.
You can go to .env file. File available on Project Root folder.
Then some here related to your database.
Then run below command to clear the old configuration cache.
php artisan config:clear
Have this problem as well. After update .env and run php artisan config:clear problem still persisted! Then after stoping the server and restart the server again with php artisan serve, problem fixed!
Change the following attribute from database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
to
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'laravel',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
This must work well then run the following command:
php artisan migrate:install
I solve this issue by update .env file with config/database.php file.
here is my config/database.php structure (I am using mysql)
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'larablog'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
And .env file structure (same like config/database.php)
DB_HOST=localhost // Host name
DB_DATABASE=larablog // Database name
DB_USERNAME=root // Database User
DB_PASSWORD= // Db password(No password here)
i was getting same error after updating database info in my .env file.
fixed the problem exit command prompt and re run php artisan serve command
it helped me out and might helpful for you.
If you're using sqlite as the database, make sure you've php5-sqlite installed
sudo apt-get install php5-sqlite
If it's installed, it might be a problem with your .env file. Replace
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
with this
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306

Resources