了解一下Mysql的安全模式

预防的问题

当进行delete,update操作时如果工程师稍微粗心大意,没有指定相应的条件,则数据库中的所有数据项都会相应受影响。MySQL的设计者也考虑了这一点,所以就产生了安全模式这种概念。

如何使用

可以通过sql_safe_updates该参数去查看当前数据库的安全模式是否已经开启 ,如果没有的话,可以通过set sql_safe_updates=1打开(0,关闭,1,打开)。

mysql> show variables like "sql_safe_updates";
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| sql_safe_updates | OFF    |
+------------------+-------+

mysql> set sql_safe_updates = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like "sql_safe_updates";
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| sql_safe_updates | ON    |
+------------------+-------+
1 row in set, 1 warning (0.00 sec)

产生的效果

  1. 拒绝update的情况
    没有指定索引项;
    有where条件但非索引项同时也没指定行数;
    where后面啥都没有携带;

  2. 拒绝delete的情况
    where后面没有携带条件;
    有条件但非索引项;
    有条件但非索引项并且没有指定行数;

update情况示例:

-- 没有携带条件
mysql> update order_record set order_name='hello world!!!';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.


mysql> desc order_record;
+-------------+----------+------+-----+---------+----------------+
| Field       | Type     | Null | Key | Default | Extra          |
+-------------+----------+------+-----+---------+----------------+
| id          | int(11)  | NO   | PRI | NULL    | auto_increment |
| order_name  | char(10) | YES  |     | NULL    |                |
| create_time | date     | YES  |     | NULL    |                |
+-------------+----------+------+-----+---------+----------------+

-- 有携带非索引项的条件
mysql> update order_record set order_name = 'hello world ' where create_time='2020-05-05';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.

-- 只携带了limit
mysql> update order_record set order_name = 'helloworld ' limit 1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

-- 携带了非索引项和limit
mysql> update order_record set order_name = 'test' where create_time>='2020-05-05' limit 1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

-- 携带了索引项
mysql> update order_record set order_name = 'test2' where id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

delete情况示例:

-- where后面没有携带条件;
mysql> delete from order_record;
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.

-- 有条件但非索引项;
mysql> delete from order_record where order_name = 'test';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.

-- 有条件但非索引项但有指定行数;
mysql> delete from order_record where order_name = 'test' limit 1;
Query OK, 1 row affected (0.01 sec)

-- 只指定了行数;
mysql> delete from order_record limit 1;
Query OK, 1 row affected (0.01 sec)

-- 携带了索引项;
mysql> delete from order_record where id = 3;
Query OK, 1 row affected (0.01 sec)

总结

当开启了安全模式后,对于没有指定索引项、只指定了非索引项作为条件,没有条件同时也没有指定行数 的delete和update情况会被拒绝执行。


版权声明:本文为ADi_1998原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。