代码改变世界

MySQL 通过set global设置变量的注意点

  abce  阅读(159)  评论(0编辑  收藏  举报

今天有人问,为什么他修改动态变量 log_output 的时候,部分可以设置成功,部分设置失败,具体现象如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
root@localhost (none)>show variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | TABLE |
+---------------+-------+
1 row in set (0.01 sec)
 
root@localhost (none)>set global log_output=file;
Query OK, 0 rows affected (0.00 sec)
 
root@localhost (none)>show variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | FILE  |
+---------------+-------+
1 row in set (0.02 sec)
 
root@localhost (none)>set global log_output=none;
Query OK, 0 rows affected (0.00 sec)
 
root@localhost (none)>show variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | NONE  |
+---------------+-------+
1 row in set (0.01 sec)
 
root@localhost (none)>set global log_output=table;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table' at line 1
root@localhost (none)>

 

在配置文件中给MySQL做配置的时候,一般习惯了不加上引号的格式。比如:

1
2
3
4
5
[mysqld]
...
log_output=FILE
slow_query_log
slow_query_log_file=slow-queries.log

 

但是在 MySQL 会话中建议加上单引号。对于设置 MySQL 系统变量,尤其是需要字符串值的变量,应该使用单引号括起来,以确保 MySQL 能够正确解释这个值。比如,按照如下方式就可以正确设置了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
root@localhost (none)>show variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | NONE  |
+---------------+-------+
1 row in set (0.01 sec)
 
root@localhost (none)>SET GLOBAL log_output = 'TABLE';
Query OK, 0 rows affected (0.01 sec)
 
root@localhost (none)>show variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | TABLE |
+---------------+-------+
1 row in set (0.02 sec)
 
root@localhost (none)>SET GLOBAL log_output = 'TABLE,FILE';
Query OK, 0 rows affected (0.00 sec)
 
root@localhost (none)>show variables like 'log_output';
+---------------+------------+
| Variable_name | Value      |
+---------------+------------+
| log_output    | FILE,TABLE |
+---------------+------------+
1 row in set (0.01 sec)
 
root@localhost (none)>

  

 

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2023-08-21 MySQL告警"[Warning] Connection attributes of length 571 were truncated"
2022-08-21 【SQLServer】执行SQL查询报错The semaphore timeout period has expired
2019-08-21 PostgreSQL的表空间
2015-08-21 11G新特性 -- Expression Statistics
2015-08-21 11G新特性 -- Multicolumn Statistics (Column groups)
点击右上角即可分享
微信分享提示