MySQL的安全机制

MySQL的安全机制

Mysql用户管理

mysql -h 192.168.1.17 -P 3306  -uroot -p123 mysql -e ‘select user,host from user’

[root@master-node ~]# mysql -h192.168.1.17 -P3306 -uroot -p123456 mysql -e 'select user,host from user'
+--------+----------------+
| user   | host           |
+--------+----------------+
| root   | %              |
| root   | 127.0.0.1      |
| root   | 192.168.1.0/24 |
| root   | ::1            |
| root   | localhost      |
| zabbix | localhost      |
+--------+----------------+
[root@master-node ~]# 

-h  主机名
-P 服务器端口
-u 用户名
-p 密码
-e 连接sql语句

\s获取当前服务器状态信息
Quit退出登录

创建用户

方法一 create user语句创建
实例:create user user1@’localhost’ identified by ‘123456’;

方法二 insert语句创建  不常用
实例:insert into mysql.user(user,host,password,ssl_cipher,x509_issuer,x509_subject)
Values (‘user2’,’localhost’,’password(‘123456’),’’,’’);
flush privileges;

方法三 grant语句创建  //同时授权,最常用
GRANT ALL ON db1.* TO 'username'@'localhost' IDENTIFIED BY 'mypass' WITH GRANT OPTION

删除用户

方法一drop user语句删除
drop user user1@’localhost’;

方法二delect语句删除
delect from mysql.user where user=’user2’ and host=’localhsot’;
flush privileges;

修改密码

  • root修改自己密码
方法一
mysqladmin -uroot -p1234567 password '123456'

方法二
Update mysql.user set password=password(‘new password’) where user=’root’ and host=’localhost’;
Flush privileges;

方法三,不用刷新的
set password=password(‘new password’);
  • Root修改其他用户密码
方法一
Set password for 'user3'@'localhost'=password('new_password');

方法二
Update mysql.user set password=password('new passowrd') where user='’'user3' and host='localhost';
Flush privileges;

方法三
Grant select on '*' to 'user3'@'localhost' identified by 'mypass';
Flush privileges;
  • 普通用户修改密码
set password=password('new passowrd');


丢失root用户密码
Vim /etc/my.cnf
[mysqld]
Skip-grant-tables   //增加这一句表示跳过授权表也就是跳过密码验证
...


保存退出
Service mysqld restart  //重启mysq服务器

登陆mysql
Mysql -uroot
Update mysql.user set password=password(‘new password’) where user=’root’ and host=’localhost’;
Flush privileges;

Mysql权限管理

权限应用的顺序:

user(Y/N)==>DB==>tables_priv==>colums_priv

语法格式

grant 权限列表 on 库名.表名 to  用户名@'客户端主机'  [identified by '密码' with grant option]
1)权限列表     all   所有权限(不包括授权权限)

                select update

2)库名.表名    *.*  所有数据库下的所有表

                数据库.*  指定数据库下的所有表  (最常用)

                数据库名称.表名称 指定数据库的指定表
                
                select(clo1),insert(col1,col2) on mydb.mytable 只能对这个库下的这张表第一列有读权限,第一第二列有插入权限

3)客户端主机

对象列表(为谁赋值了上面的增删改查的这些权限)

          %   所有主机

          192.168.2.%   192.168.2.0网段的所有主机

          192.168.2.8(localhost)   指定主机
          
4)With_option参数

    grant option          授权选项
    
    Max_queries_per_hour;   定义每小时允许查询的查询数
    
    Max_updates_per_hour;   定义每小时允许更新的查询数
    
    Max_connections_per_hour; 定义每小时可以建立的连接数
    
    Max_user_connections;    定义单个用户同时可以建立的连接数

示例:

# 给tom1用户所有库所有表的权限,没有授权权限,允许任何ip通过此用户连接数据库
grant all on *.* to tom1@'%' identified by '123123';

# 给tom2用户所有库所有表的权限,有授权权限,相当于root用户,允许任何ip通过此用户连接数据库
grant all on *.* to tom2@'%' identified by '123123' with grant option;

# 给tom3用户db1库下所有表的权限,允许任何ip通过此用户连接数据库
grant all on db1.* to tom3@'%' identified by '123123'; 

# 给tom4用户db1库下所有表的权限,允许任何192.168.1.0网段的主机通过此用户连接数据库
grant all on db1.* to tom4@'192.168.1.%' identified by '123123';

....

删除收回权限

# 查看权限
show grants\G;
show grants for zabbix@'localhost'\G;

# 回收权限
revoke 权限列表 on 对象列表 from 用户列表

revoke all on pro.* from lisi@localhost;        //回收所有权限

grant select,delete on *.* to tom4@'%';       //回收部分权限

# 权限回收后在删除用户
drop user tom4@'192.168.1.%';

注意:
5.6版本以前在日常管理中在删除用户之前应该是先删除该用户所有权限,再删除该用户,不然以后如果再创建相同的用户的话权限就还是存在的,这是不可以的,5.7后删除用户是权限也一并自动删除了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章