来源:自学PHP网 时间:2015-04-16 10:51 作者: 阅读:次
[导读] 【整理】MySQL之autocommitmysql 默认是开启 auto commit 的。可以通过如下命令查看 session 级别和 global 级别的设置:01mysql select @@session autocommit;02+----------------------+03| @@ses...
【整理】MySQL之autocommit
mysql 默认是开启 auto commit 的。可以通过如下命令查看 session 级别和 global 级别的设置:
01 mysql> select @@session.autocommit; 02 +----------------------+ 03 | @@session.autocommit | 04 +----------------------+ 05 | 1 | 06 +----------------------+ 07 1 row in set (0.00 sec) 08 09 mysql> select @@global.autocommit; 10 +---------------------+ 11 | @@global.autocommit | 12 +---------------------+ 13 | 1 | 14 +---------------------+ 15 1 row in set (0.00 sec) 16 17 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
[mysqld]
2
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 就可以了。
========= 我是分割线 =========
a. 初始状态+设置 session 级别的 autocommit 为 0
01 mysql> 02 mysql> show binlog events; 03 +------------------+-----+-------------+-----------+-------------+---------------------------------------+ 04 | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | 05 +------------------+-----+-------------+-----------+-------------+---------------------------------------+ 06 | mysql-bin.000001 | 4 | Format_desc | 1 | 120 | Server ver: 5.6.10-log, Binlog ver: 4 | 07 +------------------+-----+-------------+-----------+-------------+---------------------------------------+ 08 1 row in set (0.00 sec) 09 10 mysql> 11 mysql> show tables; 12 Empty set (0.00 sec) 13 14 mysql> 15 mysql> select @@global.autocommit; 16 +---------------------+ 17 | @@global.autocommit | 18 +---------------------+ 19 | 1 | 20 +---------------------+ 21 1 row in set (0.00 sec) 22 23 mysql> select @@session.autocommit; 24 +----------------------+ 25 | @@session.autocommit | 26 +----------------------+ 27 | 1 | 28 +----------------------+ 29 1 row in set (0.00 sec) 30 31 mysql> 32 mysql> set autocommit=0; 33 Query OK, 0 rows affected (0.00 sec) 34 35 mysql> 36 mysql> select @@global.autocommit; 37 +---------------------+ 38 | @@global.autocommit | 39 +---------------------+ 40 | 1 | 41 +---------------------+ 42 1 row in set (0.00 sec) 43 44 mysql> select @@session.autocommit; 45 +----------------------+ 46 | @@session.autocommit | 47 +----------------------+ 48 | 0 | 49 +----------------------+ 50 1 row in set (0.00 sec) 51 52 mysql> b. 创建一个测试表 01 mysql> create table t_autocommit( 02 -> id int not null auto_increment, 03 -> amount int not null default '0', 04 -> primary key(id) 05 -> )engine=innodb; 06 Query OK, 0 rows affected (0.01 sec) 07 08 mysql> 09 mysql> show tables; 10 +----------------+ 11 | Tables_in_test | 12 +----------------+ 13 | t_autocommit | 14 +----------------+ 15 1 row in set (0.00 sec) 16 17 mysql> 18 mysql> describe t_autocommit; 19 +--------+---------+------+-----+---------+----------------+ 20 | Field | Type | Null | Key | Default | Extra | 21 +--------+---------+------+-----+---------+----------------+ 22 | id | int(11) | NO | PRI | NULL | auto_increment | 23 | amount | int(11) | NO | | 0 | | 24 +--------+---------+------+-----+---------+----------------+ 25 2 rows in set (0.00 sec) 26 27 mysql> 28 mysql> select * from t_autocommit; 29 Empty set (0.00 sec) 30 31 mysql> c. 插入数据 01 mysql> 02 mysql> insert into t_autocommit set amount=1; 03 Query OK, 1 row affected (0.00 sec) 04 05 mysql> 06 mysql> select * from t_autocommit; 07 +----+--------+ 08 | id | amount | 09 +----+--------+ 10 | 1 | 1 | 11 +----+--------+ 12 1 row in set (0.00 sec) 13 14 mysql> 15 mysql> update t_autocommit set amount=amount+10; 16 Query OK, 1 row affected (0.00 sec) 17 Rows matched: 1 Changed: 1 Warnings: 0 18 19 mysql> 20 mysql> select * from t_autocommit; 21 +----+--------+ 22 | id | amount | 23 +----+--------+ 24 | 1 | 11 | 25 +----+--------+ 26 1 row in set (0.00 sec) 27 28 mysql> 29 mysql> show binlog events; 30 +------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------+ 31 | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | 32 +------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------+ 33 | mysql-bin.000001 | 4 | Format_desc | 1 | 120 | Server ver: 5.6.10-log, Binlog ver: 4 | 34 | mysql-bin.000001 | 120 | Query | 1 | 316 | use `test`; create table t_autocommit( 35 id int not null auto_increment, 36 amount int not null default '0', 37 primary key(id) 38 )engine=innodb | 39 +------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------+ 40 2 rows in set (0.00 sec) 41 42 mysql> 发现 binlog 中仅记录了 create table 动作,insert 和 update 由于 autocommit 为 0 的缘故没有被记录到 binlog 中。 d.断开 mysql ,再重新连接。 01 mysql> 02 mysql> quit 03 Bye 04 [root@Betty ~]# 05 [root@Betty ~]# mysql -u root -p 06 Enter password: 07 Welcome to the MySQL monitor. Commands end with ; or \g. 08 Your MySQL connection id is 3 09 Server version: 5.6.10-log Source distribution 10 11 Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. 12 13 Oracle is a registered trademark of Oracle Corporation and/or its 14 affiliates. Other names may be trademarks of their respective 15 owners. 16 17 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 18 19 mysql> 20 mysql> use test; 21 Reading table information for completion of table and column names 22 You can turn off this feature to get a quicker startup with -A 23 24 Database changed 25 mysql> 26 mysql> show tables; 27 +----------------+ 28 | Tables_in_test | 29 +----------------+ 30 | t_autocommit | 31 +----------------+ 32 1 row in set (0.00 sec) 33 34 mysql> 35 mysql> select * from t_autocommit; 36 Empty set (0.00 sec) 37 38 mysql> 发现什么数据都没有。为什么呢?因为 SQL 语句并没有被自己(当前 session)提交给 server 端去处理,只是在当前连接中做了相应处理。 重复上面的实验,但是保持 autocommit 的默认值(1)。 001 mysql> 002 mysql> show binlog events; 003 +------------------+-----+-------------+-----------+-------------+---------------------------------------+ 004 | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | 005 +------------------+-----+-------------+-----------+-------------+---------------------------------------+ 006 | mysql-bin.000001 | 4 | Format_desc | 1 | 120 | Server ver: 5.6.10-log, Binlog ver: 4 | 007 +------------------+-----+-------------+-----------+-------------+---------------------------------------+ 008 1 row in set (0.00 sec) 009 010 mysql> 011 mysql> show tables; 012 Empty set (0.01 sec) 013 014 mysql> 015 mysql> select @@global.autocommit; 016 +---------------------+ 017 | @@global.autocommit | 018 +---------------------+ 019 | 1 | 020 +---------------------+ 021 1 row in set (0.00 sec) 022 023 mysql> 024 mysql> select @@session.autocommit; 025 +----------------------+ 026 | @@session.autocommit | 027 +----------------------+ 028 | 1 | 029 +----------------------+ 030 1 row in set (0.00 sec) 031 032 mysql> 033 mysql> create table t_autocommit( 034 -> id int not null auto_increment, 035 -> amount int not null default '0', 036 -> primary key(id) 037 -> )engine=innodb; 038 Query OK, 0 rows affected (0.01 sec) 039 040 mysql> 041 mysql> show tables; 042 +----------------+ 043 | Tables_in_test | 044 +----------------+ 045 | t_autocommit | 046 +----------------+ 047 1 row in set (0.00 sec) 048 049 mysql> 050 mysql> describe t_autocommit; 051 +--------+---------+------+-----+---------+----------------+ 052 | Field | Type | Null | Key | Default | Extra | 053 +--------+---------+------+-----+---------+----------------+ 054 | id | int(11) | NO | PRI | NULL | auto_increment | 055 | amount | int(11) | NO | | 0 | | 056 +--------+---------+------+-----+---------+----------------+ 057 2 rows in set (0.00 sec) 058 059 mysql> 060 mysql> insert into t_autocommit set amount=1; 061 Query OK, 1 row affected (0.00 sec) 062 063 mysql> 064 mysql> select * from t_autocommit; 065 +----+--------+ 066 | id | amount | 067 +----+--------+ 068 | 1 | 1 | 069 +----+--------+ 070 1 row in set (0.00 sec) 071 072 mysql> 073 mysql> update t_autocommit set amount=amount+10; 074 Query OK, 1 row affected (0.00 sec) 075 Rows matched: 1 Changed: 1 Warnings: 0 076 077 mysql> 078 mysql> select * from t_autocommit; 079 +----+--------+ 080 | id | amount | 081 +----+--------+ 082 | 1 | 11 | 083 +----+--------+ 084 1 row in set (0.00 sec) 085 086 mysql> 087 mysql> show binlog events; 088 +------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------+ 089 | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | 090 +------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------+ 091 | mysql-bin.000001 | 4 | Format_desc | 1 | 120 | Server ver: 5.6.10-log, Binlog ver: 4 | 092 | mysql-bin.000001 | 120 | Query | 1 | 316 | use `test`; create table t_autocommit( 093 id int not null auto_increment, 094 amount int not null default '0', 095 primary key(id) 096 )engine=innodb | 097 | mysql-bin.000001 | 316 | Query | 1 | 395 | BEGIN | 098 | mysql-bin.000001 | 395 | Intvar | 1 | 427 | INSERT_ID=1 | 099 | mysql-bin.000001 | 427 | Query | 1 | 538 | use `test`; insert into t_autocommit set amount=1 | 100 | mysql-bin.000001 | 538 | Xid | 1 | 569 | COMMIT /* xid=62 */ | 101 | mysql-bin.000001 | 569 | Query | 1 | 648 | BEGIN | 102 | mysql-bin.000001 | 648 | Query | 1 | 762 | use `test`; update t_autocommit set amount=amount+10 | 103 | mysql-bin.000001 | 762 | Xid | 1 | 793 | COMMIT /* xid=64 */ | 104 +------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------+ 105 9 rows in set (0.00 sec) 106 107 mysql> 108 mysql> quit 109 Bye 110 [root@Betty ~]# 111 [root@Betty ~]# mysql -u root -p 112 Enter password: 113 Welcome to the MySQL monitor. Commands end with ; or \g. 114 Your MySQL connection id is 4 115 Server version: 5.6.10-log Source distribution 116 117 Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. 118 119 Oracle is a registered trademark of Oracle Corporation and/or its 120 affiliates. Other names may be trademarks of their respective 121 owners. 122 123 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 124 125 mysql> 126 mysql> use test; 127 Reading table information for completion of table and column names 128 You can turn off this feature to get a quicker startup with -A 129 130 Database changed 131 mysql> 132 mysql> show tables; 133 +----------------+ 134 | Tables_in_test | 135 +----------------+ 136 | t_autocommit | 137 +----------------+ 138 1 row in set (0.00 sec) 139 140 mysql> 141 mysql> select * from t_autocommit; 142 +----+--------+ 143 | id | amount | 144 +----+--------+ 145 | 1 | 11 | 146 +----+--------+ 147 1 row in set (0.00 sec) 148 149 mysql> 150 mysql>
这回该有的都有了。
========= 我是分割线 =========
网友说法:
不要设定 autocommit 这个开关,让它保持 autocommit=1 这个默认状态。 平常有查询\更新都是需要得到最新的数据,根本不需要启动一个事务,除非有特定状况才需要开启事务,再手工用 start transaction ... commit /rollback 。
这种全局设置,在生产环境中没有多大意义。一般都是在应用程序框架(如连接池的库)中设置 autocommit 是否为 ON/OFF, 说白了,就是得到数据库连接以后,显示的调用一次 set autocommit on/off (or =1/0)
|
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com