使用AWS和EC2

时间:2016-02-29 18:31:42

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

使用SHIFTEDIT IDE尝试连接到运行LAMP服务器和mysql服务器的amazon 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'(使用密码:否)

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

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

编辑:我正在使用Php 5.6.17

1 个答案:

答案 0 :(得分:0)

当您在函数/方法中创建一个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:&lt; nopassword&gt;连接,这是mysql_ *函数的行为,而不是mysqli的行为):
混合mysqli mysql _ * 函数。