Mysql 安全和DCL语句

在讨论安全时,我们考虑整个服务器主机安全(而不仅是 Mysql 服务)需要抵御攻击, 窃听, 扫描, 破解等。
Mysql 对所有连接数据库用户进行了了 ACL 访问控制,减少服务器被内部不规范操作导致故障。
Mysql 还支持客户端和服务器之间的 SSL 加密连接。
当然这里讨论的许多概念都不是特定于 Mysql 几乎所有的应用程序都适用相同的一般思路。

toc

  • Mysql 运行时,请遵循以下准则:
    • 不要给用户配置超级用户权限
    • 不要在数据库中存储明文密码
    • 不要使用较为简单的字符密码
    • 不允许非授信任主机使用扫描

用户账户管理

登录 Mysql , 使用 mysql -u root -p 可以连接数据库, 但这只是本地连接数据库的方式,在生产很多情况下都是连接网络中某一个主机上的数据库

-P //指定连接远程数据库端口[默认3306]
-h //指定连接远程数据库地址[默认localhost]
-u //指定连接远程数据库账户[默认root]
-p //指定连接远程数据库密码[默认密码为空]
-e //执行mysql数据库sql指令
-S //指定mysql数据库Socket

## 不安全
[root@Mysql ~]# mysql -uroot -p'mypass'
## 推荐方式
[root@Mysql ~]# mysql -uroot -p
Enter password:
## 推送远程登录方式
[root@Mysql ~]# mysql -h192.168.56.11 -P3306 -uroot -p
Enter password:
## 非交互式操作数据库
[root@Mysql ~]# mysql -uroot -p'Sgy123.com' -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

创建用户

## 先创建本地用户 , 后授权
mysql> create user dasha@'localhost' identified by 'DASHA123.com' with 参数;
## 基于已有用户进行授权
mysql> grant all on *.* to dasha@'localhost';
## 使用 GRANT 语句创建本地用户并授权
mysql> grant all on *.* to ersha@'localhost' identified by 'ERSHA123.com';

删除用户

## DROP USER 语句删除
mysql> drop user 'bgx1'@'localhost';
## 注意 Mysql5.6: 先回收权限 , 然后删除
mysql> revoke all privilege user_name;
mysql> drop user user_name;
## DELETE 语句删除
mysql> delete from mysql.user where user='bgx1' and host='localhost';

修改用户密码

## 方法一
mysql> set password for dasha@'localhost'=password('Dasha123.com');
## 方法二
mysql> update mysql.user set authentication_string=password('new_password') where user='bgx1' and host='localhost';
## 刷新权限
mysql> flush privileges;
## 用户自己修改自己密码
set password=password("new_password");

访问权限系统

  • mysql.user 全局授权
    • 用户字段
    • 权限字段
    • 安全字段
    • 资源控制字段
  • mysql.db (数据库级)
    • 用户字段
    • 权限字段
  • mysql.tables_priv(表级)
  • mysql.columns_priv(列级)
  • 权限应用的顺序
    • user->db->tables->columns

语法:grant 权限列表 on 库名.表名 to '用户名'@'客户端主机' [identified by ' 密码' with option 参数];

相关参数:

  • 权限列表
    • all 所有权限(不包括授权限)
    • 单独授权 select,update,insert,delete
  • 库名表名
    • . 所有库下的所有表 Global level
    • test.* 针对 test 库下的所有表 Database level
    • test.student 针对 test 库下的 student 表 Table level
    • SELECT (id),INSERT (name,age) ON test.t1 针对 test 库下面 t1 表的字段 Column level
  • 客户端主机
    • ocalhost 指定本机
    • 192.168.1.1 指定具体主机
    • 192.168.1.0 网段的所有主机
    • 192.168.1.% 网段的所有主机
    • % 指定所有主机
  • with 参数
    • GRANT OPTION 授权选项
    • MAX_QUERIES_PER_HOUR 定义每小时允许执行的查询数
    • MAX_UPDATES_PER_HOUR 定义每小时允许执行的更新数
    • MAX_CONNECTIONS_PER_HOUR 定义每小时可以建立的连接数
    • MAX_USER_CONNECTIONS 定义单个用户同时可以建立的连接数

GRANT 授权示例

## 创建远程用户 admin1 并赋予所有库下的所有表所有权限(不包括授权限)
mysql> grant all on *.* to admin1@'%' identified by 'Sgy123.com';
## 创建远程用户 admin2 并赋予所有库下的所有表所有权限
mysql> grant all on *.* to admin2@'%' identified by 'Sgy123.com' with grant option;
## 创建远程用户 admin2 并赋予 test 库下的所有表所有权限(不包括授权限)
mysql> grant all on test.* to admin3@'%' identified by 'Sgy123.com';
## 创建用户 admin4 ,只允许在 192.168.1.1 主机登陆,并赋予 test 库下的所有表所有权限(不包括授权限)
mysql> grant all on test.* to admin4@'192.168.1.1' identified by 'Sgy123.com';
## 创建远程用户 admin5 并赋予 test 库下的 t1 所有权限(不包括授权限)
mysql> grant all on test.t1 admin5@'%' identified by 'Sgy123.com';
## 创建远程用户 admin5 并赋予 test 库的 t1 表下查询 id 列和插入 name,age 列
mysql> grant select(id),insert(name,age) on test to admin6@'%' identifide by 'Sgy123.com';

访问权限回收

查看用户权限

## 查看自己的权限(两种显示方式)
mysql> show grants;
mysql> show grants\G
## 查看其它用户权限
mysql> show grants for dasha@'localhost';
mysql> show grants for dasha@'localhost'\G

回收权限

语法: REVOKE 权限列表 ON 数据库名 FROM 用户名@'客户端主机';

## 回收 delete 权限
mysql> revoke delete on *.* from dasha@'localhost';
## 回收所有权限
mysql> revoke all privileges on *.* from dasha@'localhost';
## 回收 grant 权限
mysql> revoke grant option on *.* from ersha@'localhost';

猜你喜欢

转载自www.cnblogs.com/songguoyou/p/11883800.html