为什么我的php代码未连接到我的远程MySql数据库? [英] Why is my php code not connecting to my remote MySql database?

查看:196
本文介绍了为什么我的php代码未连接到我的远程MySql数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到远程MySql数据库,并且收到此错误消息:

I'm trying to connect to a remote MySql database and I get this error message:

警告:mysqli_connect():(HY000/2002):连接尝试失败,因为一段时间后被连接方未正确响应,或者由于连接的主机未能响应而建立的连接失败.在第9行找不到资源"错误404中的C:\ myLocalDiectory \ RemoteConn.php中.

Warning: mysqli_connect(): (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\myLocalDiectory\RemoteConn.php on line 9 "Resource not found" error 404.

这是我要使用的php文件:

Here's my php file that I'm trying to use:

 <?php
     define("DB_SERVER", "serverName");
     define("DB_USER", "userName");
     define("DB_PASS", "password");
     define("DB_NAME", "dbName");


    $connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);

     if(mysqli_connect_errno()) {
            die("Database connection failed: " . 
            mysqli_connect_error() . 
             " (" . mysqli_connect_errno() . ")"
      );
   }
 ?>

我错过了什么吗?感谢您的帮助!

Am I missing something? Thanks for any help!

推荐答案

我要检查的第一件事(如果尚未检查)是实际上可以从运行PHP的计算机连接到数据库脚本.这可以排除网络或防火墙问题.

The first thing that I would check (if you haven't done so) is that you can in fact connect to the database from the computer that runs your PHP script. This to rule out a network or firewall problem.

第一件事是对服务器执行ping操作.在DOS提示符下运行:

The first thing would be pinging the server. In a DOS prompt run:

ping servername

其中服务器名"与您在上面的PHP脚本中输入的字符串相同.如果此命令没有使用类似于下面的字符串来答复,特别是第一个单词不是"Reply":

Where "servername" is the same string that you put in your PHP script above. If this does not reply with a string similar to the one below, specifically, the first word is not "Reply":

Reply from 192.168.239.132: bytes=32 time=101ms TTL=124

这意味着运行PHP脚本的计算机与mysql服务器之间很可能没有连接.然后,我将检查服务器和计算机是否正确连接到网络,服务器是否启动,运行PHP脚本的计算机上或服务器上是否没有防火墙.

This means that there is most likely no connectivity between the computer running the PHP script an the mysql server. I would then check if the server and the computer are properly connected to the network, if the server is up, an if there is not firewall in your computer running the PHP script or on the server.

现在,如果上面的测试显示对ping的答复",则可以测试是否可以从php服务器连接到Mysql服务.为此,您可以使用Mysql工作台( http://dev.mysql.com/downloads/workbench/),然后在其中创建与您要提供给脚本的数据库参数的连接.如果无法与Mysql工作台连接,则可能需要禁用Mysql服务器中的防火墙,运行PHP的计算机中的防火墙,或启用Mysql服务器以接受在PHP脚本中使用的数据库和用户名的远程连接(为了安全起见,某些发行版的Mysql服务器已安装为仅接受本地连接.

Now, if your test above shows "Reply" to the ping, you can test if you can connect to the Mysql service from your php server. For this you can use Mysql workbench (http://dev.mysql.com/downloads/workbench/) and from there create a connection with the database parameters that you are giving to your script. If you cannot connect with Mysql workbench, you might need to disable a firewall in your Mysql server, a firewall in your computer running PHP, or enable the Mysql server to accept remote connections for the database and username that you use in your PHP script (some distributions Mysql server are installed to only accept local connections for safety).

如果问题是服务器中的权限(例如,用户只能在本地连接,而不能从远程计算机进行连接),则可以使用GRANT命令在Mysql服务器中启用该权限:

If the problem is a permission in the server (the user can only connect locally but not from a remote computer for instance), you can enable the permission in the Mysql server with the GRANT command: http://dev.mysql.com/doc/refman/5.1/en/grant.html

这篇关于为什么我的php代码未连接到我的远程MySql数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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