为什么 MySQL 不喜欢用户“a”@“b”?当我刚刚从 mysql.user 中删除它们时?

发布于 2024-09-18 18:29:32 字数 1573 浏览 3 评论 0原文

这就是我所做的。

mysql> CREATE USER 'matrix'@'%.something.com' IDENTIFIED BY 'weak';
ERROR 1396 (HY000): Operation CREATE USER failed for 'matrix'@'%.something.com'
mysql> CREATE USER 'foo'@'%.something.com' IDENTIFIED BY 'weak';
Query OK, 0 rows affected (0.00 sec)

这就是我最终得到的结果:

mysql> select user,host from mysql.user;
+------------------+--------------------+
| user             | host               |
+------------------+--------------------+
| auth             | %.something.com    |
| foo              | %.something.com    |
| submit           | %.something.com    |
| testcase         | %.something.com    |
| version          | %.something.com    |
| root             | 127.0.0.1          |
| root             | this.something.com |
| debian-sys-maint | localhost          |
| master           | localhost          |
| root             | localhost          |
+------------------+--------------------+
10 rows in set (0.00 sec)

看来问题是我通过 DELETE FROM mysql.user WHERE User IN (%s); 删除了用户来解决我不知道的问题不知道帐户是否存在。

我的(丑陋的)解决方案,将 DELETE FROM mysql.user WHERE... 替换为:

DELIMITER //
CREATE PROCEDURE dropuserif(u VARCHAR(32), h VARCHAR(64))
BEGIN
  DECLARE s INT;
  SELECT COUNT(*) INTO s FROM mysql.user WHERE user = u AND host = h;
  IF s > 0 THEN
    BEGIN
      SET @d = CONCAT("DROP USER '", u, "'@'", h, "';");
      PREPARE stmt_name FROM @d;
      EXECUTE stmt_name;
      DEALLOCATE PREPARE stmt_name;
    END;
  END IF;
END//
DELIMITER ;

This is what I did.

mysql> CREATE USER 'matrix'@'%.something.com' IDENTIFIED BY 'weak';
ERROR 1396 (HY000): Operation CREATE USER failed for 'matrix'@'%.something.com'
mysql> CREATE USER 'foo'@'%.something.com' IDENTIFIED BY 'weak';
Query OK, 0 rows affected (0.00 sec)

And this is what I end up with:

mysql> select user,host from mysql.user;
+------------------+--------------------+
| user             | host               |
+------------------+--------------------+
| auth             | %.something.com    |
| foo              | %.something.com    |
| submit           | %.something.com    |
| testcase         | %.something.com    |
| version          | %.something.com    |
| root             | 127.0.0.1          |
| root             | this.something.com |
| debian-sys-maint | localhost          |
| master           | localhost          |
| root             | localhost          |
+------------------+--------------------+
10 rows in set (0.00 sec)

It seems that the issue is that I removed users via a DELETE FROM mysql.user WHERE User IN (%s); to get around the problem that I don't know if the accounts exist.

My (ugly) solution, replace the DELETE FROM mysql.user WHERE... with:

DELIMITER //
CREATE PROCEDURE dropuserif(u VARCHAR(32), h VARCHAR(64))
BEGIN
  DECLARE s INT;
  SELECT COUNT(*) INTO s FROM mysql.user WHERE user = u AND host = h;
  IF s > 0 THEN
    BEGIN
      SET @d = CONCAT("DROP USER '", u, "'@'", h, "';");
      PREPARE stmt_name FROM @d;
      EXECUTE stmt_name;
      DEALLOCATE PREPARE stmt_name;
    END;
  END IF;
END//
DELIMITER ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心清如水 2024-09-25 18:29:32

在网络上搜索“错误 1396”会出现此错误报告

原因似乎是先前删除的用户条目仍然以某种方式隐藏在数据库中,并阻止创建同名的新帐户。

在第一个语句之前尝试 FLUSH PRIVILEGES; 并再次运行它们。

A web search for "error 1396" turns up this bug report.

The cause seems to be previously deleted user entries still somehow latent in the database, and blocking the creation of new accounts with the same name.

Try a FLUSH PRIVILEGES; before the first statement and run them again.

空城仅有旧梦在 2024-09-25 18:29:32

请查看此处,以获得解决此问题的良好开端

[snark]
您是否有理由不直接搜索“mysql ERROR 1396”?
[/snark]'

look here for a good start towards this issue

[snark]
Is there a reason you didn't just search for 'mysql ERROR 1396'?
[/snark]'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文