mysqlnd cannot connect to MySQL 4.1+ using the old insecure【解决方法】

本文介绍了如何解决因使用旧版不安全密码认证而导致的MySQL连接问题。通过更新数据库配置及用户密码,确保与php5.3及以上版本的兼容性。

【MySQL】mysqlnd cannot connect to MySQL 4.1+ using the old insecure【解决方法】

mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication. Please use an administration tool to reset your password with the command SET PASSWORD = PASSWORD(‘your_existing_password’). This will store a new, and more secure, hash value in mysql.user. If this user is used in other scripts executed by PHP 5.2 or earlier you might need to remove the old-passwords flag from your my.cnf file. SQL 错误代码: “7335941”.

是采用兼容格式的密码,而 php5.3的php_mysql; php_pdo_mysql 采用的是增强的密码,所以导致两者不匹配,最方便的方式还是更新db的设置,取消 old_passwords
然后在 mysql.mysql.user,更新所有用户的密码, 如:

SET old_passwords =0;
UPDATE mysql.user SET Password =PASSWORD('testpass') WHERE User='testuser' limit 1;
SELECT LENGTH(Password) FROM mysql.user WHERE User='testuser';
FLUSH PRIVILEGES;

转载自:http://lampblog.org/1430.html

在使用PHP连接MySQL 8.0及以上版本时,可能会遇到如下错误信息: ``` SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client ``` 该问题的根本原因在于MySQL 8.0默认使用的身份验证插件为`caching_sha2_password`,而一些旧版本的PHP客户端(如使用`mysqlnd`扩展的PHP 7.4及更早版本)并不支持这种新的认证方式,导致连接失败[^3]。 ### 解决方案 #### 方法一:修改MySQL用户的身份验证方式 可以通过修改MySQL用户的认证方式为`mysql_native_password`来解决此问题。执行以下SQL语句: ```sql ALTER USER 'your_username'@'your_host' IDENTIFIED WITH mysql_native_password BY 'your_password'; ``` 其中,`your_username`为数据库用户名,`your_host`为允许连接的主机名或IP地址(如`localhost`或`%`表示任意主机),`your_password`为新密码。 执行完成后,重新尝试连接数据库,问题应得以解决[^3]。 #### 方法二:修改MySQL配置文件以使用旧认证方式 在MySQL的配置文件(通常位于`/etc/my.cnf`或`/etc/mysql/my.cnf`)中添加以下配置项: ```ini [mysqld] default-authentication-plugin=mysql_native_password ``` 此配置将使MySQL在创建新用户或修改用户密码时默认使用`mysql_native_password`作为认证方式。修改后重启MySQL服务以生效: ```bash sudo systemctl restart mysqld ``` 注意:此方法会影响所有新创建的用户和密码修改操作,因此适用于需要统一认证方式的场景[^2]。 #### 方法三:升级PHP版本或MySQL客户端扩展 如果可能,建议将PHP升级到支持`caching_sha2_password`认证方式的版本(如PHP 8.0+),并确保安装了支持该认证方式的MySQL客户端库(如`mysqlnd` 8.0+)。此外,确认`pdo_mysql`或`mysqli`扩展已启用,并使用最新的MySQL客户端库进行编译[^1]。 --- ### 示例:PHP连接MySQL 8.0的代码 确保在PHP中使用正确的连接方式,例如使用PDO连接: ```php <?php $host = '127.0.0.1'; $db = 'test_db'; $user = 'test_user'; $pass = 'test_password'; $charset = 'utf8mb4'; $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $opt = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ]; try { $pdo = new PDO($dsn, $user, $pass, $opt); echo "Connection successful!"; } catch (PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?> ``` --- ### 方法四:检查PHP扩展和MySQL客户端版本 确保PHP中使用的MySQL客户端版本支持`caching_sha2_password`。可以使用以下命令检查当前MySQL客户端版本: ```bash php -i | grep mysqlnd ``` 如果输出中显示的版本低于`mysqlnd 8.0`,则建议升级到支持MySQL 8.0认证方式的版本。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值