laravel using .env but not using config in config\database.php? - php

I'm learning laravel. I newly created a project and trying to create model, i set up the db config database.php and type php artisan migrate, but there is a error message
C:\xampp\htdocs\l1\blog>php artisan migrate
[PDOException]
SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (usin
g password: YES)
I see it is using .env config but not using config in my config.php, why?

You can change the .env file to suit your database config settings as follows
DB_HOST=localhost
DB_MAIN=MYDB
DB_USERNAME=root
DB_PASSWORD=root
Change the values of those keys according to your database connection. And place them in .env file.
restart your server once and proceed.
Other wise you can directly place those values in database.php file by replacing something like env('DB_USERNAME'),env('DB_PASSWORD') with direct values in either single or double quotes as follows
'main' => [
'driver' => 'mysql',
'host' => '',
'database' => 'mydb',
'username' => 'root',
'password' => 'root123',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
The code actually present in database.php file can be something as follows
'main' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_MAIN', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
You can simply replace these values with above mentioned values.

Related

Change database setup now having error even after reverting to orginal setup (Laravel 5)

I tried to experiment with my Laravel 5 database.php, originally
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', 'forge'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
then I changed to
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'mydb',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
now I am having this error even if I revert to the original setup?
#1045 - Access denied for user 'root'#'localhost' (using password: NO)
env file contains
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
In the first part of your config/database.php there's should be something like:
'default' => env('DB_CONNECTION', 'mysql'),
make sure you didn't change that.
Anyway, as explained here you should use a local .env file for your database configuration.

laravel5 | Homestead | Auth Registration and login is not connect with Database

I'm using laravel5 with Homestead (homestead.app).
At beginning i use localhost, that time laravel Auth working fine.
But move to homestead, its not working(cant connect with DB).
Any configurations need to change ?
MYSQL : Connection
DB_HOST=localhost
DB_DATABASE=laravel5
DB_USERNAME=homestead
DB_PASSWORD=secret
First ensure you really have .env file uploaded to the host.
The .env file is used by config/database.php file.
Ensure that it contains a line
'default' => env('DB_CONNECTION', 'mysql'),
and these lines as well:
'connections' => [
//...
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', ''),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
//...
],

Laravel 5 with homestead having unknown database

Hi I am using Laravel 5 with homestead. I am experiencing homestead first time so facing problem. I have created a database 'myDb' and imported data into this. I have installed vagrant and homestead, cloned my code and configured homestead.yaml file accordingly. I have set up my .env. When I try to run my project is is giving me unknown database myDb error. I can see my database in my phpmyadmin but I do not know why it is giving me this error. My .env file looks like
DB_HOST=localhost
DB_DATABASE=myDb
DB_USERNAME=homestead
DB_PASSWORD=secret
And I have same settings in my config/database.php too.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'myDb'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
Its really making me mad. What Am I doing wrong? I am getting following
in Connector.php line 55
at PDO->__construct('mysql:host=localhost;dbname=myDb', 'homestead', 'secret', array('0', '2', '0', false, '0')) in Connector.php line 55
at Connector->createConnection('mysql:host=localhost;dbname=myDb', array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'myDb', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, '0')) in MySqlConnector.php line 22
at MySqlConnector->connect(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'myDb', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) in ConnectionFactory.php line 60
at ConnectionFactory->createSingleConnection(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'myDb', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) in ConnectionFactory.php line 49
One common mistake that you might be making is that you're probably accessing MySQL server on your localhost while you should remember that Homestead is a complete virtual dev. environment. It has it's own Web Server, MySQL server and etc.
To see if you have "myDB" on the Homestead MySQL server as well, try accessing homestead via the following command.
homestead ssh
Once done that, try opening the mysql console via the mysql command.
mysql -u username -p
And after that list all your databases using
show databases
If you can't see "myDb" database there, than you should try connecting to Homestead MySQL server using Navicat or MySQL Workbench and move your database from your local MySQL server to the Homestead MySQL server.
That would probably fix your problem.
If you are migrating your database, check if you are migrating the same database name inside your Homestead.yaml file and the database name in your .env file.
Inside Homestead.yaml file:
databases:
- laravel
Inside your .env file:
DB_DATABASE=laravel

Laravel 5 database issue

