有一个安全例程,从一组用户名、密码和权限
- 放弃一个用户
- 创建(相同的)用户
- 授予对表、过程等的访问权限
// $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/