php 来自php函数的“连接失败:用户'root'@'localhost'的访问被拒绝(使用密码:YES)”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6445917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Connect failed: Access denied for user 'root'@'localhost' (using password: YES)" from php function
提问by gc5
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:
我写了一些 php 网页使用的函数,以便与 mysql 数据库进行交互。当我在我的服务器上测试它们时,我收到此错误:
"Connect failed: Access denied for user 'root'@'localhost' (using password: YES)"
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).
我可以在我的电脑上使用它们(使用 XAMPP),我可以使用服务器中的命令行浏览数据库的表。但是,网页无法连接。我检查了密码,但没有结果。这是正确的(否则我无法从命令行登录到 mysql)。
The call of the function is the following:
该函数的调用如下:
$conn = new mysqli("localhost", "root", "password", "shop");
Do I have to set something in my server? Thanks
我必须在我的服务器中设置一些东西吗?谢谢
Edit: PHP version 5.3.3-7+squeeze1 mysql version: 5.1.49-3 both on debian
编辑:PHP 版本 5.3.3-7+squeeze1 mysql 版本:5.1.49-3 都在 debian
回答by gc5
I solved in this way: I logged in with root username
我是这样解决的:我用root用户名登录
mysql -u root -p -h localhost
I created a new user with
我创建了一个新用户
CREATE USER 'francesco'@'localhost' IDENTIFIED BY 'some_pass';
then I created the database
然后我创建了数据库
CREATE DATABASE shop;
I granted privileges for new user for this database
我为这个数据库的新用户授予了权限
GRANT ALL PRIVILEGES ON shop.* TO 'francesco'@'localhost';
Then I logged out root and logged in new user
然后我注销root并登录新用户
quit;
mysql -u francesco -p -h localhost
I rebuilt my database using a script
我使用脚本重建了我的数据库
source shop.sql;
And that's it.. Now from php works without problems with the call
就是这样..现在从 php 工作没有问题的调用
$conn = new mysqli("localhost", "francesco", "some_pass", "shop");
Thanks to all for your time :)
感谢大家的时间:)
回答by queso
Is there a user account entry in the DB for root@localhost? In MySQL you can set different user account permissions by host. There could be several different accounts with the same name combined with the host they are connecting from. The most common are [email protected] and root@localhost. These can have different passwords and permissions. Make sure root@localhost exist and has the settings you expect.
数据库中是否有 root@localhost 的用户帐户条目?在 MySQL 中,您可以按主机设置不同的用户帐户权限。可能有多个具有相同名称的不同帐户以及它们所连接的主机。最常见的是 [email protected] 和 root@localhost。这些可以有不同的密码和权限。确保 root@localhost 存在并具有您期望的设置。
I am willing to bet, based on your explanation, that this is the problem. Connecting from another PC uses a different account than root@localhost and the command line I think connects using [email protected].
根据您的解释,我敢打赌,这就是问题所在。从另一台 PC 连接使用与 root@localhost 不同的帐户,我认为使用 [email protected] 连接的命令行。
回答by Hyman Murdoch
From what you've said so far, it sounds like a problem where the MySQL driver in PHP5.3 has trouble connecting to the older MySQL version 4.1. Have a look on http://www.bitshop.com/Blogs/tabid/95/EntryId/67/PHP-mysqlnd-cannot-connect-to-MySQL-4-1-using-old-authentication.aspx
从你到目前为止所说的来看,这听起来像是一个问题,即 PHP5.3 中的 MySQL 驱动程序无法连接到旧版 MySQL 4.1。看看http://www.bitshop.com/Blogs/tabid/95/EntryId/67/PHP-mysqlnd-cannot-connect-to-MySQL-4-1-using-old-authentication.aspx
There's a similar question here with some useful answers, Cannot connect to MySQL 4.1+ using old authentication
这里有一个类似的问题,有一些有用的答案,Cannot connect to MySQL 4.1+ using old authentication
SELECT `User`, `Host`, Length(`Password`) FROM mysql.user
This will return 16 for accounts with old passwords and 41 for accounts with new passwords (and 0 for accounts with no password at all, you might want to take care of those as well). Either use the user managements tools of the MySQL front end (if there are any) or
这将为使用旧密码的帐户返回 16,为使用新密码的帐户返回 41(对于根本没有密码的帐户,返回 0,您可能也需要注意这些)。要么使用 MySQL 前端的用户管理工具(如果有的话)要么
SET PASSWORD FOR 'User'@'Host'=PASSWORD('yourpassword');
FLUSH Privileges
回答by LowLevel
I had this problem too. But this was because of another reason.
My password began with character $
... e.g. $MyPassword
I've changed it to #MyPassword
and the problem is solved.
我也有这个问题。但这是因为另一个原因。我的密码以字符开头$
...例如,$MyPassword
我已将其更改为#MyPassword
,问题解决了。
回答by Pseudo Nym
Here maybe?
也许在这里?
I believe that the code should be:
我认为代码应该是:
$connect = new mysqli("host", "root", "", "dbname");
$connect = new mysqli("host", "root", "", "dbname");
because root does not have a password. the (using password: YES)
is saying "you're using a password with this user"
因为 root 没有密码。在(using password: YES)
被说“你正在使用的密码与此用户”
回答by Miku jessi
Easy.
简单。
open xampp control panel -> Config -> my.ini edit with notepad. now add this below [mysqld]
打开 xampp 控制面板 -> 配置 -> 用记事本编辑 my.ini。现在在[mysqld]下面添加这个
skip-grant-tables
Save. Start apache and mysql.
节省。启动apache和mysql。
I hope help you
我希望能帮助你
回答by Shrinivas Manjithaya
May be there password is not set and you are passing password as argument. Try ommitting password argument during connection..
可能没有设置密码并且您将密码作为参数传递。尝试在连接期间省略密码参数..
回答by Dutch Glory
TIP:
comment out pid and socket file, if you can't connect to server..
mysql could be connecting with incorrect pid and/or socket file,
so when you try to connect to server trough command-line it "looks"at the incorrect pid/socket file...
TIP:
出PID和套接字文件评论,如果您无法连接到服务器.. MySQL的可能是不正确的PID和/或套接字文件连接,因此,当你尝试连接到服务器低谷命令行是“看上去”在不正确的pid/socket文件...
### comment out pid and socket file, if you can't connect to server..
#pid-file=/var/run/mysql/mysqld.pid #socket=/var/lib/mysql/mysql.sock
回答by camdixon
Most likely it is not identifying your user properly. root@localhost. Select the Mysql version your MAMP is using.
很可能它没有正确识别您的用户。根@本地主机。选择您的 MAMP 正在使用的 Mysql 版本。
/Applications/MAMP/Library/bin/mysqldump -uroot -p db_name > test_db_dump.sql
/Applications/MAMP/Library/bin/mysqldump -uroot -p db_name > test_db_dump.sql
I actually wrote an alias on my computer so I don't have to remember how to get to the bin directory.
我实际上在我的计算机上写了一个别名,所以我不必记住如何进入 bin 目录。
alias mysqldumpMAMP='/Applications/MAMP/Library/bin/mysqldump'
alias mysqldumpMAMP='/Applications/MAMP/Library/bin/mysqldump'
Which allows me to run this:
这让我可以运行这个:
mysqldumpMAMP -uroot -p db_name > test_db_dump.sql
mysqldumpMAMP -uroot -p db_name > test_db_dump.sql
You can save an alias in your bash profile ~/.bash_profile
or ~/.bash_rc
您可以在 bash 配置文件中保存别名~/.bash_profile
或~/.bash_rc
回答by Adesina Yomi
Edit your privileges on PHPmyAdmin(WAMP), Note that your password and user name has not been created. So do create or edit it, that it might work with your sql connection in your php. Hope it works
编辑您对PHPmyAdmin(WAMP)的权限,注意您的密码和用户名尚未创建。所以创建或编辑它,它可能会与您的 php.ini 中的 sql 连接一起使用。希望它有效