【整理】MySQL 之 autocommit

mysql 默认是开启 auto commit 的。可以通过如下命令查看 session 级别和 global 级别的设置: 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
mysql> select @@session.autocommit;
+----------------------+
| @@session.autocommit |
+----------------------+
|                    1 |
+----------------------+
1 row in set (0.00 sec)
 
mysql> select @@global.autocommit;      
+---------------------+
| @@global.autocommit |
+---------------------+
|                   1 |
+---------------------+
1 row in set (0.00 sec)
 
mysql>
       那么如果我们不想让 mysql 执行自动提交时,应该如何禁用 autocommit 呢?可以通过 Cmd-Line、Option file、System Var 上都可用的 init_connect 来设置。 
A string to be executed by the server for each client that connects. The string consists of one or more SQL statements. To specify multiple statements, separate them by semicolon characters.
       上面这段话的意思是,每个 client 连接上来时都会由 server 执行一次由 init_connect 指定的 sql 字串。(是否可以认为是基于 session 的?) 

利用这个变量,可以通过如下方式禁用 autocommit: 
方法一: 
1
mysql>SET GLOBAL init_connect='SET autocommit=0';
方法二: 
在 MySQL 的配置文件中设置 
1
2
[mysqld]
init_connect='SET autocommit=0'
方法三: 
启动 mysql 时带上命令行参数 –init_connect='SET autocommit=0' 

值得说明的一点是,这个参数的设置对拥有 super 权限的用户是无效的,具体原因说明如下: 
Note that the content of init_connect is not executed for users that have the SUPER privilege. This is done so that an erroneous value for init_connect does not prevent all clients from connecting. For example, the value might contain a statement that has a syntax error, thus causing client connections to fail. Not executing init_connect for users that have the SUPER privilege enables them to open a connection and fix the init_connect value.
       默认开启的 autocommit 肯定会对 mysql 的性能有一定影响,但既然默认开启必定是有原因的,所以如果你不知道自己到底会遇到什么问题的情况下还是不要改这个设置为妙。举个例子来说明开启 autocommit 会产生的性能影响,如果你插入了 1000 条数据,mysql 会 commit 1000 次,如果我们把 autocommit 关闭掉,通过程序来控制,只要一次commit 就可以了。 

========= 我是分割线  ========= 

      另外一篇博客《Innodb表类型中autocommit的设置》中展示了设置 autocommit 为 0 的效果。

a. 初始状态+设置 session 级别的 autocommit 为 0 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
mysql>
mysql> show binlog events;
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
| Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                  |
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
| mysql-bin.000001 |   4 | Format_desc |         1 |         120 | Server ver: 5.6.10-log, Binlog ver: 4 |
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
1 row in set (0.00 sec)
 
mysql>
mysql> show tables;
Empty set (0.00 sec)
 
mysql>
mysql> select @@global.autocommit;
+---------------------+
| @@global.autocommit |
+---------------------+
|                   1 |
+---------------------+
1 row in set (0.00 sec)
 
mysql> select @@session.autocommit;     
+----------------------+
| @@session.autocommit |
+----------------------+
|                    1 |
+----------------------+
1 row in set (0.00 sec)
 
mysql>
mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
 
mysql>
mysql> select @@global.autocommit;
+---------------------+
| @@global.autocommit |
+---------------------+
|                   1 |
+---------------------+
1 row in set (0.00 sec)
 
mysql> select @@session.autocommit;         
+----------------------+
| @@session.autocommit |
+----------------------+
|                    0 |
+----------------------+
1 row in set (0.00 sec)
 
mysql>
b. 创建一个测试表 
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
mysql> create table t_autocommit(
    -> id int not null auto_increment,
    -> amount int not null default '0',
    -> primary key(id)
    -> )engine=innodb;
Query OK, 0 rows affected (0.01 sec)
 
mysql>
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t_autocommit   |
+----------------+
1 row in set (0.00 sec)
 
mysql>
mysql> describe t_autocommit;
+--------+---------+------+-----+---------+----------------+
| Field  | Type    | Null | Key | Default | Extra          |
+--------+---------+------+-----+---------+----------------+
| id     | int(11) | NO   | PRI | NULL    | auto_increment |
| amount | int(11) | NO   |     | 0       |                |
+--------+---------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
 
mysql>
mysql> select * from t_autocommit;
Empty set (0.00 sec)
 
mysql>
c. 插入数据 
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
34
35
36
37
38
39
40
41
42
mysql>
mysql> insert into t_autocommit set amount=1;
Query OK, 1 row affected (0.00 sec)
 
mysql>
mysql> select * from t_autocommit;          
+----+--------+
| id | amount |
+----+--------+
|  1 |      1 |
+----+--------+
1 row in set (0.00 sec)
 
mysql>
mysql> update t_autocommit set amount=amount+10;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
 
mysql>
mysql> select * from t_autocommit;             
+----+--------+
| id | amount |
+----+--------+
|  1 |     11 |
+----+--------+
1 row in set (0.00 sec)
 
mysql>
mysql> show binlog events;
+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------+
| Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                                                                                                                   |
+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------+
| mysql-bin.000001 |   4 | Format_desc |         1 |         120 | Server ver: 5.6.10-log, Binlog ver: 4                                                                                                  |
| mysql-bin.000001 | 120 | Query       |         1 |         316 | use `test`; create table t_autocommit(
id int not null auto_increment,
amount int not null default '0',
primary key(id)
)engine=innodb |
+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
 
