3

当我尝试通过远程 MySQL 服务器上的本地计算机的 shell 进行连接时,我可以成功连接:

> mysql -h remotehost -u myuser -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.

但是当我尝试使用 PHP 脚本连接时

$serverName = "remotehost"; // here I put the actual IP address
$userName = "myuser";
$passCode = "actualpassword";
mysql_connect($serverName,$userName,$passCode);

我收到以下错误

警告:mysql_connect():用户“myuser”@“localhost”的访问被拒绝(使用密码:是)

远程MySQL服务器版本为5.1.52,本机PHP版本为5.3.10-1ubuntu3.4

我发现了一个类似的问题,但答案并没有解决我的问题: problem connected to remote mysql database using php

我真的很感激一些帮助!

编辑

的输出 php -i | grep "mysql"

/etc/php5/cli/conf.d/mysql.ini,

/etc/php5/cli/conf.d/mysqli.ini,

/etc/php5/cli/conf.d/pdo_mysql.ini

mysql

MYSQL_SOCKET => /var/run/mysqld/mysqld.sock

MYSQL_INCLUDE => -I/usr/include/mysql

MYSQL_LIBS => -L/usr/lib/i386-linux-gnu -lmysqlclient_r

mysql.allow_local_infile => On => On

mysql.allow_persistent => On => On

mysql.connect_timeout => 60 => 60

mysql.default_host => no value => no value

mysql.default_password => no value => no value

mysql.default_port => no value => no value

mysql.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock

mysql.default_user => no value => no value

mysql.max_links => Unlimited => Unlimited

mysql.max_persistent => Unlimited => Unlimited

mysql.trace_mode => Off => Off

mysqli

MYSQLI_SOCKET => /var/run/mysqld/mysqld.sock

mysqli.allow_local_infile => On => On

mysqli.allow_persistent => On => On

mysqli.default_host => no value => no value

mysqli.default_port => 3306 => 3306

mysqli.default_pw => no value => no value

mysqli.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock

mysqli.default_user => no value => no value

mysqli.max_links => Unlimited => Unlimited

mysqli.max_persistent => Unlimited => Unlimited

mysqli.reconnect => Off => Off

PDO drivers => mysql

pdo_mysql

pdo_mysql.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock
4

5 回答 5

0

Use SHOW GRANTS to see if the user you are trying to login with has permission to connect from the web server's IP or domain name.

mysql> SHOW GRANTS;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*6381FFD48B21F7ED17AE12AF4F8BE4458F4B0AC7' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Use the GRANT command to give remote access to your web user, so to speak. Something like this.

mysql> GRANT SELECT ON database.table TO 'myuser'@'web_server_IP_or_domain_name' IDENTIFIED BY 'actualPassword';

Reference the MySQL 5.1 manual for exact syntax. MySQL 5.1 Manual: GRANT

于 2014-05-30T13:55:13.910 回答
0

您需要允许远程访问 mySQL(在您拥有 mySQL 数据库的服务器上)。如果您使用的是 CPanel 或 Plesk,您可以在控制面板中进行操作。我遇到了这个问题,显然 mySQL 默认配置是只允许本地主机。

于 2013-03-17T18:42:42.320 回答
0

您需要授予该用户访问权限,以便您的服务器的 IP 地址可以访问该用户。MySQL 用户经常被锁定到本地主机。

一个快速的方法是查看表mysql.users并查看该用户是否被锁定到本地主机。

有许多不错的 GUI 可以帮助添加主机,但最手动的方法可能是向数据库添加另一行。您可以将主机更改为 '%' 以进行测试,但这会打开一些东西并且不太安全。

于 2013-03-17T18:43:07.417 回答
0

您是否发布了实际代码?如果是这样,您忘记将字符串括在引号中:

$serverName = "remotehost"; // here I put the actual IP address
$userName   = "myuser";
$passCode   = "actualpassword";
mysql_connect($serverName,$userName,$passCode);
于 2013-03-17T18:36:33.793 回答
0

在继续之前,请确认 $serverName 变量确实是远程地址,主机名或 IP 地址。然后...

于 2013-03-17T18:35:26.767 回答