使用AWS和EC2拒绝用户''@ localhost'(使用密码:NO)的访问权限

时间:2022-04-15 21:00:56

I am getting the following error when using SHIFTEDIT IDE to try connect to my amazon EC2 instance running LAMP server and mysql server.

使用SHIFTEDIT IDE尝试连接到运行LAMP服务器和mysql服务器的amazon EC2实例时出现以下错误。

The code I am writing in PHP to connect to my sql server is as followed:

我用PHP编写的连接到我的sql server的代码如下:

<?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);

}
?>

OUTPUT: Success: A proper connection to MySQL was made! The my_db database is great. Host information: Localhost via UNIX socket Access denied for user ''@'localhost' (using password: NO)

输出:成功:与MySQL建立了正确的连接! my_db数据库很棒。主机信息:通过UNIX套接字的Localhost访问被拒绝用户''@ localhost'(使用密码:否)

I am definitely using the right password for root as I can successfully login when using Phpmyadmin, I just cannot make a connection with PHP for some reason.

我肯定使用正确的root密码,因为我可以在使用Phpmyadmin时成功登录,因为某些原因我无法与PHP建立连接。

Currently, I have a single Amazon ec2 instance with LAMP server and a MySQL server installed. Any help will be much appreciated.

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

EDIT: I am using Php 5.6.17

编辑:我使用的是PHP 5.6.17

1 个解决方案

#1


0  

When you create a mysqli instance (either by new mysqli(...) or mysqli_connect(....) within a function/method, you have to take php's variable scope into account. Return the mysqli instance from the function and let the caller use and/or assign that instance.

当你在函数/方法中创建一个mysqli实例(通过新的mysqli(...)或mysqli_connect(....)时,你必须考虑php的变量范围。从函数中返回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();
}

and a hunch (because of the actual error message; trying to use the default root:<nopassword> connection, that's the behaviour of the mysql_* functions, not of mysqli):
Do not mix mysqli and mysql_* functions.

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

#1


0  

When you create a mysqli instance (either by new mysqli(...) or mysqli_connect(....) within a function/method, you have to take php's variable scope into account. Return the mysqli instance from the function and let the caller use and/or assign that instance.

当你在函数/方法中创建一个mysqli实例(通过新的mysqli(...)或mysqli_connect(....)时,你必须考虑php的变量范围。从函数中返回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();
}

and a hunch (because of the actual error message; trying to use the default root:<nopassword> connection, that's the behaviour of the mysql_* functions, not of mysqli):
Do not mix mysqli and mysql_* functions.

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