“连接失败:用户 'root'@'localhost' 的访问被拒绝(使用密码:YES)"来自 php 函数

2022-01-30 00:00:00 access-denied php mysql

我写了一些php网页使用的函数,以便与mysql数据库交互.当我在我的服务器上测试它们时,我收到了这个错误:

I wrote some function used by a php webpage, in order to interact with a mysql database. When I test them on my server I get this error:

"Connect failed: Access denied for user 'root'@'localhost' (using password: YES)" 

我可以在我的电脑上使用它们(使用 XAMPP),我可以使用服务器中的命令行浏览数据库的表.但是,网页无法连接.我检查了密码,但没有结果.没错(否则我无法从命令行登录mysql).

I am able to use them on my pc (using XAMPP) and I can navigate through the tables of the database using the command line in the server. However, the webpage fails to connect. I've checked the password but with no results. It's correct (otherwise I could not log in to mysql from the command line).

函数的调用如下:

$conn = new mysqli("localhost", "root", "password", "shop");

我必须在我的服务器中设置一些东西吗?谢谢

Do I have to set something in my server? Thanks

PHP版本5.3.3-7+squeeze1mysql版本:5.1.49-3都在 debian 上

PHP version 5.3.3-7+squeeze1 mysql version: 5.1.49-3 both on debian

推荐答案

我是这样解决的:我用root用户名登录

I solved in this way: I logged in with root username

mysql -u root -p -h localhost

我创建了一个新用户

CREATE USER 'francesco'@'localhost' IDENTIFIED BY 'some_pass';

然后我创建了数据库

CREATE DATABASE shop;

我为这个数据库的新用户授予权限

I granted privileges for new user for this database

GRANT ALL PRIVILEGES ON shop.* TO 'francesco'@'localhost';

然后我退出root并登录新用户

Then I logged out root and logged in new user

quit;
mysql -u francesco -p -h localhost

我使用脚本重建了我的数据库

I rebuilt my database using a script

source shop.sql;

就是这样..现在从 php 调用没有问题

And that's it.. Now from php works without problems with the call

 $conn = new mysqli("localhost", "francesco", "some_pass", "shop");

感谢大家的宝贵时间:)

Thanks to all for your time :)

相关文章