php mysqli_connect: authentication method unknown to the client [caching_sha2_password]

前端 未结 11 2225
遥遥无期
遥遥无期 2020-11-22 03:40

I am using php mysqli_connect for login to a MySQL database (all on localhost)



        
相关标签:
11条回答
  • 2020-11-22 04:04

    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.

    0 讨论(0)
  • 2020-11-22 04:04

    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

    0 讨论(0)
  • 2020-11-22 04:07

    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.

    0 讨论(0)
  • 2020-11-22 04:08

    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';
    
    0 讨论(0)
  • 2020-11-22 04:08

    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);
    }
    
    ?>
    
    0 讨论(0)
提交回复
热议问题