【整理】MySQL 之 autocommit

2019独角兽企业重金招聘Python工程师标准>>> hot3.png


mysql 默认是开启 auto commit 的。可以通过如下命令查看 session 级别和 global 级别的设置:
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:
方法一:
mysql>SET GLOBAL init_connect='SET autocommit=0';
方法二:
在 MySQL 的配置文件中设置
[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
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. 创建一个测试表
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. 插入数据
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: 0mysql> 
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 ,再重新连接。
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 distributionCopyright (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 -ADatabase 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)。
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: 0mysql> 
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 distributionCopyright (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 -ADatabase 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)



转载于:https://my.oschina.net/moooofly/blog/169824

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/265513.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java某个类避免findbug检查_Findbugs能否在java中检测到捕获RuntimeException?

你能不能让我知道Findbugs可以在java中检测到catcing RuntimeException吗?有效的java建议我们不要捕获RuntimeException.所以我想知道Findbugs可以抓错了.另外,我已经检查过Klocwork JD.CATCH和checkstyle IllegalCatch是否适用于此目的.最佳答案 有点.在findbugs中…

交叉编译链的安装

参考:嵌入式 交叉编译链的安装 作者:一只青木呀 发布时间:2020-08-04 18:13:13 网址:https://blog.csdn.net/weixin_45309916/article/details/107789879 目录什么是交叉编译器交叉编译器的下载交叉编译器的安装1.把下载的文件放到…

Scrapy将爬取的段落整合为字符串

使用Scrapy框架爬取文章的时候,经常会遇到要爬取多个段落的问题,如果这个时候使用的是: text response.xpath("......").extract() 那么会发现爬取下来的文章是以段落为单位的list,不方便直接展示。 这个时候可以将lis…

Ubuntu下安装VS Code以及C/C++插件(PS工作目录的创建)

参考:Visual Studio Code Ubuntu下安装 以及C/C插件大全 作者:一只青木呀 发布时间:2020-08-05 11:55:53 网址:https://blog.csdn.net/weixin_45309916/article/details/107811506 目录为何选择安装VS CodeVisual Studio Code 安装…

Common Lisp中调用R

2019独角兽企业重金招聘Python工程师标准>>> R是功能强大的统计软件,和Lisp一样也有一个交互式的命令行环境,还有众多的扩展库,可以用来进行专业的统计分析。要在Common Lisp中方便的调用R的功能,可以试用rcl这个库。安…

java生成pdf加密_java使用iText 生成PDF全攻略(表格,加密)

java使用iText 生成PDF全攻略,包括创建文档,设置字体,添加表格(PdfPTable),创建新页(newPage),设置布局,加密主要使用的jar包: itextpdf-5.4.2.jar,itext-pdfa-5.4.2.jar,itext-xtra-5.4.2.jar,如果用到中文,需要CJK字体的扩展包:itext-asian.jar如果用到…

恩智浦NXP I.MX6ULL芯片介绍下载官网资料

参考:NXP I.MX6ULL芯片介绍以及资料的获取 作者:一只青木呀 发布时间:2020-09-26 10:54:26 网址:https://blog.csdn.net/weixin_45309916/article/details/108808573 目录I.MX6ULL芯片介绍以及官网资料的获取I.MX6ULL芯片介绍以及…

变速不变调方法

语音变速不变调,即语音时长规整,是指不改变原说话人的音调及语义信息,只改变说话人的语速。 语音变速不变调算法有三大类:时域法、频域法、参量法,如表2-1所示。 表2-1 变速不变调算法分类 时域法 频域法 参量法 剪…

java开发高端说法_关于Java代码的设计和开发注意事项,下列哪些说法符合《集合开发规约》:...

案例分析一:假定CPU的主频是500MHz。硬盘采用DMA方式进行数据传送,其数据传输率为4MB/s, 每次DMA传输的数据量为8KB, 要求没有任何数据传输被错过。如果CPU在DMA初始化设置和启动硬盘操作等方面用了1000个时钟周期,并且在DMA传送完成后的中断…

ES6学习笔记六(Iterator和for..of)

{let arr[hello,world];let maparr[Symbol.iterator](); //返回false时继续执行,true停止执行!console.log(map.next());console.log(map.next());console.log(map.next()); }{let obj{start:[1,3,2],end:[7,8,9],[Symbol.iterator](){let selfthis;let…

判定点是否在不规则多边形内部的问题

2019独角兽企业重金招聘Python工程师标准>>> 问题如下: 话说在平面内有一个任意的不规则的封闭多边形,另外在这个平面内还有一个点,问题:如何高效的判定这个点是在这个多边形内部还是外部?补充&#xff1a…

Cortex-A7 MPCore 架构详细介绍(九种运行模式、内核寄存器组R0~R15,有特定的名字和功能)

目录0.ARM架构的历史简介1.Cortex-A7 MPCore(即多核) 简介2.Cortex-A 处理器九种运行模式3.Cortex-A 寄存器组(内核寄存器)3.1通用寄存器3.1.1未备份寄存器(R0~R7)3.1.2备份寄存器(R8~R12、SP指针R13、备份R14也叫LR)3.1.3程序计数器R15(PC)3.2程序状态寄…

Java开发中遇到具有挑战的事_Java并发编程的挑战:遇到的问题及如何解决

并发编程的目的是为了让程序运行得更快,但是,并不是启动更多的线程就能让程序最大限度地并发执行。在进行并发编程时,如果希望通过多线程执行任务让程序运行得更快,会面临非常多的挑战,比如上下文切换的问题、死锁的问…

jQuery 属性选择器

jQuery 使用 XPath 表达式来选择带有给定属性的元素。 $("[href]") 选取所有带有 href 属性的元素。 $("[href#]") 选取所有带有 href 值等于 "#" 的元素。 $("[href!#]") 选取所有带有 href 值不等于 "#" 的元素。 $("…

使用的 SQL Server 版本不支持数据类型“datetime2”的错误解决方法

THE VERSION OF SQL IN USE DOES NOT SUPPORT DATATYPE ‘DATETIME2′ 主要错误原因,在使用ado.net entity的时候,entity使用的数据库是sqlserver 2008,或者sqlserver 2008 r2 但后来实际使用的数据库是sqlserver 2005, sqlserver…

树莓派(TCP客户端 )和Wemos(TCP服务端连接红外模块)通讯实现对红外设备的控制

参考:U如何用树莓派连接语音模块,红外模块来控制红外设备详解 作者:一只青木呀 发布时间:2020-08-12 17:14:10 网址:https://blog.csdn.net/weixin_45309916/article/details/107960066 目录硬件软件红外解码步骤1.连接…

java反编译微信小程序_教你如何一键反编译获取任何微信小程序源代码(图形化界面,傻瓜式操作)...

一键获取微信小程序源代码1 Tips:2   一键获取微信小程序源码, 使用了C#加nodejs制作 直接解压在D盘根目录下后就可以使用 将小程序文件放到 wxapkg目录下3 这个目录下有一些demo 可以先进行实验 使用正确 wxapkg exe这些文件应该在 D:CrackMinApp目录下4 然后打开…

PM2管理工具的使用

linux上PM2可以管理服务程序,防止程序无故关闭,具有程序守护功能,自动重启服务器程序,监控程序等好处,很方便,具体自己去体会! 官网地址: http://pm2.keymetrics.io/ 文档指南: ht…

C语言常用字符串操作函数大全详解(strstr,strtok,strrchr,strcat,strcmp,strcpy,strerror,strspn,strchr等)

参考:string.h中常用字符串操作函数说明(strstr,strtok,strrchr,strcat,strcmp,strcpy,strerror,strspn,strchr等) 作者:一只青木呀 发…

java将030A转换为方块_JAVA试题

1、下面关于变量及其作用范围的陈述哪个是不对的?( B )A.实例变量是类的成员变量。B.实例变量用关键字static声明。//Static 声明的是类变量C.在方法中定义的局部变量在该方法被执行时创建。D.局部变量在使用前必须被初始化。2、…