gpt4 book ai didi

php - 用户 '' @'localhost' 的访问被拒绝(使用密码 : NO) using AWS and EC2

转载 作者:行者123 更新时间:2023-11-30 22:24:25 28 4
gpt4 key购买 nike

当我使用 SHIFTEDIT IDE 尝试连接到运行 LAMP 服务器和 mysql 服务器的亚马逊 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);

}
?>

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)

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

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

编辑:我正在使用 Php 5.6.17

最佳答案

当你在函数/方法中创建一个 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 的行为):
不要 混合 mysqli mysql_* 函数。

关于php - 用户 '' @'localhost' 的访问被拒绝(使用密码 : NO) using AWS and EC2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35706902/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com