我正在尝试通过php脚本连接到MySQL数据库。我已经尝试了一切,但最终总是: “连接失败:用户'用户'@'localhost'拒绝访问(使用密码:是)”
我可以在命令行中连接到MySQL:
mysql -u user -p -h localhost
这是php脚本的一个例子:
<?php
$DB_HOST = "localhost";
$DB_USER = "user";
$DB_PASS = "pass";
$DB_NAME = "dbname";
// Create connection
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
?>
这些是用户在数据库中设置的权限:
本地主机:
GRANT USAGE ON *.* TO 'user'@'localhost' IDENTIFIED BY PASSWORD '*<numbers>'
GRANT ALL PRIVILEGES ON `dbName`.* TO 'user'@'localhost'
'%':
GRANT USAGE ON *.* TO 'user'@'%' IDENTIFIED BY PASSWORD '*<numbers>'
GRANT ALL PRIVILEGES ON `dbName`.* TO 'user'@'%'
我在我的系统上运行MAMP。
我是否还缺少其他特权?