通过PHP访问远程数据库MySQL [英] Access remote database MySQL via PHP

查看:53
本文介绍了通过PHP访问远程数据库MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 000webhost.com 上设置了一个 MySQL 数据库.我想通过 PHP 访问我的数据库中的数据.我试过了:

I have a MySQL database set up on 000webhost.com. I want to access the data in my database via PHP. I tried:

<?php 

include("connect.php");
mysql_select_db("XXXXXXX_users", $con) or die(mysql_error()); 

$result = mysql_query("SELECT * FROM data ORDER BY id DESC");
while($row = mysql_fetch_array($result))     
{
    $id = $row['id'];
    $user = $row['usrname'];
} 


echo "$user";
?>

但总是返回:连接尝试失败,因为连接方在一段时间后没有正确响应,或者因为连接的主机没有响应而建立的连接失败."

But it always returns as: "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."

我该怎么办?

  • 我没有根访问权限
  • 我正在尝试在 Apache 上运行脚本

推荐答案

在调试这类事情时,最好先确保您可以使用命令行 mysql 工具进行连接,以排除与 PHP 本身相关的问题.

When debugging this sort of thing, it's always good to make sure you can connect using the command line mysql tool first, to rule out issues related to PHP itself.

$ mysql -u myuser -h mysql.example.com -p

您还会收到更具描述性的错误消息,例如ERROR 1045 (28000): Access denied for user 'root'@'remote.example.com'(使用密码:YES)

You'll get a more descriptive error message as well, e.g. ERROR 1045 (28000): Access denied for user 'root'@'remote.example.com' (using password: YES)

如果 mysql 端口甚至在公共接口上打开(这通常是一个糟糕的主意),mysql 本身具有基于连接主机的访问控制层.您可能需要使用通配符主机名专门创建一个帐户,如下所示:

If the mysql port is even open on a public interface to begin with (which is usually a terrible idea), mysql itself has its own layer of access control based on the connecting host. You may need to have an account created specifically with a wildcard hostname, like so:

CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypassword';

@'%' 部分,特别是允许来自任何主机,本地或远程的连接.

The @'%' part, specifically, allows connection from any host, local or remote.

一旦您可以从命令行成功连接,那么将您的命令行参数复制为 mysql_connect()

Once you can successfully connect from the command line, it's then a simple issue of replicating your command line arguments as arguments to mysql_connect()

这篇关于通过PHP访问远程数据库MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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