2016-02-29 81 views
1

拒绝用户'@'localhost'(使用密码:NO)的访问当使用SHIFTEDIT IDE尝试连接到运行LAMP服务器和mysql服务器的amazon EC2实例时,出现以下错误。使用AWS和EC2

我在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); 

} 
?> 

输出:成功:到MySQL连接正确制成! my_db数据库很好,是 。主机信息:本地主机通过UNIX套接字访问被拒绝的 用户“” @“localhost”的(使用密码:NO)

我肯定使用了正确的密码根为使用phpMyAdmin当我可以登录成功,我只是出于某种原因无法与PHP建立连接。

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

编辑:我使用PHP 5.6.17

+0

你在哪个php版本上运行?作为mysqli PHP库,使用PHP 5.5或更高版本时。 –

+0

如果您尝试以其他用户身份登录,会发生什么情况?如果您还没有其他用户,请创建一个并尝试使用它。 –

+0

只是为了安全起见:你可以在'mysqli_close($ link);'行之后插入一些输出,像'echo'connection closed';'然后告诉我们输出结果是什么(并且编辑代码代码片段,以便代码和输出同步)? – VolkerK

回答

0

当你创建一个实例mysqli的(通过一个函数/方法中new mysqli(...)mysqli_connect(....),你必须要PHP的variable scope考虑Return和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(); 
} 

和直觉(因为实际的错误信息;尝试使用默认的根:<NOPASSWORD>连接,这就是mysql_的行为*本功能离子,而不是mysqli):
mix mysqli mysql_ * 功能。