6.1.5 系统变量
从 MySQL 4.0.3 开始,我们提供了对大量的系统变量和连接变量的更好的访问方式。你可以不需要关闭服务器就可以更改其中的大部变量值。
系统变量可分为两种类型:线程特定(Thread-specific)或称为连接特定(connection-specific)变量,它们是当前连接唯一的;全局变量,它们用于设置全局事件。全局变量也同样被用于设置一个新连接的相应线程特定变量的初始值。
当 mysqld 启动时,所有的全局变量以命令行参数和选项文件内容初始化。可以通过 SET GLOBAL 命令更改这些值。当一个新的连接线程被建立时,将以全局变量值初始化线程特定变量,直到你执行一个新的 SET GLOBAL 命令时,线程特定变量才会改变。
为了设置一个 全局(GLOBAL) 变量值,可以使用下面的任一句法:(在这里,我们以 sort_buffer_size 变量作为一个示例)
SET GLOBAL sort_buffer_size=value;SET @@global.sort_buffer_size=value;
为了设置一个 会话(SESSION) 变量的值,可以使用下面任一句法:
SET SESSION sort_buffer_size=value;SET @@session.sort_buffer_size=value;SET sort_buffer_size=value;
如果你没有明确指定 GLOBAL 或 SESSION,那么默认地将是设置 SESSION。
LOCAL 是 SESSION 的同义词。
通过下面的任一命令可以检索到一个 全局(GLOBAL) 变量值:
Select @@global.sort_buffer_size;SHOW GLOBAL VARIABLES like 'sort_buffer_size';
通过下面的任一命令可以检索到一个 会话(SESSION) 变量值:
Select @@session.sort_buffer_size;SHOW SESSION VARIABLES like 'sort_buffer_size';
当 检索 一个变量值时使用 @@variable_name 句法,或没有指定 GLOBAL 或 SESSION 时,如果线程特定(thread-specific)的 (SESSION) 值存在,MySQL 将返回它。如果不存在,那么 MySQL 将返回全局变量值。
在设置 全局(GLOBAL) 变量而不是在检索他们的时候需要使用 GLOBAL ,是为了在之后引用一个同名的线程特定(thread-specific)变量或删除同名的一个线程特定(thread-specific)变量时不至发生问题。在这种情况下,你可能无意间改变整个服务器的状态而不是你自己的连接。
下面的列表是你可以使用 GLOBAL 或 SESSION 对它们进行更改和检索的所有变量。
变量名 | 变量值类型 | 变量类型 |
autocommit | bool | SESSION |
big_tables | bool | SESSION |
binlog_cache_size | num | GLOBAL |
bulk_insert_buffer_size | num | GLOBAL | SESSION |
concurrent_insert | bool | GLOBAL |
connect_timeout | num | GLOBAL |
convert_character_set | string | SESSION |
delay_key_write | OFF | ON | ALL | GLOBAL |
delayed_insert_limit | num | GLOBAL |
delayed_insert_timeout | num | GLOBAL |
delayed_queue_size | num | GLOBAL |
error_count | num | LOCAL |
flush | bool | GLOBAL |
flush_time | num | GLOBAL |
foreign_key_checks | bool | SESSION |
identity | num | SESSION |
insert_id | bool | SESSION |
interactive_timeout | num | GLOBAL | SESSION |
join_buffer_size | num | GLOBAL | SESSION |
key_buffer_size | num | GLOBAL |
last_insert_id | bool | SESSION |
local_infile | bool | GLOBAL |
log_warnings | bool | GLOBAL |
long_query_time | num | GLOBAL | SESSION |
low_priority_updates | bool | GLOBAL | SESSION |
max_allowed_packet | num | GLOBAL | SESSION |
max_binlog_cache_size | num | GLOBAL |
max_binlog_size | num | GLOBAL |
max_connect_errors | num | GLOBAL |
max_connections | num | GLOBAL |
max_error_count | num | GLOBAL | SESSION |
max_delayed_threads | num | GLOBAL |
max_heap_table_size | num | GLOBAL | SESSION |
max_join_size | num | GLOBAL | SESSION |
max_sort_length | num | GLOBAL | SESSION |
max_tmp_tables | num | GLOBAL |
max_user_connections | num | GLOBAL |
max_write_lock_count | num | GLOBAL |
myisam_max_extra_sort_file_size | num | GLOBAL | SESSION |
myisam_max_sort_file_size | num | GLOBAL | SESSION |
myisam_sort_buffer_size | num | GLOBAL | SESSION |
net_buffer_length | num | GLOBAL | SESSION |
net_read_timeout | num | GLOBAL | SESSION |
net_retry_count | num | GLOBAL | SESSION |
net_write_timeout | num | GLOBAL | SESSION |
query_cache_limit | num | GLOBAL |
query_cache_size | num | GLOBAL |
query_cache_type | enum | GLOBAL |
read_buffer_size | num | GLOBAL | SESSION |
read_rnd_buffer_size | num | GLOBAL | SESSION |
rpl_recovery_rank | num | GLOBAL |
safe_show_database | bool | GLOBAL |
server_id | num | GLOBAL |
slave_compressed_protocol | bool | GLOBAL |
slave_net_timeout | num | GLOBAL |
slow_launch_time | num | GLOBAL |
sort_buffer_size | num | GLOBAL | SESSION |
sql_auto_is_null | bool | SESSION |
sql_big_selects | bool | SESSION |
sql_big_tables | bool | SESSION |
sql_buffer_result | bool | SESSION |
sql_log_binlog | bool | SESSION |
sql_log_off | bool | SESSION |
sql_log_update | bool | SESSION |
sql_low_priority_updates | bool | GLOBAL | SESSION |
sql_max_join_size | num | GLOBAL | SESSION |
sql_quote_show_create | bool | SESSION |
sql_safe_updates | bool | SESSION |
sql_select_limit | bool | SESSION |
sql_slave_skip_counter | num | GLOBAL |
sql_warnings | bool | SESSION |
table_cache | num | GLOBAL |
table_type | enum | GLOBAL | SESSION |
thread_cache_size | num | GLOBAL |
timestamp | bool | SESSION |
tmp_table_size | enum | GLOBAL | SESSION |
tx_isolation | enum | GLOBAL | SESSION |
version | string | GLOBAL |
wait_timeout | num | GLOBAL | SESSION |
warning_count | num | LOCAL |
unique_checks | bool | SESSION |
以 num 标记的变量可以设置一个数字值。以 bool 标记的变量可以设置 0、1、ON 或 OFF。enum 类型的变量通常是设置为该变量的某一个可用值,但也可以设置为相对应的数字。(enum 的第一个值为 0)。
下面是某些变量的描述:
变量 | 描述 |
identity | last_insert_id 的别名 (Sybase 兼容) |
sql_low_priority_updates | low_priority_updates 的别名 |
sql_max_join_size | max_join_size 的别名 |
delay_key_write_for_all_tables | 如它与 delay_key_write 一起被设置,那么所有新打开的 MyISAM 表将使用 delayed key writes. |
version | VERSION() 的别名 (Sybase (?) 兼容) |
在启动选项章节中可以找到其它的变量的描述,SHOW VARIABLES 的描述在 SET 部分。
6.1.6 注释句法
MySQL 服务器支持 # 到该行结束、-- 到该行结束 以及 /* 行中间或多个行 */ 的注释方格:
mysql> Select 1+1; # 这个注释直到该行结束mysql> Select 1+1; -- 这个注释直到该行结束mysql> Select 1 /* 这是一个在行中间的注释 */ + 1;mysql> Select 1+/*这是一个多行注释的形式*/1;
注意 -- (双长划) 注释风格要求在两个长划后至少有一个空格!
尽管服务器理解刚才描述的注释句法,但 MySQL 客户端的语法分析在 /* ... */ 注释方式上还有所限止:
单引号和双引号被用来标志一个被引用字符串的开始,即使是在一个注释中。如果注释中的引号没有另一个引号与之配对,那和语法分析程序就不会认为注释结束。如果你以交互式运行 mysql,你会产生困惑,因为提示符从 mysql> 变为 '> 或 ">。
一个分号被用于指出当前 SQL 语句的结束并且跟随它的任何东西表示下一行的开始。
不论你是以交互式运行 mysql 还是将命令放在一个文件中,然后以 mysql < some-file 告诉 mysql 读取它的输入,这个限制均存在。
MySQL 支持 ANSI SQL 注释风格,但在两个长划后必须跟有一个空格。