我使用mysql做了一些项目,没有问题。今天我的朋友告诉我如何在mysql中创建用户:
Create database cc;
use cc
CREATE USER 'testing'@'localhost' IDENTIFIED BY 'pass';
GRANT ALL ON cc.* TO 'testing'@'localhost';
现在我尝试使用我的项目但是我遇到了以下错误:
Connection failed: Access denied for user ''@'localhost' to database 'login'
我删除了之前创建的用户:
drop user 'testing'@'localhost';
我设法删除了该用户,我检查了用户:
select User from mysql.user;
测试帐户已经消失,但现在我有3个root帐户,我不明白为什么。由于我删除了“测试”帐户,我再次尝试了我的项目,我得到了同样的错误。为什么我不能再使用默认的'root'
帐户了?
编辑:
php代码的错误部分:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "login";
// Error is in following line
$conn = mysqli_connect ( $servername, $username, $password, $dbname );
我也试过这个:
GRANT ALL ON login.* TO 'root'@'localhost';
show grants;
结果是:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION
GRANT ALL PRIVILEGES ON login.* TO 'root'@'localhost'
似乎'root'可以访问所有内容。为什么我会收到错误?
编辑2:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "login";
$username = $_POST['username'];
$pass = $_POST['password'];
// Create connection
$conn = mysqli_connect ( $servername, $username, $password, $dbname );
// Check connection
if (! $conn) {
die ( "Connection failed: " . mysqli_connect_error () );
}
答案 0 :(得分:0)
您使用$ _POST ['username']数据覆盖之前设置的$ username变量,该数据似乎为空。因此,mysqli_connect尝试使用空白用户名连接到数据库。
要更正此问题,请将$ username变量重命名为其他不冲突的内容。
示例:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "login";
$username_post = $_POST['username'];
$pass = $_POST['password'];
// Create connection
$conn = mysqli_connect ( $servername, $username, $password, $dbname );
// Check connection
if (! $conn) {
die ( "Connection failed: " . mysqli_connect_error () );
}