php - 我可以在命令行中远程连接到 mySQL 数据库,但不能使用 php 脚本

标签 php mysql remote-connection

错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'tech'@'localhost' (using password: YES)' in .......

我可以在命令行中远程连接,但不能使用 php,有什么想法可以查看吗?

详细信息如下:

Web服务器A:1.1.1.1保存php脚本:

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', '1');

    $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 <be>";

?>

Web服务器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 进入 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)

最佳答案

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

setsebool httpd_can_network_connect=1

关于php - 我可以在命令行中远程连接到 mySQL 数据库,但不能使用 php 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19012530/

相关文章:

mysql - 是否可以编写一个 SQL 查询来返回另一个表中不存在的值?

java - 无法理解连接建立代码

ssh - VSCode Remote-SSH 无法连接到我的服务器

PHPExcel 如何应用文本对齐水平填充 - PHPExcel_Style_Alignment::HORIZONTAL_Fill

php - 如何在 PHP 中创建一个可由 JavaScript 处理的多维关联数组?

MySQL 选择带有 '=' 但不带有 'LIKE' 的 UTF-8 字符串

Perl qx() 命令未按预期工作

php - 为什么 ob_get_contents 不起作用

PHP/MYSQL 对mysql数据的回显表进行排序

MySQL:获取一个表中两个值之间的差异(多个用户ID)?