Access denied for user 'homestead'#'localhost' in Laravel - php

newbie here. I try to run:
php artisan migrate
But I get these errors:
In Connection.php line 664:
SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (usin
g password: YES) (SQL: select * from information_schema.tables where table_
schema = homestead and table_name = migrations)
In Connector.php line 67:
SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (usin
g password: YES)
This is what is contained in my database.php and .env files respectively:
'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', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
My DB name is homestead and host is localhost yet it's still giving error messages.
And this is what it looks like in myPhpAdmin user accounts overview page:
How would I fix this?

If you are using homestead without any changes then the default host for the homestead machine is 192.168.10.10.
Change
DB_HOST=localhost
to
DB_HOST=192.168.10.10
Link to the Docs

This means that either your database username or password is wrong. If you are on windows the default DB_USERNAME is root and password is empty. Try correcting your configuration in .env file.

Related

Keep on getting a php artisan migrate error on laravel while using a MAMP server?

I am using a MAMP server to host my laravel project. I am trying to push my database migrations table which I have created to the MAMP phpmyadmin database hosted on the localhost 8888 server.
However everytime I use the command , php artisan migrate I get this error:
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES) (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')
I have tried everything to change stuff in the .env file and the database.php file but nothing seems to be changing.
This is what my .env file looks like(only relevant parts):
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:H5A9PbNQk0we6UJnNzSZ68Y22B88SK6wCEq2wachQfE=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME="root"
DB_PASSWORD="temp"
UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
This is what my database.php file looks like:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', 3306),
'database' => env('DB_DATABASE', 'laravel'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'temp'),
'unix_socket' => env( 'DB_SOCKET','/Applications/MAMP/tmp/mysql/mysql.sock'),
'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'),
]) : [],
],
Here is a picture of what the user accounts look like, what I have available:
Make sure u use the right username and password in phpmyadmin
And replace them in .env file and config/database.php file
Try running php artisan config:clear and also php artisan cache:clear.
If that won't help try and login to the database using command line or bash if the credentials work.

Laravel acces denied for connecting db

I have started laravel recently, and I get the error
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO) (SQL: select count(*) as aggregate from `users` where `email` = ***)
on a clean freshly made project when I try to register with the default auth system. php artisan migrate works well, and I can connect to the database through mysql workbrench with the same credentials as in the .env file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=udemy-cms
DB_USERNAME=root
DB_PASSWORD=
I am using homestead with laravel, and mysql workbrench. I tried php artisan config:clear but did
not help.
database.php is default
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'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'),
]) : [],
Thanks in advance for any help.
First I would suggest to check if you can actually connect to your mysql using your current credentials. You should be able to connect to it in your command line like this:
mysql -uroot -p
This basically tries to connect to mysql using root as a user with no password. If that does not work your user does not exist or your password is wrong.
If it works you should run following, to see if your database udemy-cms actually exists:
show databases;
If it does not show your database you need to create it.
CREATE DATABASE udemy-cms;
It is also possible that your user does not have enough permissions, in this case you need to grant your current user enough permissions.
Make sure that you updated the below values in your .env file
DB_CONNECTION = mysql
DB_HOST = "127.0.0.1" //ip or localhost
DB_PORT = "3306" //make sure its true one
DB_DATABASE = "your_database_name"
DB_USERNAME = "root" //is as default if you didnt create a new
DB_PASSWORD = "password" //or empty
Once updated .env file, and run command php artisan config:cache then restart the server of your project and serve the project again using the php artisan serve command

Laravel API can't connect to db

I'm trying to build an restful API.
When I make a call to one of my endpoints, I get following error message:
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO) (SQL: select * from users)
The wierd thing is why this error is showing up when I try to access the endpoint, but when I use db:seed, the database is populated with dummy-data and I can use migrations also to create the tables.
The user that is connecting to the db has full privileges.
Can anyone please help me out?
sometimes .env archive not reload for change... maybe you need run command php artisan config:cache
This is my settings in the .env for the database:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=root
DB_PASSWORD=
And this are the ones from database.php:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
But as i understand it both the migrations, seeds and the api goes thru .env.

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.

ampps/Laravel5: error:Access denied for user 'root'#'localhost'

I install laravel 5.2 on Ampps (windows 10) with this tutorial.
it works.
But Now I want connect to mysql.
I create a 'test1' DB and change these two files:
config/database.php
mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'test1'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
.env
DB_HOST=localhost
DB_DATABASE=test1
DB_USERNAME=root
DB_PASSWORD=
After that I restart apache.
then in CMS I run this code:
F:\Ammps\www\quickstart>php artisan migrate
But I get this error:
error SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost'
I think I miss a step.
can please help me?
According to the Ampps FAQ you need the default mysql password:
1. Default MySQL root password is "mysql".
So add that to your .env file like this:
DB_HOST=localhost
DB_DATABASE=test1
DB_USERNAME=root
DB_PASSWORD=mysql

Categories