mysql - 在 mysql 中,删除/添加用户时出现奇怪的结果

标签 mysql security permissions

有一个安全例程,从一组用户名、密码和权限

  • 放弃一个用户
  • 创建(相同的)用户
  • 授予对表、过程等的访问权限
// $users - Array of user records
// $tables - list of tables from `show tables`

// create users as necessary, reset password
foreach( $users as $user=>$arr ) {
        mysql_query( "drop user {$arr['user']}" );
        mysql_query( "create user {$arr['user']} identified by '{$arr['pwd']}'" );
};

// for each user set permission
foreach( $users as $user=>$arr ) {
     if( @$arr['table_grant'] ) foreach( $tables as $table )
        mysql_query( "grant {$arr['table_grant']} on $table to {$arr['user']}" );
     if( @$arr['tables_revoke'] ) foreach( $arr['tables_revoke'] as $table )                
        mysql_query( "revoke {$arr['table_grant']} on $table from {$arr['user']}" );
     if( @$arr['procedures_grant'] ) foreach( $arr['procedures_grant'] as $proc 
        mysql_query( "grant execute on procedure $proc to {$arr['user']}" );
     if( @$arr['functions_grant'] ) foreach( $arr['functions_grant'] as $func )
        mysql_query( "grant execute on function $func to {$arr['user']}" );
}

如果没有用户存在,它工作正常。

如果用户已经存在,该用户就在那里,但没有密码除了最后创建的用户。

mysql> select user, password from mysql.user where user like 'm%';
+------------+-------------------------------------------+
| user       | password                                  |
+------------+-------------------------------------------+
| massage150 | *21A196706D99436A1C8163A98AE0687C432C37E2 |
| mlogin     | *DE069F0ED7E311D173CB01C3DD136D282E66048D |
| mselect    |                                           |
| mdml       |                                           |
+------------+-------------------------------------------+
4 rows in set (0.00 sec)

在处理用户和权限时,是否需要在 MySQL 中运行某种sync 进程?我是否需要删除它们,等待几秒钟,然后重新创建?或者什么?

(代码在 postgreSQL 和 Oracle 中运行良好。针对 MySQL 对 GRANT 和 REVOKE 的限制进行了一些修改,即 GRANT 是逐表(而不是 SCHEMA 范围)进行的,因此随后发出的 REVOKE 将正常工作。 )


KLUDGE 解决方案

为用户设置 GRANT 和 REVOKE 后,再通过用户和 SET PASSWORD 传递一次。丑陋,但它有效。

仍在寻求更好地了解正在发生的事情。

正确的 KLUDGE 解

根据下面的回答(谢谢!)MySQL 有一个非 ANSI 命令 FLUSH PRIVILEGES。在 DROP USER 之后和 CREATE USER 之前发布,MySQL 不再删除用户密码。

最佳答案

尝试在 CREATE USER 语句之后执行 FLUSH PRIVILEGES。根据文档:

The server caches information in memory as a result of GRANT and CREATE USER statements. This memory is not released by the corresponding REVOKE and DROP USER statements, so for a server that executes many instances of the statements that cause caching, there will be an increase in memory use. This cached memory can be freed with FLUSH PRIVILEGES.

值得一试。我注意到如果不运行此命令,对用户表的更新不会直接生效。

关于mysql - 在 mysql 中,删除/添加用户时出现奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7695672/

相关文章:

mysql - 在 SQL 中使用列名作为行的值

java - Apache Tomcat JSP 应用程序的一般强化技巧

.net - VB.NET 模块 - 我可以在访问 pub 时强制使用 <Module_Name>.Public_Member_Name 吗?成员(member)?

java - 如何: Docker container running java jar connecting to host mysql server?

php - 对于相同的测试套件,MySQL 5.7 比 5.5 慢 3 倍

c# - 保护 Azure 功能的方法有哪些

python - 显示所有 Django 规则权限的谓词?

android - FFmpeg可执行文件Chmod权限

php - 允许用户通过 Web GUI 构建自定义报告

java - 客户端策略文件的 RMI 使用