【问题标题】:Can't connect to MySQL from PHP, but is able to connect from remote machine?无法从 PHP 连接到 MySQL,但能够从远程计算机连接?
【发布时间】:2014-04-04 18:47:59
【问题描述】:

我真的想不通。在某个用户下,我可以从机器 B 连接到机器 A 上的 MySQL 数据库,但是我无法从机器 A 本身本地连接或从 PHP 连接。

为了说明,我在机器 A 上创建了一个数据库 MYTEST。然后我创建一个用户 MYTEST 并授予 MYTEST 上的所有权限:

GRANT ALL PRIVILEGES ON MYTEST.* TO 'MYTEST'@'%' IDENTIFIED BY 'mypass';
FLUSH PRIVILEGES;

我以 root 身份进行了这些操作,我可以从任何地方毫无问题地进行连接。结果:

然后,我尝试从另一台机器 B 连接:

ubuntu@ip-*-*-2-153:~$ mysql -u MYTEST -p -h *.*.*.174
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 84
Server version: 5.5.32-0ubuntu7 (Ubuntu)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

这些看起来都不错。现在我回到机器 A 并这样做:

root@wangboyang:~# mysql -u MYTEST -p
Enter password:
ERROR 1045 (28000): Access denied for user 'MYTEST'@'localhost' (using password:
 YES)
root@wangboyang:~#

而我能够以 root 身份连接:

root@wangboyang:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.

最终,我需要从 PHP 进行连接。它也不起作用:

php > $con = mysqli_connect('128.199.253.174', 'MYTEST', 'mypass');
PHP Warning:  mysqli_connect(): (28000/1045): Access denied for user 'MYTEST'@'w
angboyang.com' (using password: YES) in php shell code on line 1

我必须使用root:

php > $con = mysqli_connect('128.199.253.174', 'root', 'somepasswordhere');
php >

然后我尝试了一些激进的方法。我将所有全局权限授予 MYTEST,但所有结果都保持不变。请问有人有什么想法吗?真的很感激。

注意:

  1. 我确定密码正确。为了确认这一点,我使用了与 root 相同的密码,并将哈希值进行了比较。我可以使用root连接。所以密码是正确的。

  2. 两台机器都是Ubuntu 13.10。

  3. 我尝试在所有这些步骤之间刷新权限并重新启动 mysql 服务器,但无济于事。

【问题讨论】:

  • 我的托管公司只允许某些ip addy连接到数据库,我必须进去列出它们。也许你的也一样。
  • 我的机器 A 在 Digitalocean 上,机器 B 在亚马逊上。似乎并非如此。此外我正在尝试使用 localhost 进行连接,这应该是允许的第一件事......

标签: php mysql mysqli remote-access privileges


【解决方案1】:

我设法解决了这个问题,但我真的不明白为什么。

问题似乎是通配符“%”。它应该能够替代从任何 IP、域连接的用户。

但是,它不适用于“localhost”和“mydomain.com”的情况。我的猜测是,要么 '%' 只匹配 IP,要么行为在 doc 或 config 文件中的某处指定。

要解决它,请创建用户 'MYTEST'@'localhost' 和 'MYTEST'@'domainname.com'。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 2015-01-22
    • 2011-12-30
    相关资源
    最近更新 更多