使用 PHP 连接到远程 MySQL 服务器

发布于 2024-08-15 16:05:04 字数 468 浏览 2 评论 0原文

我正在尝试使用以下代码从本地计算机虚拟主机连接到远程 MySQL 服务器:

$conn = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
        mysql_select_db($dbname, $conn) or die(mysql_error());

我的问题是我无法在本地连接,收到错误:

无法连接到“xxx.xxx.xxx.xxx”上的 MySQL 服务器 (10060)

当我将相同的 PHP 文件上传到服务器时,情况并非如此。我可以毫无问题地查询数据库。

我也无法通过命令行连接,但我可以访问 cPanel,这排除了我的 IP 被意外禁止的可能性。

我的本地服务器运行 PHP 5.2.9,远程服务器运行 5.2.12

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:

Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)

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.

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.

My local server is running PHP 5.2.9, the remote server 5.2.12

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尝蛊 2024-08-22 16:05:04
  • 必须设置服务器的防火墙以启用端口 3306 上的传入连接
  • 您必须在 MySQL 中拥有一个允许从 %(任何主机)连接的用户(请参阅 manual 了解详细信息)

当前问题是第一个问题,但在您解决它之后您可能会得到第二个。

  • 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.

七色彩虹 2024-08-22 16:05:04

使用PHP连接远程MySQL服务器非常容易,您所要做的就是:

  1. 在远程服务器中创建一个MySQL用户。

  2. 向用户授予完全权限。

  3. 使用 PHP 代码连接到服务器(下面给出示例)

$link = mysql_connect('your_my_sql_servername or IP Address', 'new_user_which_u_created', 'password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

echo 'Connected successfully';

mysql_select_db('sandsbtob',$link) or die ("could not open db".mysql_error());
// we connect to localhost at port 3306

It is very easy to connect remote MySQL Server Using PHP, what you have to do is:

  1. Create a MySQL User in remote server.

  2. Give Full privilege to the User.

  3. Connect to the Server using PHP Code (Sample Given Below)

$link = mysql_connect('your_my_sql_servername or IP Address', 'new_user_which_u_created', 'password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

echo 'Connected successfully';

mysql_select_db('sandsbtob',$link) or die ("could not open db".mysql_error());
// we connect to localhost at port 3306
自此以后,行同陌路 2024-08-22 16:05:04

我刚刚解决了这样的问题。
我了解到的是:

  1. 您必须编辑 my.cnf 并在 [ 下设置 bind-address = your.mysql.server.address mysqld]
  2. 注释掉skip-networking字段
  3. 重新启动mysqld
  4. 检查它是否正在运行

    mysql -u root -h your.mysql.server.address –p 
    
  5. 创建一个以 % 作为域的用户(usr 或其他任何内容)并授予她对相关数据库的访问权限。

    mysql>创建由“some_pass”标识的用户“usr”@“%”;
    mysql>使用授予选项将 testDb.* 上的所有权限授予 'monty'@'%';
    
  6. 打开端口 3306 的防火墙(您可以使用 iptables。确保为每个人打开端口,或者如果您处于严格的安全状态,则只允许客户端地址)

  7. 重新启动防火墙/iptables,

您现在应该能够从客户端服务器 php 脚本连接 mysql 服务器。

I just solved this kind of a problem.
What I've learned is:

  1. you'll have to edit the my.cnf and set the bind-address = your.mysql.server.address under [mysqld]
  2. comment out skip-networking field
  3. restart mysqld
  4. check if it's running

    mysql -u root -h your.mysql.server.address –p 
    
  5. create a user (usr or anything) with % as domain and grant her access to the database in question.

    mysql> CREATE USER 'usr'@'%' IDENTIFIED BY 'some_pass';
    mysql> GRANT ALL PRIVILEGES ON testDb.* TO 'monty'@'%' WITH GRANT OPTION;
    
  6. open firewall for port 3306 (you can use iptables. make sure to open port for eithe reveryone, or if you're in tight securety, then only allow the client address)

  7. restart firewall/iptables

you should be able to now connect mysql server form your client server php script.

猥︴琐丶欲为 2024-08-22 16:05:04

这可能不是帖子问题的答案。但这可能对和我面临同样情况的人有所帮助:

客户端有两张网卡,一张无线网卡和一张普通网卡。
ping 服务器可以成功。但是 telnet serverAddress 3306 会失败。
并且会抱怨

无法连接到“xxx.xxx.xxx.xxx”上的 MySQL 服务器 (10060)

当尝试连接到服务器时, 我禁止了正常的网络适配器。
并尝试了telnet serverAddress 3306它可以工作。然后当连接到MySQL服务器时它可以工作。

This maybe not the answer to poster's question.But this may helpful to people whose face same situation with me:

The client have two network cards,a wireless one and a normal one.
The ping to server can be succeed.However telnet serverAddress 3306 would fail.
And would complain

Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)

when try to connect to server.So I forbidden the normal network adapters.
And tried telnet serverAddress 3306 it works.And then it work when connect to MySQL server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文