您的位置 首页 数据库

mysql 禁止修改命令不加条件执行命令(数据安全操作规范)

mysql 禁止修改命令不加条件执行命令(数据安全操作规范)

服务端禁止不带where条件操作数据库表有两种方法:

利用sql_safe_updates配置参数,表示在delete,update操作中:

没有where条件,当where条件中列没有索引可用,且无limit限制时会拒绝更新。

# 方法1:临时执行
mysql> set global sql_safe_updates=1;
-- 退出重新登陆生效
mysql> update stu set sname='baimei';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. 
-- 配置效果展示
​
# 方法2:永久生效
[root@db01 ~]# vi /etc/my.cnf
[mysqld]
init-file=/opt/init.sql
-- 新建脚本
echo 'set global sql_safe_updates=1;' >/opt/init.sql
chmod +x /opt/init.sql
/etc/init.d/mysqld restart
[root@db01 ~]# mysql -uroot -pbaimei123 -e "select @@global.sql_safe_updates"
+-------------------------------------+
| @@global.sql_safe_updates |
+-------------------------------------+
| 1                                                |
+-------------------------------------+

客户端禁止不带where条件操作数据库表有两种方法:

# 方法一:把safe_updates=1加入到my.cnf的client标签下
[root@db01 ~]# vim /etc/my.cnf
[mysql]
socket=/tmp/mysql.sock
safe_updates=1
-- 客户端配置信息
mysql> update stu set sname='baimei01';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. 
-- 配置效果展示
​
# 方法2:设置数据库别名操作方式
alias mysql='mysql -U'
-U, --safe-updates  Only allow UPDATE and DELETE that uses keys
-- 表示以安全更新模式登录数据库,并放入/etc/profile永久生效。
mysql> update stu set sname='baimei03';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.
-- 配置效果展示

 

 

欢迎来撩 : 汇总all

白眉大叔

关于白眉大叔linux云计算: 白眉大叔

热门文章