简体   繁体   English

无法连接到PHP中的远程SQL数据库,但是设置了远程访问并确认正常工作

[英]Can't connect to remote SQL database in PHP, but remote access is set up and confirmed working

I'm trying to learn to use SQL stuff in PHP but I'm having an issue simply connecting to my database. 我正在尝试学习在PHP中使用SQL东西,但是在连接数据库时遇到了问题。

The database is almost certainly working with remote access to some extent. 几乎可以肯定,数据库在某种程度上可以与远程访问一起使用。 I disabled iptables temporarily (I know, bad bad bad!), so there's no firewall blocking access. 我暂时禁用了iptables(我知道,不好,很糟糕!),所以没有防火墙阻止访问。 The MySQL user I am using is configured to be able to connect from any IP. 我正在使用的MySQL用户配置为可以从任何IP连接。 The SQL server is listening properly to all connections and is run on a Linux dedicated server. SQL服务器正在正确侦听所有连接,并在Linux专用服务器上运行。

[root@1742CC-XEON ~]# netstat -lpnut | grep mysql
tcp        0      0 0.0.0.0:3315                0.0.0.0:*                   LISTEN      3915/mysqld

I've also used the exact same credentials to successfully remotely connect to my database in Java, like so: 我还使用了完全相同的凭据来成功地以Java远程连接到我的数据库,如下所示:

        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://__ip__:_port_/_dbname_", "username", "password");
        System.out.println("Connection established.");

This functions just fine, and I can successfully run queries, etc. Since I have iptables disabled at the moment this connection functions just fine anywhere that has Java and the Java SQL connector. 此功能运行良好,并且我可以成功运行查询等。由于目前已禁用iptables,因此该连接在具有Java和Java SQL连接器的任何地方运行都很好。

Now, I'm trying to connect to the same database in PHP: 现在,我正在尝试连接到PHP中的同一数据库:

        $db = mysqli_connect($sql_host, $sql_user, $sql_pass, $sql_db, $sql_port);

And this connection fails with the following error: 并且此连接失败,并出现以下错误:

Warning: mysqli_connect(): (HY000/2003): Can't connect to MySQL server on 'xx.xx.xx.ip' (111)

Note that $sql_user, $sql_pass, $sql_host, etc. are all the same as the credentials used in the Java connection, so there shouldn't be any issue with the user not being able to access the server from a new IP, since that user is granted access on % and I've made sure that it is indeed able to access the SQL server from multiple IPs not explicitly allowed in the users table. 请注意,$ sql_user,$ sql_pass,$ sql_host等都与Java连接中使用的凭据相同,因此用户不能从新IP访问服务器不会有任何问题,因为该用户被授予对%的访问权限,并且我已经确保它确实能够从users表中未明确允许的多个IP访问SQL Server。 I've also tried different users, including root, and all give the same connection error. 我还尝试了不同的用户,包括root用户,并且都给出了相同的连接错误。

I've spent a few hours looking around but I honestly can't find anything that seems to be the right answer, would really appreciate any help! 我已经花了几个小时环顾四周,但老实说我找不到合适的答案,非常感谢您的帮助!

Is it possible that this is caused by my webhost? 这可能是由我的虚拟主机引起的吗? I've tried this script on HostGator webhost as well as the whois.com hosting and both give the same error. 我已经在HostGator网络主机以及whois.com主机上尝试了此脚本,并且都给出了相同的错误。 Could they possibly be preventing the PHP script from connecting to the remote database? 他们可能会阻止PHP脚本连接到远程数据库吗?

您是否尝试过从Web主机远程登录到3315上的服务器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM