一:创建普通用户
create user username[identified by [password] 'password']
[,username[identified by [password] 'password']
.....
[,username[identified by [password] 'password']
]
例子:执行sql语句create user ,创建名为'cjgong'密码为123456的用户账号
create user 'cjgong'@'localhost' identified by '123456';
二:执行insert 语句执行创建用户
insert into user(host,user,password) values('hostname','username',password('password'));
例子:执行sql语句create user ,创建名为'cjgong'密码为123456的用户账号
insert into user(host,user,password,ssl_cipher,x509_issuer,x509_subject) values('localhost','cjgong',password('123456'),'','','');
flush privileges;
ssl_cipher,x509_issuer,x509_subject是设置安全字段
三:执行grant语句来创建语句
grant语句k可以创建用户时赋予权限
grant priv_type on databasename.tablename
to username[identified by [password] 'password']
[,username[identified by [password] 'password']]
....
[,username [identified by [password] 'password']]]
priv_type表示用户实现设置所创建用户账号的权限:参数databasename.tablename表示所创建的用户账号的权限范围,即只能在制定的数据库和表上使用这些权限:其他部分与create user 语句一样。
例子:执行grant语句,创建名为cjgong2密码为123456的用户账户,同时设置其只具有select权限。
grant select on company.t_dept
to 'cjgong2'@'localhost' identfified by '123456';
四:root修改密码
(1): 在dos窗口通过mysqladmin命令修改root密码
mysqladmin -u username -p password "new_password"
(2):通过set命令修改root密码,当用root连接到数据库后:
set password=passwprd("newpassword");
(3):更新系统表mysql.user来修改root密码
update user set password=password("newpassword")
where user="root" and host="localhost";
五:利用root用户修改普通用户账户密码
(1):grant priv_type on database.table
to user[identified by [password] 'new_password']
(2):通过set命令修改cjgong用户密码
set password for
'username'@'localhost'=password("new_password");
例如:
set password for
'cjgong'@'localhost'=password("123456");
六:删除普通用户和账号
(1):通过drop user 来删除用户
drop user user1[,user2...]...
例如:
drop user 'cjgong'@'localhost';
七:权限管理
(1):赋予普通用户权限
grant priv_type [(colum_list)] on database.table
to user [indentified by[password] 'password']
[,user [identified by [password] 'password']]
....
[with with-option [with_option]...]
参数priv_type表示权限类型,column_list表示权限作用的字段【不设置默认为整个表】,databases.table表示数据库的某个表 ;参数user表示用户,有用户名和主机名构成;关键字 identified by用来实现设置密码;至于with_option,其值只能是五个值中的一个;
省略。。。
例子:
grant select,create,drop on *.*
to 'cjgong'@'localhost' identified by 'cjgong' //identified by后面会设置密码
with grant option
(2):查看用户权限
show grants for user
例如
show grants
for 'xiahelong'@'localhost' \G
(3):回收权限
revoke priv_type [(colum_list)] on database.table
from user1 [indentified by[password] 'password']
[,user2 [identified by [password] 'password']]
revoke all privileges,grant,option//回收所有权限
from user1 [indentified by[password] 'password']
[,user 2[identified by [password] 'password']]
例子:
revoke select on *.*
from 'cjgong'@'localhost';