Access denied for user 'root'#'localhost' - Laravel - php

I have an application with Laravel 4, that runs in localhost correctly,
but when I uploaded it in my host I received the error .
app>config>database.php file is:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'forum',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
And bootstrap>start.php is:
$env = $app->detectEnvironment(array(
'local' => array('homestead'),
));

This worked for me:
php artisan config:clear
Even though I changed the config details in the .env file, I was getting the Access denied error. Running the above command will clear configuration cache file and hence laravel will read the fresh data from the .env file.

You must find out the credentials of Database host, Database Name , Database Username and Database Password . (If any prefix for tables too) and then replace it with the current credentials.
Use the concept of environments and store values in ENV variables :
http://laravel.com/docs/4.2/configuration
You can store Env Variables as array in .{ENV_NAME}.env.php and access those variables as $_ENV['variable_name'].

Maybe you should add double-quotes for password in env:
# Config: database
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=db_user
DB_PASSWORD="db_password"
and
php artisan config:clear

Check your mysql database is running perfectly or not. Then check you db user root in mysql with no password and restart your server.

Laravel Access denied for user 'user'#'localhost' (using password: YES)
In .env I changed add ' ' in line 'user' and 'password':
DB_USERNAME=user
DB_PASSWORD=password
to:
DB_USERNAME='user'
DB_PASSWORD='password'
Worked for me!
Run -> php artisan optimize:clear
Good luck <3

Related

Laravel: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

