简体   繁体   English

MySQL数据库接受远程连接,但不接受PHP

[英]MySQL Database accepting remote connections, but not from PHP

So I setup a MySQL server on a dedicated server. 因此,我在专用服务器上设置了MySQL服务器。 I attempted to connect to it with this PHP script. 我试图用此PHP脚本连接到它。

<?php
$servername = "******";
$username = "******";
$password = "******";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

Yes the ***** in my script are replaced with the proper information. 是的,我的脚本中的*****被替换为正确的信息。

The Connection was refused with this error 连接被拒绝,出现此错误

Connection failed: Can't connect to MySQL server on '66.45.230.186' (111)

So I tried it from another host, I used http://www.rainbowspuppiessunshine.com/tools/dbtest/index.php 所以我从另一台主机尝试过,我使用了http://www.rainbowspuppiessunshine.com/tools/dbtest/index.php

From here I also got an error : 从这里我也有一个错误:

Lost connection to MySQL server at 'reading initial communication packet', system error: 111

So I tried to connect to the MySQL from my VPS. 所以我试图从我的VPS连接到MySQL。 I installed MySQL on it and made a connection through users "root" and "steam" and was able to access my database and add tables through that. 我在上面安装了MySQL,并通过用户“ root”和“ steam”建立了连接,并且能够访问我的数据库并通过它添加表。

The VPS is hosted by the same host as my Dedicated server (running my MySQL server) so I figured perhaps since it was on the same LAN that might be why it was allowing a connection. VPS与我的专用服务器(运行我的MySQL服务器)由同一主机托管,因此我认为可能是因为它位于同一LAN上,这可能就是为什么允许连接的原因。 So I decided to install a MySQL client on my local machine (HeidiSQL 9.1). 因此,我决定在本地计算机(HeidiSQL 9.1)上安装MySQL客户端。 I was able to make a connection with that client from my local machine (500 miles away from my Dedicated Server) 我能够从本地计算机(距我的专用服务器500英里)与该客户端建立连接

So I am absolutely clueless as to why I am able to connect to the MySQL remotely from another MySQL client, but I am unable to connect to it through any PHP script. 因此,对于为什么我能够从另一个MySQL客户端远程连接到MySQL,我绝对一无所知,但是我无法通过任何PHP脚本连接到它。 I really didn't know there was much difference. 我真的不知道有什么区别。

The MySQL server is a fresh install (other than setting up permissions for remote connections and adding a database with two tables). MySQL服务器是全新安装(除了为远程连接设置权限和添加具有两个表的数据库之外)。 It's running on Ubuntu 14.04 它在Ubuntu 14.04上运行

+------------------+-------------------------------------------+---------------+
| user             | password                                  | host          |
+------------------+-------------------------------------------+---------------+
| root             | *367201834FD28DD7137E8947B4E1A1F5CAC4BBB4 | localhost     |
| root             | *367201834FD28DD7137E8947B4E1A1F5CAC4BBB4 | myserver      |
| root             | *367201834FD28DD7137E8947B4E1A1F5CAC4BBB4 | 127.0.0.1     |
| root             | *367201834FD28DD7137E8947B4E1A1F5CAC4BBB4 | ::1           |
| debian-sys-maint | *AA9A40B349EDF9989A118FEFCF255353033F4A6D | localhost     |
| steam            | *367201834FD28DD7137E8947B4E1A1F5CAC4BBB4 | **.**.***.*** |
| steam            | *367201834FD28DD7137E8947B4E1A1F5CAC4BBB4 | %             |
| root             | *367201834FD28DD7137E8947B4E1A1F5CAC4BBB4 | %             |
+------------------+-------------------------------------------+---------------+

. . . was edited to hide the IP 被编辑以隐藏IP

Firstly,please check the mysql and mysqli support in php . 首先,请检查php中的mysql和mysqli支持。 Create index.php at your web location (/var/www/html),Following is the file content. 在您的网站(/ var / www / html)中创建index.php,以下是文件内容。

<?php
 echo phpinfo();
?>

access index.php at the web browser( http://localhost/index.php ) and check the mysql and mysqli item is exist or not . 在Web浏览器( http://localhost/index.php )上访问index.php并检查mysql和mysqli项目是否存在。 在此处输入图片说明

if not , please re-install the php mysql support , in your case use apt-get command to install them. 如果不是,请重新安装php mysql support,在这种情况下,请使用apt-get命令进行安装。

Secondly , please login into you mysql and check the remote connections status. 其次,请登录到您的mysql并检查远程连接状态。

// in command line
mysql -u your user -p your password
// execute following sql
select user,password,host from mysql.user;

following is the result 以下是结果 在此处输入图片说明

then please check the host column, if this column values are the specific ip or hostname(localhost,192.168.xxx.xxx etc),please change to any client can connect it. 然后请检查主机列,如果此列值是特定的ip或主机名(localhost,192.168.xxx.xxx等),请更改为任何客户端都可以连接它。 Assume your database connections info like following 假设您的数据库连接信息如下

database  demo
user  demo
password demo
//execute the sql to change
grant all on demo.* to demo@'%' identified by 'demo';
flush privileges;

after above steps done and please try to the connect mysql from php again. 完成上述步骤后,请尝试再次从php连接mysql。

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

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