Laravel acces denied for connecting db - php

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

Related

Error on using different MySQL server in Laravel

Thanks for your attension.
I have a Laravel 5.5 project and I want to use it with a MySQL server - being hosted on another server.
I can access MySQL server on the web browser.
https://xxx.xxx.xxx.xxx/phpmyadmin
And now I want to connect this MySQL server with a Laravel project on my local.
So I changed .env and config/database.php files like this:
.env file
DB_CONNECTION=mysql
DB_HOST=xxx.xxx.xxx.xxx (MySQL server host IP4 address like: 111.111.111.111)
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=user_name
DB_PASSWORD=password
config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'xxx.xxx.xxx.xxx'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'db_name'),
'username' => env('DB_USERNAME', 'user_name'),
'password' => env('DB_PASSWORD', 'password'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
],
After that I tried this:
php artisan config:clear
php artisan cache:clear
php artisan serve
But Laravel project shows me error like this:
"""
SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.\r\n
(SQL: select * from `table_name` where `one_filed` = some_value limit 1)
"""
Please help me.
Thanks.
Welcome to stackoverflow. There can be multiple issues, and you may find the correct thing from the docs or phpmyadmin config (if you have access to it):
MySQL configuration my not be on standard port of 3306
It might need SSL configuration to connect to.
The host might not be the same IP address as PHP My Admin as also stated in the comments. Even if the mysql server is accessible via the same host (very unlikely, but possible), the mysql user might have been configured with a special host

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.

issue on free live host but not on localhost - SQLSTATE[HY000] [1045] Access denied for user 'forge'#'

my laravel app with mysql database works fine on my local computer using WAMP. when I upload to a free live host to test I get this message when I try to register a user to the database:
Illuminate\Database\QueryException
SQLSTATE[HY000] [1045] Access denied for user 'forge'#'192.168.0.64' (using password: NO) (SQL: select count(*) as aggregate from users where email = jdoe#jdoe.com)
Basically anything that has to do with accessing the database on the live site does not work.
It's like mysql is an issue. Here is my settings:
config/database.php:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'sql201.epizy.com'),
'port' => env('DB_PORT', '3306'),
'database' => env('epiz_25791111_contact_db', 'forge'),
'username' => env('epiz_25791111', 'forge'),
'password' => env('mypassword1', ''),
'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'),
]) : [],
],
.env file
DB_CONNECTION=mysql
DB_HOST=sql201.epizy.com
DB_PORT=3306
DB_DATABASE=epiz_25791111_contact_db
DB_USERNAME=epiz_25791111
DB_PASSWORD=mypassword1
Your hoster does not allow connection from external sources.
Source
#Kevind at your mysql server's mysql prompt enter the following:
CREATE USER 'forge'#'192.168.0.64' IDENTIFIED BY 'PASSWORD';
GRANT ALL PRIVILEGES ON *.* TO 'forge'#'192.168.0.64' WITH GRANT OPTION;
FLUSH PRIVILEGES;
be sure to change PASSWORD to the password you want.
Folks, I figured it out. I was using the free host password instead of the Vpanel password which are different. The database features are now accessible. Apparently this free host decides to copy the random password of the vpanel to the mysql password also. I'm not sure if I clarified but the database was in the Vpanel not an external source. Thanks a lot though.

SQLSTATE[HY000] [1049] Unknown database 'ictinder'

So for some reason I get this error in my browser when using Laravel. I feel like I got everything set up correctly:
This is my .env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ictinder
DB_USERNAME=root
DB_PASSWORD=secret
This is my database.php file:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'ictinder'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'secret'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
This is a screenshots from my phpMyAdmin, it's Dutch so if you don't understand something, let me know
This is what the error looks like in my browser
Thank you for taking your time to help out, or at least read my question!
EDIT 1:
I already tried php artisan cache:clear and php artisan config:clear, both didn't work. I can use php artisan migrate.
If you are using vagrant to run your project. you have to change your db port into 33060 and change your db username into homestead
if you are running your program using php artisan serve, your db port should be 3306 into your db username
see the docs laravel homestead
Hope this can help you
What you need to do in your case is to remove the ictinder from your database and also from your .eve file. then recreate the ictinder again from where it was been remove, it's adventurous to leave the empty DB_PASSWORD like this DB_PASSWORD=, if your MySQL does not have a password, also remember that
If you are using the PHP's default web server (e.g. php artisan serve) you need to restart your server after changing your .env file values.

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.

Categories