mysql>
       发现 binlog 中仅记录了 create table 动作,insert 和 update 由于 autocommit 为 0 的缘故没有被记录到 binlog 中。

d.断开 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
32
33
34
35
36
37
38
mysql>
mysql> quit
Bye
[root@Betty ~]#
[root@Betty ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.10-log Source distribution
 
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql>
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql>
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t_autocommit   |
+----------------+
1 row in set (0.00 sec)
 
mysql>
mysql> select * from t_autocommit;
Empty set (0.00 sec)
 
mysql>
       发现什么数据都没有。为什么呢?因为 SQL 语句并没有被自己(当前 session)提交给 server 端去处理,只是在当前连接中做了相应处理。 


重复上面的实验,但是保持 autocommit 的默认值(1)。 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
mysql>
mysql> show binlog events;
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
| Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                  |
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
| mysql-bin.000001 |   4 | Format_desc |         1 |         120 | Server ver: 5.6.10-log, Binlog ver: 4 |
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
1 row in set (0.00 sec)
 
mysql>
mysql> show tables;
Empty set (0.01 sec)
 
mysql>
mysql> select @@global.autocommit;                     
+---------------------+
| @@global.autocommit |
+---------------------+
|                   1 |
+---------------------+
1 row in set (0.00 sec)
 
mysql>
mysql> select @@session.autocommit;                                   
+----------------------+
| @@session.autocommit |
+----------------------+
|                    1 |
+----------------------+
1 row in set (0.00 sec)
 
mysql>
mysql> create table t_autocommit(
    -> id int not null auto_increment,
    -> amount int not null default '0',
    -> primary key(id)
    -> )engine=innodb;
Query OK, 0 rows affected (0.01 sec)
 
mysql>
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t_autocommit   |
+----------------+
1 row in set (0.00 sec)
 
mysql>
mysql> describe t_autocommit;
+--------+---------+------+-----+---------+----------------+
| Field  | Type    | Null | Key | Default | Extra          |
+--------+---------+------+-----+---------+----------------+
| id     | int(11) | NO   | PRI | NULL    | auto_increment |
| amount | int(11) | NO   |     | 0       |                |
+--------+---------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
 
mysql>
mysql> insert into t_autocommit set amount=1;
Query OK, 1 row affected (0.00 sec)
 
mysql>
mysql> select * from t_autocommit;
+----+--------+
| id | amount |
+----+--------+
|  1 |      1 |
+----+--------+
1 row in set (0.00 sec)
 
mysql>
mysql> update t_autocommit set amount=amount+10;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
 
mysql>
mysql> select * from t_autocommit;             
+----+--------+
| id | amount |
+----+--------+
|  1 |     11 |
+----+--------+
1 row in set (0.00 sec)
 
mysql>
mysql> show binlog events;
+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------+
| Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                                                                                                                   |
+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------+
| mysql-bin.000001 |   4 | Format_desc |         1 |         120 | Server ver: 5.6.10-log, Binlog ver: 4                                                                                                  |
| mysql-bin.000001 | 120 | Query       |         1 |         316 | use `test`; create table t_autocommit(
id int not null auto_increment,
amount int not null default '0',
primary key(id)
)engine=innodb |
| mysql-bin.000001 | 316 | Query       |         1 |         395 | BEGIN                                                                                                                                  |
| mysql-bin.000001 | 395 | Intvar      |         1 |         427 | INSERT_ID=1                                                                                                                            |
| mysql-bin.000001 | 427 | Query       |         1 |         538 | use `test`; insert into t_autocommit set amount=1                                                                                      |
| mysql-bin.000001 | 538 | Xid         |         1 |         569 | COMMIT /* xid=62 */                                                                                                                    |
| mysql-bin.000001 | 569 | Query       |         1 |         648 | BEGIN                                                                                                                                  |
| mysql-bin.000001 | 648 | Query       |         1 |         762 | use `test`; update t_autocommit set amount=amount+10                                                                                   |
| mysql-bin.000001 | 762 | Xid         |         1 |         793 | COMMIT /* xid=64 */                                                                                                                    |
+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------+
9 rows in set (0.00 sec)
 
mysql>
mysql> quit
Bye
[root@Betty ~]#
[root@Betty ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.10-log Source distribution
 
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql>
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql>
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t_autocommit   |
+----------------+
1 row in set (0.00 sec)
 
mysql>
mysql> select * from t_autocommit;
+----+--------+
| id | amount |
+----+--------+
|  1 |     11 |
+----+--------+
1 row in set (0.00 sec)
 
mysql>
mysql>
这回该有的都有了。 

========= 我是分割线  ========= 

网友说法: 
不要设定 autocommit 这个开关,让它保持 autocommit=1 这个默认状态。 平常有查询\更新都是需要得到最新的数据,根本不需要启动一个事务,除非有特定状况才需要开启事务,再手工用 start transaction ... commit /rollback 。
这种全局设置,在生产环境中没有多大意义。一般都是在应用程序框架(如连接池的库)中设置 autocommit 是否为 ON/OFF, 说白了,就是得到数据库连接以后,显示的调用一次 set autocommit on/off (or =1/0)

上一篇:echarts数据更新禁止图表重新绘制


下一篇:嵌入式基础教程之嵌入式系统的知识体系