【发布时间】:2013-09-25 18:30:54
【问题描述】:
错误: 致命错误:未捕获的异常 'PDOException' 带有消息 'SQLSTATE[28000] [1045] Access denied for user 'tech'@'localhost' (using password: YES)' in .........
我可以在命令行中远程连接,但不使用 php,有什么想法可以查看吗?
以下是详细信息:
Webserver A: 1.1.1.1 保存 php 脚本:
$REGIS_DB_SERVER = "2.2.2.2"; //I've tried the IP as well as "pretendhostname.com"
$REGIS_DB_NAME = "db_name";
$REGIS_DB_USER = "tech"; // or any other user name
$REGIS_DB_PASSWORD = "password";
//connect to db
$db = new PDO("mysql:host = $REGIS_DB_SERVER; dbname=$REGIS_DB_NAME; charset=utf8", $REGIS_DB_USER, $REGIS_DB_PASSWORD);
print_r($db -> errorInfo());
echo "Connected<br>";
$sql = "CREATE TABLE Test(PersonID int,LastName varchar(255),FirstName varchar(255),Address varcha(255),City varchar(255))";
$result = $db->query($sql);
echo "$result <br>";
?>
Webserver B: 2.2.2.2 保存 mySQL 数据库: 运行 Ubuntu 11.04
在 /etc/mysql/my.cnf 中
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#Commented out to allow remote connections to this database
#bind-address = 216.70.81.240
我运行mysql> select Host,User from user; 并得到:
+-----------------------+------------------+
| Host | User |
+-----------------------+------------------+
| % | root |
| 127.0.0.1 | root |
| 1.1.1.1 | tech |
| localhost | debian-sys-maint |
| localhost | root |
| localhost | tech |
+-----------------------+------------------+
Open Port 3306 in ubuntu 12.04 to allow connections to mysql from any ip 表示这不是 iptable 问题并测试连接:
我 ssh-ed 到 Webserver A 1.1.1.1 并运行:
mysql -u tech -p -h 2.2.2.2
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 200672
Server version: 5.1.63-0ubuntu0.11.04.1 (Ubuntu)
【问题讨论】:
-
这看起来像数据库权限/特权问题。您可能需要从该主机仔细检查该用户的数据库权限。
-
请注意,错误消息显示的是
tech@localhost,而不是tech@1.1.1.1。看起来它忽略了host = $REGIS_DB_SERVER。 -
我注意到了这一点,这可能就是原因。我不知道为什么它忽略了
mysql:host = $REGIS_DB_SERVER;我错过了 Webserver A 1.1.1.1 上的设置吗? -
根据 MYSQL 文档:dev.mysql.com/doc/refman/5.5/en/access-denied.html 它说:如果您在使用 Perl、PHP、Python 或 ODBC 程序时遇到问题,请尝试使用 mysql -u user_name db_name 或 mysql 连接到服务器-u 用户名 -pour_pass db_name。如果您能够使用 mysql 客户端进行连接,则问题出在您的程序上,而不是访问权限。所以我猜我的PHP错了? php 脚本可用于访问本地 mySQL 数据库。它似乎只是忽略了远程数据库的 host=
标签: php mysql remote-connection