当我尝试在我的 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。
为什么?
因为在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 更新之前有效。
关于php - MySQLi 访问被 php 拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40532046/