MySQL remote connection fails with “unknown authentication method”

两盒软妹~` 提交于 2019-11-26 12:19:58

问题


I am trying to remotely connect to MySQL server online from my local machine, but I am getting the following error:

Warning: PDO::__construct(): The server requested authentication 
method unknown to the client [mysql_old_password] in 
C:\\xampp\\htdocs\\ticket\\terminal\\sync.php

SQLSTATE[HY000] [2054] The server requested authentication method 
umknown to the client

My local MySQL server version is 5.5.27, libmysql - mysqlnd 5.0.10 The remote MySQL server version is 5.5.23, the mysqlnd version isn\'t exposed.

I guess it\'s an incompatible password hash issue, but I do not know how to resolve it. Below is part of my connection code

$dsn = \'mysql:host=184.173.209.193;dbname=my_db_name\';
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => \'SET NAMES utf8\',
); 

try {
    $online_dbh = new PDO($dsn, \'myusername\', \'mypassword\', $options);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo \"Congratulations!\";
} catch (PDOException $e) {
    echo $e->getMessage();
} 

回答1:


Assuming you're using PHP 5.3+, you could be experiencing one of the Backward Incompatibility Changes:

The new mysqlnd library necessitates the use of MySQL 4.1's newer 41-byte password format. Continued use of the old 16-byte passwords will cause mysql_connect() and similar functions to emit the error, "mysqlnd cannot connect to MySQL 4.1+ using old authentication."

If so, see https://stackoverflow.com/a/1340538/187954 for information on updating your password.




回答2:


I overcame the challenge. I found out that my remote MySQL database host still uses the old MySQL password hash which is 16-byte, while my localhost database server uses 41-byte password hash. I used the following query to find the password length:

SELECT PASSWORD('mypass') 

I changed my localhost database server password hash to 16-byte by running the following query

SET GLOBAL old_passwords = 1;

Then I edited my.ini file, and set the old_password=1 to ensure that when the server restarts, it won't revert to the new password system. But that didn't solve my problem.

I figured out that it was PHP that handles the authentication, since I was using PHP's MySQL API, so I downgraded to PHP 5.2.8 and I was able to make the remote connection successfully.

I hope this helps someone.




回答3:


This may help someone with this issue. This is how I fixed it in my situation. From the MySQL PHP API (PDO_MYSQL) website

When running a PHP version before 7.1.16, or PHP 7.2 before 7.2.4, set MySQL 8 Server's default password plugin to mysql_native_password or else you will see errors similar to The server requested authentication method unknown to the client [caching_sha2_password] even when caching_sha2_password is not used.

This is because MySQL 8 defaults to caching_sha2_password, a plugin that is not recognized by the older PHP (mysqlnd) releases. Instead, change it by setting default_authentication_plugin=mysql_native_password in my.cnf. The caching_sha2_password plugin will be supported in a future PHP release. In the meantime, the mysql_xdevapi extension does support it.




回答4:


I ran into this same problem and figured out the problem had indeed to do with PHP.

The solution was simple for me: switch to mysqli instead of PDO. When using Zend_Db, this is as easy as changing 1 line:

$db = Zend_Db::factory('pdo_mysql',array('host' => MYSQL_HOST, 'username' => MYSQL_USER, 'password' => MYSQL_PASS, 'dbname' => MYSQL_SCHEMA));

Becomes

$db = Zend_Db::factory('mysqli',array('host' => MYSQL_HOST, 'username' => MYSQL_USER, 'password' => MYSQL_PASS, 'dbname' => MYSQL_SCHEMA));

But if you application is not using the Zend_Db abstraction (or any other) layer, you could be in trouble.




回答5:


I had this problem on a shared hosting service (bluehost.com). I just reset the borking MySql user's password via the web interface provided by Bluehost. I re-used the old password, retyped it at the interface and saved and everything was fine again.




回答6:


I got this problem when running bitbucket pipelines with laravel 5.6 and default mysql database (set is services:).

solution was to use older version of mysql

definitions:
  services:
    mysql:
     image: mysql:5.6

PS: it was PHP 7.1




回答7:


alter user 'username'@'localhost' identified with mysql_native_password by 'password'; would fix it.



来源:https://stackoverflow.com/questions/14612551/mysql-remote-connection-fails-with-unknown-authentication-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!