php - MySQLi 访问被 php 拒绝

标签 php mysql mysqli joomla access-denied

当我尝试在我的 PHP 中连接 MySQLi 时,出现以下错误:

Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) in xxxxxxxx

Connect Error (1045) Access denied for user 'root'@'localhost' (using password: YES)

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

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

这是我用来连接数据库的测试文件:

<?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();

有了这个,我得到了连接正常,但是当我尝试使用连接到数据库的“joomla”方式这样连接时,我仍然得到错误

Error displaying the error page: Application Instantiation Error: Could not connect to 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 |
+----+------+-----------------+------+---------+------+----------+------------------+

最佳答案

经过一整天的努力,我终于弄明白了,

joomla 对它使用的文件很奇怪,它更喜欢 joomla 文件夹中的 configurations.php.txt,高于站点根目录中的 configurations.php。

为什么?

因为在fa​​ctory.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 更新之前有效。

关于php - MySQLi 访问被 php 拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40532046/

相关文章:

php - php负载均衡

php - 如果列具有逗号分隔值并包含字符串的任何字符,如何在 mysql 中查找结果

php - 从多个表中选择-PHP

MySQL 双连接

php - 错误:警告:mysqli_query()期望参数1为mysqli

php - 如何使用/不使用循环php随机获取和显示结果

php - 为什么成员变量通常是私有(private)的?

php - Laravel5 - 不应静态调用非静态方法

php - 在一个 SQL 列中保存多个元素的好方法?

mysql - 如何更改MySQL的用户名和密码?