使用PHP连接到远程MySQL服务器 [英] Connecting to remote MySQL server using PHP
问题描述
我正在尝试使用以下代码从本地计算机virtualhost连接到远程MySQL服务器:
I am attempting to connect to a remote MySQL server from my local machine virtualhost using the following code:
$conn = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
mysql_select_db($dbname, $conn) or die(mysql_error());
我的问题是我无法在本地连接,收到错误消息:
My problem is that I am unable to connect locally, receiving the error:
无法连接到"xxx.xxx.xxx.xxx"(10060)上的MySQL服务器
Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)
当我将相同的PHP文件上传到服务器时,情况并非如此.我能够毫无问题地查询数据库.
This is not the case when I upload the same PHP file to the server. I am able to query the database with no problems at all.
我也无法通过命令行进行连接,但是我可以访问cPanel,这排除了我的IP被意外禁止的可能性.
I am unable to connect via command line either, but I can access cPanel which rules out the chance of my IP being banned accidentally.
我的本地服务器正在运行PHP 5.2.9,远程服务器是5.2.12
My local server is running PHP 5.2.9, the remote server 5.2.12
推荐答案
- 必须设置服务器防火墙以启用端口3306上的传入连接
- 您必须在MySQL中拥有一个允许从
%
(任何主机)进行连接的用户(请参见- firewall of the server must be set-up to enable incomming connections on port 3306
- you must have a user in MySQL who is allowed to connect from
%
(any host) (see manual for details)
当前问题是第一个,但在解决该问题后,您很可能会得到第二个问题.
The current problem is the first one, but right after you resolve it you will likely get the second one.
这篇关于使用PHP连接到远程MySQL服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!