首页 文章

从php拒绝MySQLi访问

提问于
浏览
2

当我尝试在PHP中连接MySQLi时,我收到以下错误:

警告:mysqli :: __ construct():( HY000 / 1045):xxxxxxxx中用户“root”@“localhost”(使用密码:YES)拒绝访问连接错误(1045)用户“root”@“localhost”访问被拒绝'(使用密码:是)

但是我可以使用完全相同的凭据登录到phpmyadmin并使用mysql控制台 .

我已授予root @ localhost所有权限 .

这是我用来连接数据库的testfile:

<?php

$mysqli = new mysqli('localhost', 'root', 'rootPassword', 'myDatabase');

if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error );
}
echo 'Connection OK';
$mysqli->close();
?>

我该怎么做才能解决这个问题?

更新:

这是新代码:

$mysqli = new mysqli('localhost', 'root', 'root', 'ignis', '3307');

if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error );
}
echo 'Connection OK';
$mysqli->close();

有了这个,我得到连接OK,但是当我尝试连接这样使用'joomla'方式连接到数据库我仍然得到错误

显示错误页面时出错:应用程序实例化错误:无法连接到MySQL .

有谁知道如何解决这个问题?

这是我的configuration.php文件的一部分,用于连接数据库:

public $dbtype = 'mysqli';
public $host = 'localhost:3307'; 
//tried using :59420 no success either but i have configured it
//to port 3307 in wamp so idk how the mysql console ever came up with port 59420.
public $user = 'root';
public $password = 'root';
public $db = 'ignis';
public $dbprefix = 'igns_';

show processlist 的结果

+----+------+-----------------+------+---------+------+----------+------------------+
| Id | User | Host            | db   | Command | Time | State    | Info             |
+----+------+-----------------+------+---------+------+----------+------------------+
| 85 | root | localhost:59420 | NULL | Query   |    0 | starting | show processlist |
+----+------+-----------------+------+---------+------+----------+------------------+

1 回答

  • 0

    经过一整天我终于明白了,

    joomla对它使用的文件很奇怪,它更喜欢在站点根目录下的configurations.php上面的joomla文件夹中的configurations.php.txt .

    为什么?

    因为在factory.php文件中有这段代码:

    public static function getConfig($file = null, $type = 'PHP', $namespace = '')
        {
            if (!self::$config)
            {
                if ($file === null)
                {
                    $file = JPATH_CONFIGURATION . '/configuration.php.txt'; 
                }
    
                self::$config = self::createConfig($file, $type, $namespace);
            }
            return self::$config;
        }
    

    其中 JPATH_CONFIGURATION = JPATH_ROOT (joomla文件夹)和 /configuration.php.txt 是硬编码的,因此忽略了/configurations.php文件 .

    但我仍然没有得到的是为什么mysql在Windows更新后突然中断?是因为有一个更新与阻塞没有指定端口的连接有关吗?因为这在Windows更新之前有效 .

相关问题