Php 拒绝用户访问''@';本地主机';(使用密码:否)使用AWS和EC2
当使用ShifteditIDE尝试连接到运行LAMP服务器和mysql服务器的AmazonEC2实例时,我遇到以下错误 我用PHP编写的连接到sql server的代码如下:Php 拒绝用户访问''@';本地主机';(使用密码:否)使用AWS和EC2,php,mysql,amazon-web-services,amazon-ec2,root,Php,Mysql,Amazon Web Services,Amazon Ec2,Root,当使用ShifteditIDE尝试连接到运行LAMP服务器和mysql服务器的AmazonEC2实例时,我遇到以下错误 我用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
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服务器的AmazonEC2实例。任何帮助都将不胜感激
编辑:我使用的是PHP5.6.17当你在一个函数/方法中创建一个mysqli实例(通过
newmysqli(…)
或mysqli\u 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();
}
在函数/方法中创建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();
}
您在那里运行的是哪个php版本?当使用php 5.5或更高版本时,作为mysqli php库。如果您尝试以其他用户身份登录,会发生什么情况?创建一个,如果您还没有其他用户,请尝试。为了安全起见:您可以在mys之后插入一些输出,如echo'connection closed';
qli_关闭($link);
行,然后向我们显示输出内容(同时编辑代码段,以便代码和输出同步)?@ShashankShah嗨,我用的是PHP5.6。17@SageArslan您好,我尝试过以各种其他用户的身份登录-当使用php 5.5或更高版本时,我会收到与您在那里运行的php版本相同/类似的错误消息?与mysqli php库相同。如果您尝试以其他用户的身份登录,会发生什么情况?创建一个,如果没有其他用户,请尝试然而,为了安全起见:你能在mysqli_close($link);
行之后插入一些输出,比如echo'connection closed';
,然后告诉我们输出是什么(同时编辑代码片段,以便代码和输出同步)?@ShashankShah嗨,我用的是PHP5.6。17@SageArslan您好,我尝试过以其他用户的身份登录-我收到了相同/类似的错误消息