MySQL remote connection fails with “unknown authentication method”

前端 未结 7 1059
名媛妹妹
名媛妹妹 2020-11-28 15:22

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 r         


        
相关标签:
7条回答
  • 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.

    0 讨论(0)
  • 2020-11-28 15:34

    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.

    0 讨论(0)
  • 2020-11-28 15:41

    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.

    0 讨论(0)
  • 2020-11-28 15:46

    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.

    0 讨论(0)
  • 2020-11-28 15:48

    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.

    0 讨论(0)
  • 2020-11-28 15:51

    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

    0 讨论(0)
提交回复
热议问题