从 MySQL 删除用户不起作用

Drop user from MySQL doesn't work

我正在尝试从 MySQL 服务器中删除用户:

$conn = new PDO("mysql:host=$host;port=$port;dbname=mysql;charset=utf8","$adminname", "$pass",array( PDO::ATTR_PERSISTENT => true));
$sql_deleteuser="DELETE FROM `mysql`.`user` WHERE `user`.`User` = '$username'";                 
//$sql_deleteuser="SELECT `User` FROM `mysql`.`user`";  
$PDOStatement3=$conn->prepare($sql_deleteuser);
//$PDOStatement3->bindParam(':username', $username, PDO::PARAM_STR);   
$res_exec=$PDOStatement3->execute();

但它不起作用并且 errorInfo() 中没有错误,我试过了

$sql_deleteuser="DROP USER '$username'@'%'";

$sql_deleteuser="IF EXISTS (SELECT * FROM sys.database_principals WHERE name = N'$username') DROP USER [$username]"; 相同的结果。

连接很好我用 SELECT 查询测试它并且它有效。

我是不是漏掉了什么。任何帮助将不胜感激。

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//Edit: Use REVOKE to revoke the privileges. This removes rows for the account from all the grant tables except the user table, and revokes any global privileges listed in the user table.

// sql to delete a record, use $host to remove a specific user
$sql = "DELETE FROM `mysql`.`user` WHERE `user`.`User` = '$username" AND `user`.`Host` = '$host";


// use exec() because no results are returned
$conn->exec($sql);
echo "Record deleted successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}

尝试上面的代码,你也会发现错误!

要么您没有删除用户的特权/权利

The DROP USER statement removes one or more MySQL accounts. To use it, you must have the DELETE privilege for the mysql database

可能问题是用户打开了连接 因为 mysql 文档说:

Important

DROP USER does not automatically close any open user sessions. Rather, in the event that a user with an open session is dropped, the statement does not take effect until that user's session is closed. Once the session is closed, the user is dropped, and that user's next attempt to log in will fail. This is by design.

如何终止连接看到这个答案:

how to kill mySQL connections

参见文档:

https://dev.mysql.com/doc/refman/5.0/en/drop-user.html