远程 mySQL 连接抛出“无法连接到 MySQL 4.1”使用旧的不安全身份验证”来自 XAMPP 的错误

发布于 2024-12-21 05:41:21 字数 1155 浏览 2 评论 0原文

我正在 XAMPP/WinXP 上运行 WordPress 的本地副本进行开发,但希望保持与远程数据库的连接。无论我如何尝试,我总是收到“建立数据库连接时出错”的消息。

在同一台 PC 上,我可以使用任意数量的 mySQL 客户端连接到远程 mySQL 数据库,并且在 mySQL 端,用户和数据库都设置为接受来自任何通配符域的传入请求。我还可以轻松地从我的 PC ping 远程数据库服务器(尽管我不知道如何在 XAMPP 中执行此操作)。

XAMPP是它自己的小宇宙,无法与外界联系吗?或者有什么我明显忽略的东西不允许我联系?

错误

Warning: mysql_connect() [function.mysql-connect]: Premature end of data (mysqlnd_wireprotocol.c:553) in C:\xampp\htdocs\dbtest.php on line 5 
Warning: mysql_connect() [function.mysql-connect]: OK packet 1 bytes shorter than expected in C:\xampp\htdocs\dbtest.php on line 5 
Warning: mysql_connect() [function.mysql-connect]: mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication

编辑

感谢@Michael 建议我创建一个简单的连接脚本,以便我可以更好地了解引发的实际错误。这表明它与 mySQL 中的 old_password 设置有关。有关如何解决此问题的完整说明,请参阅下面我的答案。

这是我放入 xampp\htdocs 文件夹中并进行测试的测试脚本:

<?php

$mysqli = new mysqli('my.server.address', 'user_name', 'password', 'database_name');

if ($mysqli->connect_error){
    die ("Connect error: " . $mysqli->connect_error );
}

I'm running a local copy of WordPress on XAMPP/WinXP for development, but would like to maintain a connection to the remote database. I keep getting "Error establishing database connection" no matter what I try.

On the same PC, I can connect to the remote mySQL DB using any number of mySQL clients, and on the mySQL side, the both the user and the database are set to accept incoming requests from any wildcard domain. I can also easily ping the remote database server from my PC (though I don't know how to do it from WITHIN XAMPP).

Is XAMPP its own little universe that can't reach through to the outside world? Or is there something I'm clearly overlooking that's not letting me connect?

Errors

Warning: mysql_connect() [function.mysql-connect]: Premature end of data (mysqlnd_wireprotocol.c:553) in C:\xampp\htdocs\dbtest.php on line 5 
Warning: mysql_connect() [function.mysql-connect]: OK packet 1 bytes shorter than expected in C:\xampp\htdocs\dbtest.php on line 5 
Warning: mysql_connect() [function.mysql-connect]: mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication

Edit

Thanks to @Michael for suggesting I just create a simple connection script so I can get better insight into the actual error that's being thrown. This revealed that it had to do with the old_password setting in mySQL. See my Answer below for a full description of how to resolve this issue.

Here's the test script I put inside my xampp\htdocs folder and tested out:

<?php

$mysqli = new mysqli('my.server.address', 'user_name', 'password', 'database_name');

if ($mysqli->connect_error){
    die ("Connect error: " . $mysqli->connect_error );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别把无礼当个性 2024-12-28 05:41:21

我不太清楚为什么这会成为我的 XAMPP 安装中的一个问题,因为我也在服务器的本地机器上运行 PHP 5.3.x 并且没有遇到这些问题。但是,这与我的 mySQL 服务器以“旧密码”加密模式运行有关。较新版本的 PHP 不允许此类连接,因此您需要更新 mySQL 服务器以使用较新的密码加密。以下是假设您可以控制 mySQL 服务器的步骤。如果你不这样做,那就超出了我的知识范围。

  1. 找到名为 my.cnf 的 mysql 服务器配置文件。我在 /etc/my.cnf 找到了我的。您可以使用sudo nano /etc/my.cnf

    编辑它,

  2. 查找显示 old_passwords=1 的行并将其更改为 old_passwords=0< /代码>。您现在已经告诉服务器,下次运行它时,并要求它使用 PASSWORD() 命令加密密码,它使用新的 41 字符加密而不是 16 字符“旧”式加密

  3. 现在你必须重新启动你的mysql服务器/服务。 YMMV,但在 Fedora 上,可以通过 sudo service mysqld restart 轻松完成。检查操作系统关于重新启动 mysql 守护进程或服务的说明

  4. 现在我们必须实际编辑 mysql 中的 user 表。因此,打开 mysql 的交互式 shell(在服务器上您可以输入 mysql -uYourRootUsername -pYourRootPassword )

  5. 更改为 mysql 数据库。这是包含服务器操作和身份验证的所有好东西的数据库。您必须具有 root 访问权限才能使用此数据库。如果您收到“访问被拒绝”的消息,那么您就 SOL 了。对不起。 use mysql; 将切换到该数据库

  6. 现在我们要更新给您带来悲伤的用户。最终,您可能想要更新所有用户,但现在,我们只关注引发错误的用户。 update user set Password=password('YOUR_PASSWORD') where User='YOUR_USERNAME';

  7. 现在你只需要告诉 mysql 在该用户尝试连接时使用新密码进行身份验证。 flushprivileges;.

你应该可以走了!

I'm not really clear on why this became an issue on my XAMPP installation, since I'm also running PHP 5.3.x on the server's local box and wasn't experiencing those issues there. However, it has to do with my mySQL server running in "old password" encryption mode. Newer versions of PHP won't allow those kinds of connections, so you need to update your mySQL server to use the newer password encryption. Here are the steps, assuming you have control over the mySQL server. If you don't, that falls out of the scope of my knowledge.

  1. locate the configuration file for the mysql server called my.cnf. I found mine at /etc/my.cnf. You can edit it with sudo nano /etc/my.cnf

  2. Look for a line that says old_passwords=1 and change that to old_passwords=0. You have now told the server that the next time it is run, and it is asked to encrypt a password using the PASSWORD() command, it use the new 41-character encryption rather than the 16-character 'old' style encryption

  3. Now you have to restart your mysql server / service. YMMV, but on Fedora that was easily done with sudo service mysqld restart. Check your OS' instructions for restarting the mysql daemon or service

  4. Now we have to actually edit our user table within mysql. So open up an interactive shell to mysql (on the server you can type mysql -uYourRootUsername -pYourRootPassword)

  5. Change to the mysql database. This is the database that holds all the good stuff for server operation and authentication. You must have root access to work with this database. If you get an 'access denied' you're SOL. Sorry. use mysql; will switch to that database

  6. Now we want to update the user that was giving you grief. Ultimately you'll probably want to update all your users, but for now, we're just focusing on the user that threw the error. update user set Password=password('YOUR_PASSWORD') where User='YOUR_USERNAME';

  7. Now you just need to tell mysql to use the new password for authentication when that user attempts to connect. flush privileges;.

You should be good to go!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文