【问题标题】:Access denied for user ''@'localhost' (using password: NO) using AWS and EC2使用 AWS 和 EC2 拒绝用户 ''@'localhost' 的访问(使用密码:否)
【发布时间】:2016-06-12 22:13:28
【问题描述】:

我在使用 SHIFTEDIT IDE 尝试连接到我的运行 LAMP 服务器和 mysql 服务器的亚马逊 EC2 实例时收到以下错误。

我用PHP编写的连接我的sql服务器的代码如下:

<?php
function connect_to_database() {

$link = mysqli_connect("localhost", "root", "test", "Jet");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);

}
?>

输出:成功:与 MySQL 建立了正确的连接! my_db 数据库是 伟大的。主机信息:Localhost via UNIX socket Access denied for 用户''@'localhost'(使用密码:NO)

我确实使用了正确的 root 密码,因为我在使用 Phpmyadmin 时可以成功登录,但由于某种原因我无法与 PHP 建立连接。

目前,我有一个安装了 LAMP 服务器和 MySQL 服务器的 Amazon ec2 实例。任何帮助将不胜感激。

编辑:我使用的是 PHP 5.6.17

【问题讨论】:

  • 你在那里运行的是哪个 php 版本?作为 mysqli PHP 库,当使用 PHP 5.5 或更高版本时。
  • 如果您尝试以其他用户身份登录会发生什么?如果您还没有任何其他用户,请创建一个并尝试。
  • 为了安全起见:您能否在mysqli_close($link); 行之后插入一些像echo 'connection closed'; 这样的输出,然后向我们展示输出是什么(并编辑代码sn-p ,以便代码和输出同步)?
  • @ShashankShah 嗨,我使用的是 PHP 5.6.17
  • @SageArslan 嗨,我尝试以其他各种用户身份登录 - 我收到相同/类似的错误消息

标签: php mysql amazon-web-services amazon-ec2 root


【解决方案1】:

当您创建 mysqli 实例时(通过函数/方法中的new mysqli(...)mysqli_connect(....),您必须考虑 php 的 variable scopeReturn 函数中的 mysqli 实例并让调用者使用和/或分配该实例。

<?php
/*
builds and throws an exception from the error/errno properties of a mysqli or mysqli_stmt instance
@param useConnectError true: use connect_error/connect_errno instead
@throws mysqli_sql_exception   always does
*/
function exception_from_mysqli_instance($mysqli_or_stmt, $useConnectError=false) {
    // see http://docs.php.net/instanceof
    if ( !($mysqli_or_stmt instanceof mysqli) && !($mysqli_or_stmt instanceof mysqli_stmt) {
        // see docs.php.net/class.mysqli-sql-exception
        throw new mysqli_sql_exception('invalid argument passed');
    }
    else if ($useConnectError) {
        // ok, we should test $mysqli_or_stmt instanceof mysqli here ....
        throw new mysqli_sql_exception($mysqli_or_stmt->connect_error, $mysqli_or_stmt->connect_errno);
    }
    else {
        throw new mysqli_sql_exception($mysqli_or_stmt->error, $mysqli_or_stmt->errno);
    }

}
/* creates a new database connection and returns the mysqli instance
@throws mysqli_sql_exception   in case of error
@return valid mysqli instance
*/
function connect_to_database() {
    $link = new mysqli("localhost", "root", "test", "Jet");
    // see http://docs.php.net/mysqli.quickstart.connections
    if ( $link->connect_errno) {
        // a concept you might or might not be interested in: exceptions
        // in any case this is better than to just let the script die
        // give the other code components a chance to handle this error  
        exception_from_mysqli_instance($link, true);
    }

    return $link;
}

try { // see http://docs.php.net/language.exceptions
  // assign the return value (the mysqli instance) to a variable and then use that variable
  $mysqli = connect_to_database();

  // see http://docs.php.net/mysqli.quickstart.prepared-statements
  $stmt = $mysqli->prepare(....)
  if ( !$stmt ) {
    exception_from_mysqli_instance($stmt);
  }
  ...
}
catch(Exception $ex) {
  someErrorHandler();
}

和预感(由于实际的错误消息;尝试使用默认的 root: 连接,这是 mysql_* 函数的行为,而不是 mysqli 的行为):
不要混合使用 mysqlimysql_* 函数。

【讨论】:

    猜你喜欢
    • 2016-12-27
    • 2015-10-07
    • 2018-02-22
    • 2016-12-27
    • 2012-06-17
    • 2019-12-19
    • 2017-10-26
    相关资源
    最近更新 更多