您好,UncleToo欢迎您!  为了更好的浏览本站,请使用高版本浏览器
RSS  Tag     设为首页 | 加入收藏
 您所在的位置:首页 > 数据库技术 > MySQL

MySQL创建用户与授权方法

作者:未知  来源:转载  日期:2016-02-25 21:28:01
收藏  评论:( 0 )  阅读:435

一、创建用户:

命令:

Create USER 'username'@'host' IDENTIFIED BY 'password';

参数说明:

username - 你将创建的用户名,

host - 指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost, 如果想让该用户可以从任意远程主机登陆,可以使用通配符%.

password - 该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器.

例子:

Create USER 'dog'@'localhost' IDENTIFIED BY '123456';
Create USER 'pig'@'192.168.1.101_' IDENDIFIED BY '123456';
Create USER 'pig'@'%' IDENTIFIED BY '123456';
Create USER 'pig'@'%' IDENTIFIED BY '';
Create USER 'pig'@'%';


二、授权:

命令:

GRANT privileges ON databasename.tablename TO 'username'@'host'

说明:

privileges - 用户的操作权限,如Select , Insert , Update 等(详细列表见该文最后面).如果要授予所的权限则使用ALL.;

databasename - 数据库名,

tablename-表名,如果要授予该用户对所有数据库和表的相应操作权限则可用*表示, 如*.*.

例子:

GRANT Select, Insert ON test.user TO 'pig'@'%';
GRANT ALL ON *.* TO 'pig'@'%';

注意:用以上命令授权的用户不能给其它用户授权,如果想让该用户可以授权,用以下命令:
GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION;


三、设置与更改用户密码

命令:

SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');

如果是当前登陆用户用SET PASSWORD = PASSWORD("newpassword");

例子:

SET PASSWORD FOR 'pig'@'%' = PASSWORD("123456");


四、撤销用户权限

命令:

REVOKE privilege ON databasename.tablename FROM 'username'@'host';

说明:

privilege, databasename, tablename - 同授权部分.

例子:

REVOKE Select ON *.* FROM 'pig'@'%';

注意: 假如你在给用户'pig'@'%'授权的时候是这样的(或类的):GRANT Select ON test.user TO 'pig'@'%', 则在使用REVOKE Select ON *.* FROM 'pig'@'%';命令并不能撤销该用户对test数据库中user表的Select 操作.相反,如果授权使用的是GRANT Select ON *.* TO 'pig'@'%';则REVOKE Select ON test.user FROM 'pig'@'%';命令也不能撤销该用户对test数据库中user表的Select 权限.

具体信息可以用命令 SHOW GRANTS FOR 'pig'@'%'; 查看.


五、删除用户

命令:

Drop USER 'username'@'host';


附表:在MySQL中的操作权限

AlterAllows use ofAlter TABLE.
Alter ROUTINEAlters or drops stored routines.
CreateAllows use ofCreate TABLE.
Create ROUTINECreates stored routines.
Create TEMPORARY TABLEAllows use ofCreate TEMPORARY TABLE.
Create USERAllows use ofCreate USER,Drop USER,RENAME USER, andREVOKE ALL PRIVILEGES.
Create VIEWAllows use ofCreate VIEW.
DeleteAllows use ofDelete.
DropAllows use ofDrop TABLE.
EXECUTEAllows the user to run stored routines.
FILEAllows use ofSelect...INTO OUTFILEandLOAD DATA INFILE.
INDEXAllows use ofCreate INDEXandDrop INDEX.
InsertAllows use ofInsert.
LOCK TABLESAllows use ofLOCK TABLESon tables for which the user also hasSelectprivileges.
PROCESSAllows use ofSHOW FULL PROCESSLIST.
RELOADAllows use ofFLUSH.
REPLICATIONAllows the user to ask where slave or master
CLIENTservers are.
REPLICATION SLAVENeeded for replication slaves.
SelectAllows use ofSelect.
SHOW DATABASESAllows use ofSHOW DATABASES.
SHOW VIEWAllows use ofSHOW Create VIEW.
SHUTDOWNAllows use ofmysqladmin shutdown.
SUPERAllows use ofCHANGE MASTER,KILL,PURGE MASTER LOGS, andSET GLOBALSQL statements. Allowsmysqladmin debugcommand. Allows one extra connection to be made if maximum connections are reached.
UpdateAllows use ofUpdate.
USAGE

Allows connection without any specific privileges.




MySQL
 
授权
 
除非特别声明,本站所有PHP教程及其他教程/文章均为原创、翻译或网友投稿,版权均归UncleToo中文网所有, 转载请注明作者及出处。
原文网址:http://www.uncletoo.com/html/mysql/1176.html
读完这篇文章后,你是否有所收获? 分享是一种生活的信念!
  • 0
  • 0
我来说两句
更多>>网友评论