how to connect to db with laravel 5 - php

I updated my laravel4 to laravel5 now my problem is i cant connect to database
My mysql
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'lue'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ' '),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
i returns this problem
PDOException in Connector.php line 47:
SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES)
help :)

You need to edit your .env file in the root of your application and change the DB username and password there

Make sure you are editing the .env file, and not a .env.php file. In Laravel 4 there was a way to use .env.environment.php but in Laravel 5 it's all done with a .env file (note that it doesn't have a php suffix)
Also the data in the .env file must not be in the array format. It should just be as follows:
APP_ENV=local
APP_DEBUG=true
APP_KEY=longstring
DB_HOST=localhost
DB_DATABASE=dbname
DB_USERNAME=homestead
DB_PASSWORD=secret

I had the same problem and this is how I fixed it.Go to the root of your laravel folder (application folder) in my case it is C:\firstapp and open .env file in notepad++ Update the following code accordingly.
DB_HOST=localhost
DB_DATABASE=firstapp (this is database name)
DB_USERNAME=root (user name)
DB_PASSWORD= (password , leave empty if none , do not even put "")
Important
and then restart your application like this php artisan serve (using command line) while you are in the root of you application folder. See this image for clarification link.

You need to edit your .env file in the root of your application. Just configure as following example:
APP_ENV=local
APP_DEBUG=true
APP_KEY=6CqAQJdOd8oKoh0NhbefkmS0Pd8FDH6h
DB_HOST=localhost
DB_DATABASE=your database name
DB_USERNAME=your database user name
DB_PASSWORD=your password
CACHE_DRIVER=file
SESSION_DRIVER=file

Related

Core UI - Laravel -> Setting Up

I'm trying to make Core UI - Laravel work on my localhost (https://github.com/taboritis/coreui-laravel).
I have followed the steps. I can get to the login screen. I have created the John Doe user by running php artisan migrate:fresh --seed, and I also made the migrations.
I've tried to log in, but every time, I get this error:
SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost'
(using password: YES) (SQL: select * from users where email =
john.doe#example.com limit 1)
My .env file looks like this:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=coreui
DB_USERNAME=root
DB_PASSWORD=root
My config/database.php file looks like this:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'coreui'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
I'm kind of stuck here. What should I do next?
Solved: For some reason I had to restart the web server and after several php artisan config:clear finally worked. Thanks everyone for your comments. You are all upvoted.
Happy coding!
The password is 321ewq , as you can see in
coreui-laravel/database/seeds/UsersTableSeeder.php
GitHub link of the user seeder
Edit
The documentation suggests you to put this configuration in your .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=coreui
DB_USERNAME=root
DB_PASSWORD=
But your .env file looks like:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=coreui
DB_USERNAME=root
DB_PASSWORD=root
Restart a web server and run this command:
php artisan config:clear

SQLSTATE[28000] [1045] Access denied for user [duplicate]

I just installed laravel and made a migration. But when i try to run it i get this error:
[PDOException]
SQLSTATE[28000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES)
Can anyone tell me what is wrong? I think the Database.php file looks different than normal. Is this something new in the new Laravel?
My Database config:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'LaraBlog'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Hope someone can help me :)
I tried to make changes to database.php file present within config folder it looks something like this
'default' => 'mysql',
....
...
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'sample'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
I am not using any VM, I am using my local machine, with the database user as root and password a null.
I have also changed my .env file and it looks something like this:
APP_ENV=local
APP_DEBUG=true
APP_KEY=zLzPMzs5W4FNNuguTmbG8M0iFqhIVnsP
DB_HOST=localhost
DB_DATABASE=sample
DB_USERNAME=root
DB_PASSWORD=null
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
Even after doing all the changes when I try to register using the registration form that is shipped with laravel, I try to add a user to my database I get the following error
After doing all the changes I cleared the cache and loaded it again and it seems to work for me now!
if any one is also facing the same issue just run the following commands
php artisan cache:clear
php artisan config:cache
I figured it out :) Had to chance the .env file :)
Is there any changes in the schemaes?
Shouldn't this work:
public function up()
{
// Create table with columns
Schema::create('users', function($table) {
$table->increments('id');
$table->string('username');
$table->string(Hash::make('password'));
$table->string('firstname');
$table->string('lastname');
$table->string('email')
$table->string('role');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
// Insert table to database
Schema::drop('users');
}
Try to check out the ".env" file in your root directory. These values are taken first. Yours are just the defaults if there are none given in the .env file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Your_Database_Name
DB_USERNAME=Your_UserName
DB_PASSWORD=Your_Password
Make changes in the database.php file in **config > local >database.php
rather than change config > database.php file.
hope it will work ;)
practically when you are using localhost server like xampp, you create the database manually "homestead", such errors are due to missing database arguments. 100% percent working test it and see
hit your cmd and type in:
create database homestead;
i got the same issue after creating laravel project. The authentication command run successfully.
php artisan make:auth
But when i try to register or login i got the same error. I run the following command
php artisan config:clear
then stopped laravel application and also my server. after restarting my laravel app and server everything was ok.
Tried all the solutions here but no work for me
This Worked on the first try:
If you are using the PHP's default web server (eg. php artisan serve) you need to restart your server after changing your .env file values.
I found this solution from here laravel.io (Read the solution of Byjml)

Laravel 5.3 - PHP Artisan Migration

I'm new to Laravel and just started with Laravel 5.3. While watching my tutorials and on my way to the Models video I encountered a problem with my "connection".
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using pas
sword: YES) (SQL: select * from information_schema.tables where table_schem
a = testing and table_name = migrations)
[PDOException]
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using pas
sword: YES)
Here's my database.php: (I tried configuring it before where the values are the same with my .env file but it still didn't work. I'm posting my current database.php file to get more insight/knowledge about configuration.)
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
And here's my .env file:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3307
DB_DATABASE=testing
DB_USERNAME=root
DB_PASSWORD=password
I don't use homestead and running only on my local server. I'm also using xampp on Windows 7.
Change you .env file like this --
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=testing
DB_USERNAME=root
DB_PASSWORD=
leave the password empty. Then try again.
Still error? then check the following --
If using xampp, wampp or any, dont forget to run mysql and apache.
A database with name 'testing' is created in phpmyadmin or any other
It looks like your username or password is not correct.
Are you sure you have .env.example renamed to .env?
This video may be helpful if you are trying to find information regarding artisan command https://www.youtube.com/watch?v=ix6CQ3rh8WY
There are several reasons for this error,
I hope this will save your time:
1- Confirm that the mysql and apache is running.
2- Setup (create) your database before the first deployment.
3- if you use vagrant, use vagrant IP instead of 127.0.0.1
4- Install latest version of PDO.