after installing a new laravel app 5.7 and trying to migrate I get this error:
Illuminate\Database\QueryException : SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client (SQL: select * from information_schema.tables where table_schema = xxx_db and table_name = migrations)
at
C:\xampp\htdocs\xxxxx\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll
format the error 661| // message to include the bindings with SQL,
which will make this exception a 662| // lot more helpful to the
developer instead of just the database's errors. 663| catch (Exception
$e) {
664| throw new QueryException( 665| $query,
$this->prepareBindings($bindings), $e 666| ); 667| } 668|
Exception trace:
1 PDOException::("PDO::__construct(): The server requested
authentication method unknown to the client [caching_sha2_password]")
C:\xampp\htdocs\xxxxx\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
2 PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=xxx_db ",
"root", "**********", [])
C:\xampp\htdocs\xxxxx\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
Please use the argument -v to see more details.
This query solved my problem.
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'root#123';
Enter to your mysql as root and run this query
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY '1234';
You can change 1234 to be your password
Run these commands
php artisan config:clear
php artisan migrate
Note: this worked for me.
Re installed MySQL choosing Legacy Authentication Method as shown in iamge attached SQL Authentication method
Database parameters in .env as follows
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=database_username
DB_PASSWORD=database_password(if any)
Database parameters in config/database.php
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'database_name'),
'username' => env('DB_USERNAME', 'database_username'),
'password' => env('DB_PASSWORD', 'database_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'),
]) : [],
],
Run php artisan migrate from the projects terminal
By Providing DB_SOCKET in .env file this issue can be resolved like this :
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=my_db
DB_USERNAME=root
DB_PASSWORD=
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
Please Check Server localhost Port on phpmyadmin and .env file like (3306,3307,8889)
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3307
DB_DATABASE=mydb
DB_USERNAME=root
DB_PASSWORD=masterpass
Solution is here with 2 step:
Step 1. You have to edit /etc/mysql/my.cnf file and append this setting in mysqld section:
[mysqld]
default_authentication_plugin= mysql_native_password
Step 2. Then run following mysql command:
CREATE USER 'new_root'#'localhost' IDENTIFIED WITH mysql_native_password BY '123';
GRANT ALL PRIVILEGES ON *.* TO 'new_root'#'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
It is better if you restart your mysql service.
sudo systemctl restart mysql
This problem happens when you have install mysql8 and your code compatible with mysql5
You can run Mysql installer - Community (if you are in windows) and then reconfigure mysql server to use legacy authentication method. it should be solve your problem without pain.
Unfortunetly you changes your mysql user or password. If you user/password changed then go to .env file and change your phpmyadmin user and password. Give your database name
DB_DATABASE=hrms
DB_USERNAME=username
DB_PASSWORD=password
If you are getting through this kind of issue like me, avoid losing your time
My scenario: I had to start an existing Laravel5.1 application on PHP5.6. Docker didn't helped me, so I had to create a virtual machine for that.
This issue happens because of the authentication driver for MySQL. On version 8, it will use caching_sha2_password (and Laravel 5 doesn't know what to do with this.
One way to correct this is to downgrade your MySQL version back to 5.7. I've tried a lot, but my apt crashed after "forcing" it to downloading 5.7. If you want to try, heres a discussion for it.
95% of other discussions on StackOverflow and other forums will say to you just ALTER your MySQL user for the mysql_native_password. For some reason, it DOES NOT work. Even after restarting mysql services and flushing it's privileges, doesn't work. I've tried this kind of solutions for hours.
Luckily we have a hero in THIS discussion and is #Amir Hosseinzadeh. You have to create a new user AFTER changing the MySQL driver configuration. Just follow his/her solution on top and it will work.
Go to your .env file and make sure DB_CONNECTION=mysqland DB connections are correct.

Laravel seems to randomly use the default database.php configuration instead of .env ones

EDIT : Thanks to #PedroFaria99, clear config cache solved the problem, but if anybody want to bring an explanation about the randomness aspect feel free.
I have an issue with my laravel 5.5 local installation (production environnement isn't impacted). Here Laravel is used as an API and serves a client-sided VueJS application.
Sometimes (randomly), my laravel is returning 500 error to my client. It can happens on various routes, never the same one, after 1 to 10 successives HTTP request or not and when I check the storage
[2018-03-09 13:44:08] production.ERROR: PDOException: SQLSTATE[HY000] [1045] Access Denied for user: 'forge'#'#localhost' (password: NO) in [...] Illuminate\Database\Connectors\Connector.php:119
However, my .env file is settup, and my database.php is using env() with default parameters are "forge" and "localhost". So I tried to change this parameter to "test", and the next 500 errors was same but with "test" instead of "forge".
I'm very confused, since this error doesn't happen systematicly.
.env file
APP_ENV=local
APP_DEBUG=true
DB_HOST=localhost
DB_DATABASE=mydatabase
DB_USERNAME=root
DB_PASSWORD=
database.php
...
'default' => env('DB_CONNECTION', 'mysql'),
...
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'test'),
'username' => env('DB_USERNAME', 'test'),
'password' => env('DB_PASSWORD', '')
...
Try
php artisan cache:clear
php artisan config:cache
It happens because laravel saves env values in its cache and if your config is not cached it will take the configuaration that are saved in the database.php file. You can not directly access env values on runtime.
I hate to revive a dead thread but after some soul searching I found the issue might just be php's lack of thread safety : https://github.com/laravel/framework/issues/28571 and the great writeup here : https://mattallan.me/posts/how-php-environment-variables-actually-work/

Laravel Access denied for user 'root'#'localhost' (using password: YES) in laravel 4.2

I have old project which built using Laravel 4.2.I am getting following error
PDOException (1045)
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES)
I have googled and tried every think but i couldn't able to fix it
.env file
APP_KEY=az9tq5VHQCV9g5m2CsgY89jtijrCMgEA
DB_HOST=localhost
DB_DATABASE=billing
DB_USERNAME=root
DB_PASSWORD=1234
database.php
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'billing',
'username' => 'root',
'password' => '1234',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Can any one guide me where m doing wrong ?
Note: Before asking question i tried by updating composer update as well as most of the stackoverflow answers.
Updated
I have tested this connection by creating php file
<?php
$servername = "localhost";
$username = "root";
$password = "1234";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
i will get Connected successfully message
My error was Laravel Access denied for user 'root'#'localhost' (using password: NO)
and I only got this error at my host site.
In .env I changed:
DB_USERNAME=user
DB_PASSWORD=password
to:
DB_USERNAME='user'
DB_PASSWORD='password'
Worked for me!
DB_HOST="localhost" worked for me.
And to be safe, wrap .env variables in double quotes to avoid ENV errors:
DB_USERNAME="root"
DB_PASSWORD="#Password"
These will give errors: DB_PASSWORD=#Password, DB_PASSWORD=I have spaces
# is a start of a comment and is exactly the same as //
Run php artisan serve after configure .env, not before.
Laravel 4.x doesn't even support ENV files. You just have to see whether settings in ./config/[env name]/database.php are correct.
I just ran into the same problem on a new Laravel install.
The problem was that I was connecting to my ubuntu localhost mysql server instead of to the vagrant box's mysql server that was on it's own ip address '192.168.10.10'.
I changed over to that and all worked a charm :)
I'm test in Laravel 5.6.33 and i see this error: "Access denied for user 'root'#'localhost'", but the username and password is correct.
In this version i think of this is a BUG, i change DB_HOST, from 127.0.0.1 to localhost and works!
DB_HOST=127.0.0.1 (before)
DB_HOST=localhost (after)
Try In .ENV
APP_ENV=local
DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=billing
DB_USERNAME=root
DB_PASSWORD=1234
And Delete Cache Files From Root/boostrap/chache/files
and Run The App
The error says it all, Laravel can't connect to a DB. Check priveleges and make sure the DB exists. Also, try to connect to a DB with a client, like MySQL Workbench using same login and password. This will give a hint about what you can do to fix this. If you can't do this, it's not a Laravel issue.
Try This
.env file
APP_ENV=local
DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=billing
DB_USERNAME=root
DB_PASSWORD=1234
config/database.php
default' => env('DB_CONNECTION', 'mysql'),
'mysql' => array(
'driver' => 'mysql',
'host' => env('DB_HOST','localhost'),
'database' => env('DB_DATABASE','billing'),
'username' => env('DB_USERNAME','root'),
'password' => env('DB_PASSWORD', '1234'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
Else checkout the .env file and check the connection
I got the same problem. You should add "" to the DB_USERNAME and DB_PASSWORD
So,
Step 1: DB_USERNAME="root" DB_PASSWORD="1234"
Step 2: Run "php artisan config:clear"
Step 3: Run "php artisan cache:clear"
please delete config file located at bootstrap/cache/config.php
it will be autogenerated when you run php artisan config:cache command
it will work
I faced the same problem when learning laravel using homestead, the problem happened when I try to open a page. It tried to connect to database but got rejected, although I can connect to the database using mysql command line.
The problem was I had wrong assumption that since homestead forward web server request from guest to host, the same thing is also same with myqsl, but apparently it is not.
Homestead has it's own mysql service, so my problem was fixed by:
- homestead ssh into the guest machine
- mysql -u root -p to connect to mysql command line, default password is "secret"
- create the database, but you need to change your password first, just change into the same password as in mysql root's password in your host machine for convenience
- exit the mysql cli and
- php artisan migrate to create the tables
- refresh your browser, now it should be able to connect to your db
Access denied for user 'root'#'localhost' (using password: YES)
The error was coming? The possible reason is your database
connection.If the database is not connect then throw the error.
So Check the database releted all things and If all things is correct.
If not solved then change
DB_HOST=127.0.0.1 TO DB_HOST=localhost
Make sure your .env is using the correct port number. If you're using phpMyAdmin this is how is setup:
MariaDB defaults to 3306
and
MySQL defaults to 3308
So change your .env to use port 3308 for MySQL. I hope this helps!
I changed this line DB_HOST=127.0.0.1 in .env to DB_HOST=localhost and it worked for me.
I had a similar problem, the password is the major issue in such communication, see my picture below, i solved it after changing password, php artisan serve and then reloading my view. It worked.
please login to mysql or phpmyadmin and change your password.
enter image description here
It sounds kind of obvious, but check the credentials of the database.
For me something was wrong in the env file. So I created a fresh installation, copied the env file from there and updated the credentials and it worked.
If you're using docker and still having issues with user access denied issues, you may want to try starting over fresh with the DB, which is was what it took for me.
First, I figured out where my DB data volumes were being stored in my docker-compose.yml file, which ended up being here: [app root dir]/docker/db/data/
db:
container_name: ${APP_NAME}_db
image: mysql:8.0
ports:
- 33060:3306
volumes:
- ./docker/db/data:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
- MYSQL_DATABASE=${DB_DATABASE}
Then I ran this in the app root directory to remove all the docker containers.
docker-compose rm -v
Next I navigated to the DB data files and removed all of them:
cd ~/[app root dir]/API/docker/db/data
sudo rm -rf ./*
Finally, I started up the containers and was then able to connect with my database user credentials.
docker-compose up --build --force-recreate --no-deps
REFERENCE: https://github.com/docker-library/mysql/issues/51
If the username and password are correct and the problem still persists, try clearing the cache using the following method
Route::get('/clear-cache', function () {
Artisan::call('cache:clear');
Artisan::call('route:clear');`
});
Laravel 6 just set secret instead of empty space.
DB_PASSWORD=secret

Laravel 5 + PostgreSQL: "Database [postgres] not configured." Error

I'm trying to get started with Laravel + PostgreSQL and been following the database tutorial.
Unfortunately, after updating the database configuration file and running php artisan migrate, the following error appears:
[InvalidArgumentException]
Database [postgres] not configured.
What puzzles me is that I didn't specify the "postgres" database in the configuration, but another database I set through cPanel, say "example_database".
Here's some relevant parts of my /config/database.php configuration:
'default' => env('DB_CONNECTION', 'postgres')
And inside the connections array of the same file:
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'example_database'), // This seems to be ignored
'username' => env('DB_USERNAME', 'example_username'),
'password' => env('DB_PASSWORD', 'example_password'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public'
],
The actual database credentials I'm using are working perfectly on my SQL Workbench client, so this seems to be a Laravel config problem. Any ideas? I have searched around for at least an hour to no avail.
You have to enter your configuration in the .env file.
The configuration you made will only be loaded if they are not already defined in .env
You need to use pgsql instead of postgres.
DB_CONNECTION=pgsql
DB_HOST=localhost
DB_DATABASE=DB_NAME
DB_USERNAME=USER
DB_PASSWORD=PW
Laravel sometimes caches your configurations. If you run into this problem while everything looks alright try running
php artisan config:cache
I know this is an old question and it already has an answer; however, here is an small explanation why:
If you check your database.php in the config directory, you will see that you have few connections types, including pgsql. So, the key have to match to the DB_CONNECTION in .env file. You can definitely replace pqsql connection key with postgres, and it will work on the same way.
However, I would recommend replacing the value DB_CONNECTION, instead of modifying the config.
DB_CONNECTION=pgsql

Laravel 4 artisan migration unable to connect to remote mysql db [HY000] [1450] Access denied for user 'myuser'#'mypc' (using password: YES)

EDIT: I finally realised artisan is trying to connect to my machine instead of the remote database host specified in config. I assumed it would retrieve the hosts address along with the username and password. I'll try to specify the host's address on commandline and post back.
EDIT 2: I found the problem. Technical support for the host mislead me to my first edit. The problem was a mismatch in the password between a '1' and an 'l'. Sorry to have wasted your time.
I'm developing a Laravel 4 web app and I'm trying to execute migrations from sentry into a remote mysql database. I have been able to connect via phpmyadmin using the same credentials stored into laravel without any hassle, but artisan won't work.
Here's artisan command and output:
$ php artisan migrate --package=cartalyst/sentry
[PDOException]
SQLSTATE[HY000] [1045] Access denied for user 'myuser'#'mypc' (using password: YES)
migrate [--bench[="..."]] [--database[="..."]] [--path[="..."]] [--package[="..."]] [-- pretend] [--seed]
Laravel connection config:
'default' => 'mysql',
[...]
'mysql' => array(
'driver' => 'mysql',
'host' => 'hostname.example.com',
'database' => 'mydb',
'username' => 'myuser',
'password' => 'supersecretpassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
I don't have mysql installed locally, just apache, php and phpmyadmin. My box is archlinux and I don't have access to the remote host.
Any ideas?
You need to grant rights to that user on that host (this is the machine from where you connect):
GRANT ALL ON mydb.* TO myuser#mypc IDENTIFIED by 'supersecretpassword';

Categories