I've installed laravel 5 successfully by using this command:
composer create-project laravel/laravel test-laravel-5-project dev-develop --prefer-dist
I even verified the version of installed laravel by using php artisan -V command. The output was
Laravel Framework version 5.0-dev
Then I went to app/config/database.php, gave dafault db as mysql and gave configurations as
'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'Logintestfive'), 'username'=> env('DB_USERNAME', 'root'), 'password'=> env('DB_PASSWORD', 'manasa'), 'charset'=> 'utf-8', 'collation'=> 'utf-8_unicode_ci', prefix=> '', 'strict'=> false, ]
Then I went to localhost:8000/auth/register and filled up the form and submitted the data and this is the error which I got:
PDOException in Connector.php line 47: SQLSTATE[28000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES)
But I've neither used laravel homestead for installng laravel 5 in my system nor used vagrant to set up laravel homestead. And it tells me like this:
in Connector.php line 47
at PDO->__construct('mysql:host=localhost;dbname=homestead', 'homestead', 'secret', array('0', '2', '0', false, '0')) in Connector.php line 47
at Connector->createConnection('mysql:host=localhost;dbname=homestead', array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, '0')) in MySqlConnector.php line 20
at MySqlConnector->connect(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) in compiled.php line 10545
at ConnectionFactory->createSingleConnection(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) in compiled.php line 10541
at ConnectionFactory->make(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false), 'mysql') in compiled.php line 10459
How can I fix those issues?
I got a similar problem. I started my server using php artisan serve command, and edited the .env file and refreshed the web-page which did not reflect any changes!
I fixed it by stopping the webserver, editing the .env.php file and restarting the webserver!
So I guess it's a caching-issue of the .env.php file.
Laravel 5.1.10
As the answers already given above by Ganesh and Yordi, But i'm combining the both of them into single one.
Issue:
When i try to create a database in phpmyAdmin and want to create schema/table through Laravel Schema builder service i got the following errors:
PDOException in Connector.php line 50: SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES)
in Connector.php line 50
at PDO->__construct('mysql:host=localhost;dbname=homestead', 'homestead', 'secret', array('0', '2', '0', false, '0'))
in Connector.php line 50
at Connector->createConnection('mysql:host=localhost;dbname=homestead', array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, '0')) in MySqlConnector.php line 22
at MySqlConnector->connect(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) .............
Solution 1:
Then I realised that the issues were in the env file, I forgot to change the values in the env file. The .env file is located in the laravel 5 root directory
open the file and change the following values with your ones
DB_DATABASE = localhost,
DB_USERNAME = databse-username,
DB_PASSWORD = databse-user-password
Solution 2:
Or change in the database.php file without env() as Ganesh syas:
// instead of this block
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'your database name'),
'username' => env('DB_USERNAME', 'your database username'),
'password' => env('DB_PASSWORD', 'your database password'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
// use this one
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'your database name',
'username' => 'your database username',
'password' => 'your database password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
The both solutions worked for me.
Laravel is using the variables contained in your .env file.
From: http://laravel.com/docs/master/configuration
It is often helpful to have different configuration values based on the environment the application is running in. For example, you may wish to use a different cache driver locally than you do on your production server. It's easy using environment based configuration.
Laravel utilizes the DotEnv PHP library by Vance Lucas. In a fresh Laravel installation, the root directory of your application will contain a .env.example file. If you install Laravel via Composer, this file will automatically be renamed to .env. Otherwise, you should rename the file manually.
All of the variables listed in this file will be loaded into the $_ENV PHP super-global when your application receives a request. You may use the env helper to retrieve values from these variables. In fact, if you review the Laravel configuration files, you will notice several of the options already using this helper!
Feel free to modify your environment variables as needed for your own local server, as well as your production environment. However, your .env file should not be committed to your application's source control, since each developer / server using your application could require a different environment configuration.
If you are developing with a team, you may wish to continue including a .env.example file with your application. By putting place-holder values in the example configuration file, other developers on your team can clearly see which environment variables are needed to run your application.
The default .env file looks something like:
APP_ENV=local
APP_DEBUG=true
APP_KEY=YOUR_KEY_HERE
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
CACHE_DRIVER=file
SESSION_DRIVER=file
I got the same problem. After I use the command "php artisan config:clear" to clear the old configuration cache file, it can work. Maybe you can try it.
By the way, my English is not good, hope you can understand.
Laravel is using .env file so change the .env file or change in the database.php file without env()
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'your database name',
'username' => 'your database username',
'password' => 'your database password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
In Config->database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'DBName'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
In .env file :
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=DBName
DB_USERNAME=root
DB_PASSWORD=
Close the laravel application server and restart again for clearing the cache.
php artisan serve
Or you can do :
php artisan config:clear
This will clear the cache in config files.
Now it will definitely work! Cheers!
Laravel config/database.php uses the .env file for database name,username asnd password, Please change your .env file
DB_HOST=localhost
DB_DATABASE=my_project
DB_USERNAME=root
DB_PASSWORD=myproject
After changing this run the below command:
composer dump-autoload
then run your project
php artisan serve
I had a very similar issue and didn't know where to look. I cleared the cache, restarted server, etc., no joy. Until I found it...
Before I reverted to a previous version on the Git repo, I used to have my session driver set to use the database! Silly thing, but it's not necessarily the database connection what you need to look at but instead where you are invoking the database to be used at the first place.
I hope that somebody who'll end up on this page the way I did will find this helpful.
i have same issui, edit file .env on root folder and change database config DB_DATABASE DB_USERNAME DB_PASSWORD
Inside your laravel eg. c:/www/laravel, create a new directory called 'local' inside app/config.Copy database.php (app/config/database.php).This works for me
Yes i had the same issues where everything went well right from migration installation to php artisan migrate. But when i tried to create a user the access denied.
Solution i found was to close the ctrl + d to terminate the server and restart. I worked... :)
you can restart your server
I think is a catch error in Laravel so run your server again with "php artisan serve" and refresh your website page IT's OK.
for me
sudo service apache2 reload
did the job

In Laravel 4 - SQLSTATE[HY000] [1045] Access denied for user 'forge'#'localhost' (using password: NO)

I get this error in Laravel 4. My config/database.php file says,
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'my-site-name',
'username' => 'my-site-name',
'password' => 'my-password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Earlier I changed my environment to production, then now to local.
If you changed your environment to local, you should check if you have a database.php file located in your \app\config\local\ folder. Your environment specific configurations override the global ones from your \app\config\ folder.
Seeing as your connection is trying to use the forge username for localhost, it seems like the configuration you just posted is being overwritten by another config file.
In database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'your_db_name'),
'username' => env('DB_USERNAME', 'your_db_user_name'),
'password' => env('DB_PASSWORD', 'your_db_password'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
In .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_db_name
DB_USERNAME=your_db_user_name
DB_PASSWORD=your_db_password
hope, this may work out. Thanks.

Categories