1

此代码从我的 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();
?>
4

2 回答 2

0

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

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

在您的调试中它使用。

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

这些必须是相同的变量。如果未填写,系统默认可能使用@localsystem.com。

于 2013-07-14T18:39:32.837 回答
0

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

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

改变:

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

SchemaName.*

username给您想要访问的用户

ip更改为服务器所在的IP

identified by password代替password用户密码。

然后发出:

FLUSH PRIVILEGES

或者重启整个mysql服务

于 2013-06-11T22:16:47.093 回答