MySQL用户权限管理

用户权限管理主要有以下内容

1、可以限制用户访问那些数据库、数据表;

2、可以限制用户可以对那些表进行select、create、delete、alter、insert、update等操作;

3、限制用户登录的IP或域名;

4、可以限制用户自己的权限可以授权给其他用户。

添加用户

创建用户的实例:

1  create user 'username'@'host' identified by 'password';

说明:username:就是用户名;host:指定在那个主机上可以登录(%所有的远程主机都可以访问,localhost本机访问);password:对应用户的密码

实例:

1  create user 'mc'@'localhost' identified by '123456';

说明:创建一个root用户,可以在本地访问,访问的密码是123456;

查看用户的信息

1 use mysql;
2 select User,Host from user;

说明:查看用户名和访问的主机

修改密码

第一种

1 use mysql;
2 update user set authentication_string=password('123456') where user='root';
3 flush privileges;

说明:这种写法是Mysql5.7之后的写法;

第二种

语法:set password for ‘用户名’@’登录地址’=password(‘密码’)

1 set password for 'root'@'localhost'=password('123456');

第三种

语法:mysqladmin -u用户名 -p旧的密码 password 新密码

1 mysqladmin -uroot -p123456 password 1234abcd;

说明:mysqladmin位于mysql安装目录的bin目录下;

用户重命名

1 rename user 'root'@'localhost' to 'test'@'localhost';

说明:用户名root修改为test;

1 rename user 'root'@'localhost' to 'test'@'%';

说明:将用户root的用户名修改为test,并将本地访问修改为任何主机都可以访问;

删除用户

1 delete from user where User='用户名';
1 drop user 'root'@'localhost';

权限管理

刷新权限

1 flush privileges;

说明:再修给完权限的时候,一定要刷新权限才可以生效(将权限信息从内存写入数据库)

查看用户的权限

1 show grants for 'root'@'localhost';

说明:查看root用户的权限

给用户授权

语法:GRANT 权限名字(多个权限用","号隔开) ON 数据库名.表名 TO '用户名'@'允许登录的ip地址'[IDENTIFIED BY '密码'];

1 grant select,create,drop,update,alter on *.* to 'yangxin'@'localhost' identified by 'yangxin0917' with grant option;

说明:with grant option可以将用户自己的权限分配给其他的用户;

回收用户的权限

语法:revoke 权限名字(多个权限用“,”号隔开) on 数据库名.表名 from 用户名@host identified by '密码'

1 revoke create on *.* from'yangxin'@'localhost' identified by 'yangxin0917';
2  flush privileges;

说明:回收当前用户create所有数据表和数据库的权限;

posted @ 2018-08-23 10:24  努力的九月  阅读(829)  评论(0编辑  收藏  举报