简体   繁体   English

使用PHP拒绝访问mysql DB,但可以使用phpmyadmin和在命令行上登录

[英]ACCESS DENIED to mysql DB with PHP but can login with phpmyadmin and on the command line

Blew away that server, restarting fresh. 吹走该服务器,然后重新启动。 WAY too much time wasted on this! 太多时间浪费在此上!

Let me know if I left any details out please. 让我知道是否遗漏了任何细节。 Have spent about a week researching this and trying things to no avail. 花了大约一个星期的时间对此进行研究,但仍无济于事。

Creating a website (first time) for my upcoming wedding and I would like my guests to be able to post comments to the site. 为即将举行的婚礼创建一个网站(第一次),我希望我的客人能够在该网站上发表评论。 I am a professional software engineer but not a web dev so this was my attempt at teaching myself more about php and perl and learning to work with Linux. 我是专业的软件工程师,但不是Web开发人员,所以这是我尝试自学更多有关php和perl以及学习使用Linux的知识。

Setup LAMP on Ubuntu 12.0.4 hosted on Amazon. 在Amazon托管的Ubuntu 12.0.4上设置LAMP。 Followed standard setup tutorial online. 在线遵循标准设置教程。 Apache2 with PHP5. 带有PHP5的Apache2。

Created new MYSQL users (names changed for obvious reasons) user@localhost and user@% with passwords. 使用密码创建了新的MYSQL用户(由于明显的原因而更改了名称)user @ localhost和user @%。 All privileges granted to both (even on the specific DB), then flushed. 授予两者的所有特权(即使在特定的DB上),然后刷新。 Firewall port 3306 opened. 防火墙端口3306已打开。 bind-address in the mysql config file set to 0.0.0.0 or commented out (have tried both). mysql配置文件中的bind-address设置为0.0.0.0或已注释掉(都尝试过)。 Made sure not to start it with --skip-networking. 确保不要使用--skip-networking来启动它。

I can connect with user@localhost just fine when I run mysql -u user -hlocalhost -p as well as with phpmyadmin on the web. 当我运行mysql -u user -hlocalhost -p以及在网络上使用phpmyadmin时,我可以使用user @ localhost进行连接。 Correct privileges (including GRANT) are shown when I query that and I have granted each user full access to the DB I created for the site. 当我查询正确的特权(包括GRANT)时,将显示该特权,并且已授予每个用户对我为该站点创建的数据库的完全访问权限。

Now my issue is when using php or perl I cannot connect to the DB from the webpage or from running the scripts on the command line (such as sudo php script.php). 现在我的问题是当使用php或perl时,我无法从网页或在命令行上运行脚本(例如sudo php script.php)连接到数据库。

Here is the line of my php script that fails, I can post the perl if anyone would like: 这是我的php脚本行失败,如果有人愿意,我可以发布perl:

mysql_connect("localhost", "user", "password")or die( mysql_error());

No matter what I do I get this error when running the script: 无论我做什么,在运行脚本时都会收到此错误:

Access denied for user 'user'@'localhost' (using password: YES) 用户'user'@'localhost'的访问被拒绝(使用密码:是)

Same error, every dang time. 同样的错误,每当当。

Now because I can log in with those same credentials through the command line and phpmyadmin I have to assume it is a setting I have messed somewhere on the server. 现在,因为我可以通过命令行和phpmyadmin使用相同的凭据登录,所以我必须假定它是服务器上某个地方弄乱的设置。 I don't know what else to check aside from what I already have and what feels like hundreds of answers I have read on the subject haven't helped. 除了已经拥有的东西之外,我不知道还有什么要检查的东西,而且我读过的数百个答案似乎对我没有帮助。 If anyone could offer some advice that would be great, I am at wits end and about to trash the entire server and start fresh. 如果有人可以提供一些很棒的建议,那么我无能为力,就要浪费整个服务器并重新开始。

Or if no one has an answer for this, any other way I can achieve my end goal of having users be able to post comments to the site? 还是如果没有人对此有答案,我可以通过其他任何方式实现让用户能够在网站上发表评论的最终目标?

The user you're going to use with your scripting should have both the "@localhost" and "'@%'" - Have a peek at the man. 您要与脚本一起使用的用户应同时具有“ @localhost”和“'@%'”-窥视这个人。 http://dev.mysql.com/doc/refman/5.1/en/adding-users.html This gave me a headache once. http://dev.mysql.com/doc/refman/5.1/en/adding-users.html这一次让我头疼。

尝试这个:

    mysql_connect("127.0.0.1", "user", "password")or die( mysql_error());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM