mysql仅在用户不存在时创建用户 [英] mysql create user only when the user doesn't exist
问题描述
我想执行CREATE USER
语句,但仅当用户不存在时才执行.
最好的方法是什么?
I want to execute a CREATE USER
statement, but only when the user doesn't already exist.
What would be the best way of doing this?
推荐答案
如果要创建用户,则还需要创建一个授权.如果授权不存在,则该授权会隐式创建一个用户(这就是为什么鼓励您在创建授权时包括密码,以防万一它们不存在的原因).参见 http://dev.mysql.com/doc/refman/5.1/zh/grant.html
If you're creating a user, you need to create a grant too. The grant implicitly creates a user if it doesn't exist (which is why you are encouraged to include the password when creating a grant, just in case they don't exist). see http://dev.mysql.com/doc/refman/5.1/en/grant.html
因此,一种选择是仅创建授予(使用密码),然后隐式创建用户.
So an option is to just create the grant (with the password) and the user is implicitly created.
例如:
GRANT ALL PRIVILEGES ON db_name.*
TO 'user'@'%' IDENTIFIED BY 'password'
WITH GRANT OPTION;
这篇关于mysql仅在用户不存在时创建用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!