mysql创建用户(如果不存在) [英] mysql create user if not exists
本文介绍了mysql创建用户(如果不存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个查询来检查mysql用户列表以创建新用户.
I have a query to check mysql users list for create new user.
IF (SELECT EXISTS(SELECT 1 FROM `mysql`.`user` WHERE `user` = '{{ title }}')) = 0 THEN
CREATE USER '{{ title }}'@'localhost' IDENTIFIED BY '{{ password }}'
END IF;
但是我得到这个错误:
ERROR 1064 (42000) at line 3: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF (SELECT EXISTS(SELECT 1 FROM `mysql`.`user` WHERE `user` = 'cms_localhost')) = 0 ' at line 1
推荐答案
In 5.7.6 and above, you should be able to use CREATE USER
CREATE USER IF NOT EXISTS 'user'@'localhost' IDENTIFIED BY 'password';
请注意,5.7.6方法实际上并未授予任何权限.
Note that the 5.7.6 method doesn't actually grant any permissions.
如果您使用的不是具有此功能的版本(低于5.7.6),则可以执行以下操作:
If you aren't using a version which has this capability (something below 5.7.6), you can do the following:
GRANT ALL ON `database`.* TO 'user'@'localhost' IDENTIFIED BY 'password';
这将创建一个不存在的用户
This will create the user if it doesn't exist
请注意,如果您使用的是MySQL 8,则GRANT ALL
方法将不会创建用户.
Note, if you are on MySQL 8, the GRANT ALL
method will not create a user.
这篇关于mysql创建用户(如果不存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文