PHP mysqli->连接到远程主机使用用户名插入本地主机名

时间:2013-06-11 22:08:02

标签: php mysqli remote-access

此代码从我的Mac运行,但它不是从托管服务器运行的。错误消息是:

Tue, 11 Jun 2013 14:46:48 -0700
Remote host: my_remoteserver.com
Remote user: my_user
Remote db: my_remotedbname
mysqli Failed to connect to MySQL: Access denied for user 'my_user'@'localsystem.com' (using password: YES)

由于某种原因,本地apache / php系统将“@ localsystem.com”附加到我的用户ID。我可以调整哪些配置参数来防止这种情况?

从我的Mac运行时,输出正如预期的那样:

Tue, 11 Jun 2013 18:03:41 -0400
mysqli Connection was OK!
Show databases returned 2 rows.
information_schema ()
my_remotedatabase ()

代码:

<?php
$hostname = "my_remoteserver.us";
$user = "my_user";
$password = "_today0613";
$dbname   = "my_remotedbname";

echo date(r, time());
echo "Remote host: " . $hostname . "<br/>";
echo "Remote user: " . $username . "<br/>";
echo "Remote db: " . $dbname . "<br/>";


$con=mysqli_connect($hostname,$user,$password,$dbname);

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "mysqli Failed to connect to MySQL: " . mysqli_connect_error();
  } else { echo "mysqli Connection was OK!<br />\n";}

/* Select queries return a resultset */
if ($result = $con->query("SHOW databases")) {
    printf("Show databases returned %d rows.<br />\n", $result->num_rows);

    /* fetch object array */
    while ($row = $result->fetch_row()) {
        printf ("%s (%s)<br />\n", $row[0], $row[1]);
    }
    /* free result set */
    $result->close();

    }
$con->close();
?>

2 个答案:

答案 0 :(得分:0)

您是否为用户启用了远程访问?如果没有,请以root身份或其他管理用户身份登录到您的mysql并发出:

GRANT ALL PRIVILEGES ON *.* TO  ‘USERNAME’@‘IP’  IDENTIFIED  BY  ‘PASSWORD’;

更改:

*.*代表所有模式,所有表格。对一个模式的特定访问权限(推荐):

SchemaName.*

username给您想要访问的用户

ip更改为服务器所在的IP

identified by passwordpassword替换为用户密码。

然后发出:

FLUSH PRIVILEGES

或重启整个mysql服务

答案 1 :(得分:0)

好的,这是一个问题。可能是原因。

$con=mysqli_connect($hostname,$user,$password,$dbname);

在你的调试中使用。

echo "Remote user: " . $username . "<br/>";

这些必须是同一个变量。默认情况下,系统可能使用@ localsystem.com(如果未填充)。