Mysql
DCL数据控制语言
数据控制语言(DCL:Data Control Language)是用来设置或者更改数据库用户或角色权限的语句,这些语句包括GRANT、DENY、REVOKE等语句
限制root用户指定ip登录
查看root用户可以在哪台机器登录
select user,host from mysql.user where user='root';
+------+-----------+
| user | host |
+------+-----------+
| root | % |
| root | localhost |
+------+-----------+
修改mysql库里边的user表
update mysql.user set host='localhost' where user='root';
刷新权限
flush privileges;
用户密码
修改用户密码分三种方法:
- 第一种
// set password for 用户@ip = password('密码');
set password for root@localhost = password('root');
- 第二种
// mysqladmin -u用户 -p旧密码 password 新密码;
mysqladmin -urootmysqladmin -uroot -proot password;
- 第三种
update mysql.user set authentication_string=password('root') where user='root' and host='localhost';
忘记密码
- 第一步
修改配置文件my.cnf (默认在/etc/my.cnf),在[mysqld]下面加上 skip-grant-tables (跳过权限的意思)
- 第二步
重启mysql服务
- 第三步
mysql -uroot -p 无需密码登录进入
- 第四步
修改密码
创建新用户并限制ip网段登录
创建用户的语法
create user 'username'@'host' identified by 'password';
username:你将创建的用户名
host:指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机 登陆,可以使用通配符%
password:该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器
创建用户语法
创建一个mitday用户,并指定登录密码:123456,可以在任何一台远程主机都可以登录
create user 'mitday'@'%' identified by '123456';
创建一个mitday用户,并指定登录密码:为空,指定在120网段的机器登录
create user 'mitday'@'120.%.%.%' identified by '';
查看权限
select * from mysql.user where user='mitday';
show grants for 'mitday'@'%';
/* USAGE:无权限的意思 */
+------------------------------------+
| Grants for mitday@% |
+------------------------------------+
| GRANT USAGE ON *.* TO 'mitday'@'%' |
+------------------------------------+
show grants for 'root'@'localhost';
/* WITH GRANT OPTION:表示这个用户拥有grant权限,即可以对其他用户授权 */
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
删除用户语法
// drop user 'username'@'host';
drop user 'mitday'@'%';
delete from mysql.user where user = 'mitday';
库表权限授权与回收
授权语法
grant 权限1,权限2..... on 数据库对象 to '用户'@'host' identified by 'password';
对现有用户进行授权:对现有用户mitday授予所有库所有表所有权限
grant all privileges on *.* to 'mitday';
- all privileges:代表所有权限
- . :代表所有库所有表
对没有的用户进行授权:创建一个新用户chairman授予student库的所有权限,登录密码123456,任何一台主机登录
grant all privileges on student.* to 'chairman'@'%' identified by '123456';
对没有的用户进行授权:创建一个新用户chairman授予student库的study表 查与修改权限,登录密码123456,任何一台主机登录
grant select,update on student.study to 'chairman'@'%' identified by '123456'
对没有的用户进行授权:对用户chairman授予student库的study表insert 权限,登录密码123456,任何一台主机登录
grant insert on student.study to 'chairman'@'%' identified by '123456';
回收语法
revoke 权限1,权限2..... on 数据库对象 from '用户'@'host';
回收mitday用户的所有权限(注意:并没有回收它的登录权限)
revoke all privileges on *.* from 'mitday' @ '%';
flush privileges;
回收pig用户的所有权限(并回收它的登录权限)
delete from mysql.user where user='pig';
flush privileges;
回收chairman用户对student库的study的查与修改权限
revoke select,update on student.study from 'chairman'@'%';
flush privileges;