I have my project ready and I have just uploaded to a shared hosting server. Everything works perfectly fine on my local machine but the page is displaying the error:
"SQLSTATE[28000] [1045] Access denied for user 'root'#'localhost' (using password: NO)"
I have checked my password and username and they are both correct. The server requirements are also as I already have 2 other Laravel sites running with the same host.
I have done php artisan config:cache php artisan cache:clear But no headway.
What could be causing this and how do I get it solved? I earnestly need a solutions as this has kept me sleepless for nights now.
try to with these 3 artisan command.
1) php artisan config:clear
2) php artisan cache:clear
3) php artisan config:cache
different config? it's saying your password is empty ;)
"I have checked my password and username"
"(using password: NO)"
app/config/database.php:
'mysql' => array(
'read' => array(
'host' => '192.168.1.1',
),
'write' => array(
'host' => '196.168.1.2'
),
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '<insert password here?>',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Open terminal and type
sudo mysql -u root -p
It will prompt you in mysql, here you can fire any mysql commands.
Use mysql table for change table type, so we can use empty password. Bellow is command for it
USE mysql;
Now we change type of table by following command
UPDATE user SET plugin='mysql_native_password' WHERE User='root';
now we have to flush the privileges, because we have used UPDATE. If you use INSERT, UPDATE or DELETE on grant tables directly you need use FLUSH PRIVILEGES in order to reload the grant tables.
FLUSH PRIVILEGES;
now exit from mysql by following command
exit;
now restart mysql server by following command
service mysql restart
Hope this may help
Thank you.
Anytime you deploy a project, you should typically run the php artisan config:cache command as part of your production deployment routine. I suggest you
Remove the configuration cache file
Flush the application cache
Create a cache file for faster configuration loading
To do this, run the following Artisan commands on your command line
php artisan config:clear
php artisan cache:clear
php artisan config:cache
Where you don't have access to the command line on your server, you can programmatically execute commands by adding the following to your routes:
Route::get('/clear-cache', function() {
$exitCode = Artisan::call('config:clear');
$exitCode = Artisan::call('cache:clear');
$exitCode = Artisan::call('config:cache');
return 'DONE'; //Return anything
});
And then call the clear-cache route from your browser.
Related
Edit:
php artisan config:cache work nice, but now I got other problem.
the URL giving me Error 500
I upload a project to a new subdomain area after I changed .env file
when I open URL I still got an error with the old database and user
I tried to check online with the .env file but - I don't know where he stored this database, I tried to see where is this name with ctrl+f but - nothing found
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=faceterc_hot
DB_USERNAME=faceterc_hot
DB_PASSWORD=testpro
I expect to get the same error maybe but not with the old database name.
and this gives me a indicate that maybe the file or something not changed or he using the other details from I don't know where
Good, practice for, If you change in a .env file first restart the server using below command.
php artisan serve
then after run below more command for clear old cache and config.
composer dump-autoload
php artisan config:cache
php artisan config:clear
You can simply do a
php artisan config:cache
In case you are on a shared hosting, you can change the values in config/database.php to only use the set values in your .env. Setting it like this will make sure, it only use the .env values.
mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
php artisan config:clear Solve the problem with DATABASE error
just ran :
php artisan config:clear
with ssh at the main folder.
Just execute
php artisan config:cache
as the laravel cache's the environment files rather than accessing the data from it on every request.
It's always a good practice to run these couple of commands when you change any environment related variable:
php artisan config:clear
php artisan cache:clear
Execute these commands:
php artisan config:cache
php artisan route:cache
php artisan optimize
That will clear your env cache and it should do.
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.
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
When I ssh into my local vagrant machine I can execute all artisan commands. Hower outside of it, any commands that needs database access such as artisan migrate gives Access denied for user 'root'#'localhost'. If it is possible, how can I use artisan without having to log in to the vagrant machine?
I would also like to do for example Artisian::call('migrate') for example during testing. But that gives the same error.
The reason for the access denied error is that mysql by default limits access to databases to the local machine. It is possible to open up for remote connections as described here, but it wouldn't be a good idea for production environments.
A better way is to add aliases for the commands you want as described here.
For doing calls to artisan etc in code, Laravel has a built in way of running commands on remote servers. First add connection information to the app/config/remote.php file. For vagrant it should look something like this:
'connections' => array(
'production' => array(
'host' => 'localhost',
'username' => 'vagrant',
'password' => 'vagrant',
'key' => '',
'keyphrase' => '',
'root' => '/vagrant',
),
),
Then execute artisan migrate like this:
SSH::run(array('cd /vagrant', 'php artisan migrate'));
You can do more advanced things as well. Here is the documentation.
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';