Reputation: 2708
I am using php mysqli_connect
for login to a MySQL database (all on localhost)
<?php
//DEFINE ('DB_USER', 'user2');
//DEFINE ('DB_PASSWORD', 'pass2');
DEFINE ('DB_USER', 'user1');
DEFINE ('DB_PASSWORD', 'pass1');
DEFINE ('DB_HOST', '127.0.0.1');
DEFINE ('DB_NAME', 'dbname');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if(!$dbc){
die('error connecting to database');
}
?>
MySQL Server ini File:
[mysqld]
# The default authentication plugin to be used when connecting to the server
default_authentication_plugin=caching_sha2_password
#default_authentication_plugin=mysql_native_password
with caching_sha2_password
in the MySQL Server ini file, it's not possible at all to login with user1 or user2;
error: mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password] in...
with mysql_native_password
in the MySQL Server ini file, it's possible to login with user1, but with user2, same error;
how can I login using caching_sha2_password
on the mySql Server?
Upvotes: 171
Views: 342195
Reputation: 851
For mysql 8.4, this suggested line doesn't work anymore,
[mysqld]
default-authentication-plugin=mysql_native_password
Instead, you should use,
[mysqld]
mysql_native_password=ON
mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
Upvotes: 0
Reputation: 341
I have the similar issue. Before solving it lets first understand the problem.
MySQL 5.7 and earlier:
These versions use mysql_native_password
as the default authentication plugin. This plugin uses a simple password hashing algorithm and is widely supported by various MySQL client libraries, including older PHP versions.
MySQL 8.x: The default authentication plugin is caching_sha2_password
, which provides better security through the SHA-256 hashing algorithm. This newer authentication method was not initially supported by all MySQL client libraries, including older versions of PHP's MySQL extensions (like mysqli and PDO).
PHP 7.2 and earlier:
These versions were released before or around the time MySQL 8.0 was released. As such, the default MySQL extensions in these PHP versions may not support caching_sha2_password
out of the box.
PHP 7.4 and later: These versions have improved support for MySQL 8.x and its default caching_sha2_password
authentication plugin. Thus, they can connect to MySQL 8.x databases using the new authentication method without requiring configuration changes.
So the problem is when you are using latest MySQL (v8.+) with old PHP ( 7.3.x and earlier) that does not have support for caching_sha2_password
you might get similar issue.
to solve this issue you need change mysql default authentication plugin from caching_sha2_password
to mysql_native_password
Update my.cnf then restart Mysql
[mysqld]
default_authentication_plugin= mysql_native_password
Login into mysql as root using terminal then run these
- when your mysql host is 'localhost'
ALTER USER 'dbuser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root@123';
- when your mysql host is other than 'localhost', like 10.10.0.2 etc.
ALTER USER 'dbuser'@'%' IDENTIFIED WITH mysql_native_password BY 'root@123';
[Note] make sure you replace dbuser
with your actual database username.
- finally flush all privilege
FLUSH PRIVILEGES;
Upvotes: 3
Reputation: 1035
In my case:
I'm using PHP 5.6
and MYSQL 8
. Since MYSQL 8
comes with caching_sha2_password
as default authentication and PHP 5.6
doesn't support it yet, i have to set MYSQL 8 authentication to mysql_native_password
by doing:
add
default-authentication-plugin=mysql_native_password
to the end of line of /etc/mysql/conf.d/mysql.cnf
configuration file in ubuntu 22.04
Upvotes: 0
Reputation: 635
In Digital Ocean Managed Mysql, we have an option to change encryption, you can change to legacy and it'll work ok.
Upvotes: 0
Reputation: 1302
If you have not yet already changed your MySQL default authentication plugin, you can do so by:
root
to MySQLa. if you are running MySQL in a different server:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password
BY 'password';
b. if you are running MySQL in a different server:
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password
BY 'password';
Upvotes: 1
Reputation: 4530
In my case, i'm using PHP Symfony
framework and it's a silly mistake.
The database credential was wrong in paramers.yml
.
After changing the credentials accordingly the problem was gone.
Upvotes: 0
Reputation: 4288
Note: This method may not work if you have the problem with root user account.
Upvotes: 0
Reputation: 143
In my case when I was using WAMP server I fixed it using following
Using the above steps I was able to run the program successfully.
Upvotes: 0
Reputation: 1439
I am using laravel 5.8 and having MAMP server got this error resolved by adding DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock in .env file like below
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=dbname
DB_USERNAME=root
DB_PASSWORD=root
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
Upvotes: 0
Reputation: 3164
As of PHP 7.4, this is no longer an issue. Support for caching_sha2
authentication method has been added to mysqlnd.
Currently, PHP mysqli extension do not support new caching_sha2 authentication feature. You have to wait until they release an update.
Check related post from MySQL developers: https://mysqlserverteam.com/upgrading-to-mysql-8-0-default-authentication-plugin-considerations/
They didn't mention PDO, maybe you should try to connect with PDO.
Upvotes: 105
Reputation: 1557
Now you can upgrade to PHP7.4 and MySQL will go with caching_sha2_password
by default, so default MySQL installation will work with mysqli_connect
No configuration required.
Upvotes: 5
Reputation: 507
Like many many people, I have had the same problem. Although the user is set to use mysql_native_password, and I can connect from the command line, the only way I could get mysqli() to connect is to add
default-authentication-plugin=mysql_native_password
to the [mysqld] section of, in my setup on ubuntu 19.10, /etc/mysql/mysql.conf.d/mysqld.cnf
Upvotes: 36
Reputation: 1330
If you're on a Mac, here's how to fix it. This is after tons of trial and error. Hope this helps others..
Debugging:
$mysql --verbose --help | grep my.cnf
$ which mysql
/usr/local/bin/mysql
Resolution: nano /usr/local/etc/my.cnf
Add: default-authentication-plugin=mysql_native_password
-------
# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
bind-address = 127.0.0.1
default-authentication-plugin=mysql_native_password
------
Finally Run: brew services restart mysql
Upvotes: 8
Reputation: 3
I think it is not useful to configure the mysql server without caching_sha2_password encryption, we have to find a way to publish, send or obtain secure information through the network. As you see in the code below I dont use variable $db_name, and Im using a user in mysql server with standar configuration password. Just create a Standar user password and config all privilages. it works, but how i said without segurity.
<?php
$db_name="db";
$mysql_username="root";
$mysql_password="****";
$server_name="localhost";
$conn=mysqli_connect($server_name,$mysql_username,$mysql_password);
if ($conn) {
echo "connetion success";
}
else{
echo mysqli_error($conn);
}
?>
Upvotes: -4
Reputation: 119
I tried this in Ubuntu 18.04 and is the only solution that worked for me:
ALTER USER my_user@'%' IDENTIFIED WITH mysql_native_password BY 'password';
Upvotes: 4
Reputation: 4336
If you're on Windows and it's not possible to use caching_sha2_password
at all, you can do the following:
The Installer will make all the configuration changes needed for you.
Upvotes: 32
Reputation: 529
ALTER USER 'mysqlUsername'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mysqlUsernamePassword';
Remove quotes (') after ALTER USER
and keep quote (') after mysql_native_password BY
It is working for me also.
Upvotes: 45
Reputation: 455
It's working for me (PHP 5.6 + PDO / MySQL Server 8.0 / Windows 7 64bits)
Edit the file C:\ProgramData\MySQL\MySQL Server 8.0\my.ini
:
default_authentication_plugin=mysql_native_password
Reset MySQL service on Windows, and in the MySQL Shell...
ALTER USER my_user@'%' IDENTIFIED WITH mysql_native_password BY 'password';
Upvotes: 22
Reputation: 99
I ran the following command
ALTER USER 'root' @ 'localhost' identified with mysql_native_password BY 'root123';
in the command line and finally restart MySQL in local services.
Upvotes: 9
Reputation: 3640
I solve this by SQL command:
ALTER USER 'mysqlUsername'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mysqlUsernamePassword';
which is referenced by https://dev.mysql.com/doc/refman/8.0/en/alter-user.html
if you are creating new user
CREATE USER 'jeffrey'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
which is referenced by https://dev.mysql.com/doc/refman/8.0/en/create-user.html
this works for me
Upvotes: 343