Why is my php code not connecting to my remote MySql database?

后端 未结 2 968
天命终不由人
天命终不由人 2021-01-29 11:35

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

Warning: mysqli_connect(): (HY000/2002): A connection attempt failed because the connecte

2条回答
  •  孤城傲影
    2021-01-29 11:56

    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.

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

    ping servername
    

    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
    

    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.

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

    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

提交回复
热议问题