使用 PHP 连接到远程 MySQL 服务器 [英] Connecting to remote MySQL server using PHP

查看:47
本文介绍了使用 PHP 连接到远程 MySQL 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码从我的本地机器虚拟主机连接到远程 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