到远程MySQL服务器的PHP连接

pokxtpni  于 2022-12-17  发布在  Mysql
关注(0)|答案(5)|浏览(122)

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

> 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 connecting 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
tf7tbtn2

tf7tbtn21#

在继续之前,请确认$serverName变量确实是一个远程地址,可以是主机名或IP地址。

kgqe7b3p

kgqe7b3p2#

你发布了实际的代码吗?如果是的话,你忘了用引号把字符串括起来:

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

cgh8pdjw3#

你需要允许远程访问mySQL(在你有mySQL db的服务器上)。如果你使用的是CPanel或Plesk,你可以在控制面板中完成。我遇到了这个问题,显然mySQL的默认配置是只允许localhost。

4jb9z9bj

4jb9z9bj4#

您需要为该用户授予访问权限,以便通过您的服务器IP地址进行访问。通常情况下,MySQL用户被锁定到本地主机。
一种快速的方法是查看表mysql.users,看看该用户是否锁定到本地主机。
有许多不错的GUI可以帮助添加主机,但最手动的方法可能是向DB中添加另一行。您可以将主机更改为“%”以进行测试,但这会打开一些内容,而且不太安全。

gupuwyp2

gupuwyp25#

使用SHOW GRANTS查看您尝试登录的用户是否有权从Web服务器的IP或域名进行连接。

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)

可以说,使用GRANT命令为您的 *web用户 * 给予远程访问权限。

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

请参考MySQL 5.1手册了解确切的语法。MySQL 5.1 Manual: GRANT

相关问题