How do I connect my Laravel 5 app to mysql using MAMP?

I've gotten several different errors running php artisan migrate while trying different things. The most recent one is:
Access denied for user 'homestead'#'localhost' (using password: YES
This is my database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'port' => '3306'
],
and this is the .env
APP_ENV=local
APP_DEBUG=true
APP_KEY=halMyGPhfVZGrdpibFIwEE0wZipP5lqu
DB_HOST=localhost *updated
DB_DATABASE=forge *updated
DB_USERNAME=forge *updated
DB_PASSWORD= *updated
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
Try changing the following in your .env file or remove all the keys related to DB from .env file.
DB_HOST=localhost
DB_DATABASE=forge
DB_USERNAME=forge
DB_PASSWORD=
I had run into same issue, The problem is, the database.php file looks for the key DB_DATABASE etc. in the .env file. Since, that key is present in .env file, It doesn't consider the default value forge that you have provided in the statement env('DB_DATABASE', 'forge')
In plain english, "Look for the key DB_DATABASE in .env, if present, use that else use forge as default"
In your case, the keys are present in .env file hence, though you provide forge, it considers homestead that is defined in the .env file. And hence the error Access denied on 'homestead'#'localhost'
Hope I clarified the things.

connecting to database issue in laravel 5.1

I am new to laravel and while trying to connect to database in localhost using xammp iam getting an error that
SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES)
my connection code is that
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'hindutemples'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
but 'hindutemples' database exists i dnt know why it is trying to connect with homestead
Any help appreciated.
rename .env.example to .env. Then set appropriate values in .env files. It should work.
UPDATE
Laravel 5 stores settings in .env file. Laravel 5 comes with default .env.example file in a root folder. If you can not find it in project root it may have been hidden.
You need to rename .env.example or create new .env file inside project root and put app settings in it like given below,
APP_ENV=local
APP_DEBUG=true
APP_KEY=0SewSBZ47SfA4vRV6o8BeW74PMFwPpoX
DB_HOST=localhost
DB_DATABASE=hindutemples
DB_USERNAME=root
DB_PASSWORD=
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
env() returns values from .env files. if it could not find setting in .env file it returns false or if supplied default e.g. env('DB_USERNAME', 'homestead') it will return default value. in your case it can not find .env setting so it is returning homestead as a username and hence error.
Read More

Categories