use mysql;
select * from user;
// 一句话查询
select * from mysql.user;
create user '用户名'@'地址' identified by '密码';
create user 'username'@'host' identified by 'password';
说明:
示例:
//创建新用户,名为test,密码为123456,只允许本机登陆
create user 'test'@'localhost' identified by '123456';
//创建新用户,名为test,密码为123456,只允许192.168.1.1机器登陆
create user 'test'@'192.168.1.1' identified by '123456';
//创建新用户,名为test,密码为123456,可以从其他电脑远程登陆mysql所在服务器
create user 'test'@'%' identified by '123456';
//创建新用户,名为test,没有密码,可以从其他电脑远程登陆mysql所在服务器
create user 'test'@'%' ;
grant privileges on databasename.tablename to 'username'@'host';
grant all on *.* to 'test'@'localhost';
说明:
示例:
//表示给用户test授权,让test能给test库中的user表实行select和insert
grant SELECT,INSERT on test.user to 'test'@'localhost';
//表示给用户test授权,让test能给所有库所有表实行所有的权力。
grant all on *.* to 'test'@'localhost';
注意:
用以上命令授权的用户不能给其它用户授权,如果想让该用户可以授权,用以下命令:
grant all on databasename.tablename to 'test'@'localhost' with grant option;
grant all on *.* to 'test'@'localhost' with grant option;
revoke privileges on databasename.tablename from 'username'@'host';
revoke all on *.* from 'test'@'localhost';
注意:
//(1)若授予权利是这样写:
grant SELECT on *.* to ‘test’@‘%’;
// 以下命令是不能撤销用户test 对 test.user 中的SELECT 权利的
revoke SELECT on test.user from ‘test’@‘%’;
//(2)反过来 授予权力
grant SELECT on test.user to ‘test’@‘%’;
// 以下命令是不能用来撤销用户test 对test库的user表的SELECT 权利的
revoke SELECT on *.* from ‘test’@‘%’;
drop user 'username'@'host';
// 删除test用户
drop user 'test'@'localhost';
flush privileges;
set password for 'username'@'host' = password('newpassword');
// 如果是设置当前用户的密码:
set password = password('newpassword');
//示例
set password = password('123456');