quot;Connect failed: Access denied for user #39;root#39;@#39;localhost#39; (using password: YES)quot; from php function(“连接失败:用户 root@localhost 的访问被拒绝(使用密码:YES)来自 php 函数)
问题描述
我写了一些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 :)
这篇关于“连接失败:用户 'root'@'localhost' 的访问被拒绝(使用密码:YES)"来自 php 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“连接失败:用户 'root'@'localhost' 的访问被拒绝(使用密码:YES)"来自 php 函数


- 没有作曲家的 PSR4 自动加载 2022-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Laravel 仓库 2022-01-01