2

错误:致命错误:未捕获的异常 '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>";

?>

网络服务器 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             |
+-----------------------+------------------+

在 ubuntu 12.04 中打开端口 3306 以允许从任何 ip 连接到 mysql意味着这不是 iptable 问题并测试连接:

我通过 ssh 进入 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)
4

3 回答 3

3

您需要禁用 SELinux,以运行以下 CMD

setsebool httpd_can_network_connect=1

于 2018-04-19T11:21:29.010 回答
1

发现: PDO无法连接远程mysql服务器后 发现,如果没有“理解”主机,PDO连接会默认为host=localhost。仔细观察,我意识到作业之间有 2 个空格。我没有意识到空格在语法中很重要。

所以最后,这是一个语法错误:

工作解决方案(通过删除空格):

$db = new PDO("mysql:host=$REGIS_DB_SERVER; dbname=$REGIS_DB_NAME; charset=utf8", $REGIS_DB_USER, $REGIS_DB_PASSWORD);
于 2013-09-26T22:32:59.607 回答
0

这绝对是一个数据库权限/特权问题。让您的 dba/sysadGRANT privileges让您的本地服务器访问数据库

于 2013-09-25T18:51:14.617 回答