PHP-MYSQL脚本无法连接到服务器上的数据库

时间:2014-06-30 07:57:25

标签: php mysql apache security web

这是我写的PHP代码:

<?php
$con=mysqli_connect("193.167.138.9","username","password","DatabaseName");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$t_name = mysqli_real_escape_string($con, $_POST['name']);
$t_email = mysqli_real_escape_string($con, $_POST['email']);
//$age = mysqli_real_escape_string($con, $_POST['age']);
$ip = getenv("REMOTE_ADDR") ;
$date_time = date("l j F Y g:ia", time()- date("Z")) ;

$sql="INSERT INTO IP_TIME (Name, MAIL_ADDRESS,IP_ADDRESS,DATE_TIME)
VALUES ('$t_name', '$t_email', '$ip', '$date_time')";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
echo "Check the IP_TIME table";

mysqli_close($con);
?>

这里193.167.138.9是我正在使用的服务器的IP。最初当我在我的本地机器上测试它时,在“193.167.138.9”的位置,它是“localhost。

当我尝试在服务器上运行代码时,它会显示以下错误消息。

Failed to connect to MySQL: Can't connect to MySQL server on '193.167.138.9' (111)Error: 

知道出了什么问题吗?我该如何解决?

我在mysql配置文件中检查了它,特别是my.conf文件,如下所示:

[mysqld]
#
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#
bind-address           = 127.0.0.1

不确定是什么阻止了mysql的连接。任何帮助/建议都会有所帮助

4 个答案:

答案 0 :(得分:0)

您必须将绑定地址更改为

bind-address           = 193.167.138.9

答案 1 :(得分:0)

您应该像这样评论bind-address

# bind-address = 127.0.0.1

或使用您的SERVER IP地址更改bind-address

bind-address = 193.167.138.9

答案 2 :(得分:0)

只是一个想法,但您是否已将用户配置为能够远程连接到数据库?例如。将主机配置为&#39;%&#39;对于任何主机(因为你提到你之前用localhost测试过它)?

答案 3 :(得分:0)

您需要在服务器计算机上使用请求的计算机IP地址在mysql上创建用户,如下所示

grant all privileges on *.* to 'user'@'171.15.12.3' identified by 'password';