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: 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 ,再重新连接。

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)。

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)

mysql autocommit_【整理】MySQL 之 autocommit相关推荐

  1. MySQL 语句整理 2019-5-3

    MySQL 语句整理 在整理完Oracle的一些常见用语句后,由于MySQL的语法跟Oracle略有不同,随跟PN的MySQL视频进行了间接整理. 查询薪水大于1800, 并且部门编号为20或30的员 ...

  2. mysql中整理设置__MySQL整理

    登录 命令:mysql -h [IP 地址/域名] -P [端口] -u [用户名] -p[密码] 端口默认是3306 mysql -hlocalhost -uroot -p 断开 exit; \q; ...

  3. MySQL知识点整理汇总

    文章目录 前言 一.数据库与SQL 1. 数据库与数据库管理系统 2. 关系数据库 3. MySQL语句的种类 4. MySQL语句的基本书写规则 二.MySQL语句的两大顺序 1. MySQL 语句 ...

  4. 结构化查询语句简称mysql_整理MySql常用查询语句

    MySql的性能优化 性能优化是通过某些有效的方法提高MySQL数据库的性能.性能优化的目的是为了是MySQL数据运行速度更快.占用的磁盘空间更小.性能优化包括很多方面,例如优化查询速度.优化更新速度 ...

  5. MYSQL学习整理(4):函数

    MYSQL零基础小白,梳理学习内容督促自己,也为方便自己日后回忆,也希望可以帮助到网友. 这里推荐宋红康老师的MySQL课程,学习资源(如有侵权请联系删除): MySQL数据库教程天花板,mysql安 ...

  6. MySQL数据库整理

    1.数据库的概念 传统的数据存储:计算机程序时运行在内存的,内存空间有限,并且不能持久保存,所以 程序运行之前需要均价在的数据.运行中需要保存的数据以及运行完需要产生的数据都需要持久化的保存,即需要保 ...

  7. 自整理---Mysql高级笔记

    MySQL 的架构介绍 数据库的种类: 列式数据库:Hbase 键值对数据库:Redis .Memcached 文档数据库:MongoDB,用于文件存储 (项目中用过,用来存储Json文件) 时序数据 ...

  8. mysql碎片整理 提速_MySQL 优化:碎片整理

    MySQL碎片就是MySQL数据文件中一些不连续的空白空间,这些空间无法再被全部利用,久而久之越来多,越来越零碎,从而造成物理存储和逻辑存储的位置顺序不一致,这就是碎片. 碎片是如何产生的 delet ...

  9. datetime类型怎么输入_精心整理MySQL基本使用(数据库的操作、数据类型、MySQL的常用命令)...

    前言:瑞典 MySQL AB 公司开发,目前属于 Oracle 公司. MySQL是一种关联数据库管理系统 由于其体积小.速度快.总体拥有成本低.MySQL软件采用了双授权政策(本词条"授权 ...

最新文章

  1. Flex报错Error #2048: 安全沙箱冲突
  2. Turn over a new leaf
  3. 神策沙龙回顾:大数据技术和金融、房产、理财的深度结合
  4. ZOJ2112(区间动态求第K大)
  5. 产品经理经验谈100篇(二)-数据分析应用,如何构建指标体系?
  6. Chapter7-11_Deep Learning for Question Answering (2/2)
  7. windows7自带录制屏幕怎么用
  8. JS字符转为json对象
  9. ubuntu + pycharm + anaconda + pyqt5 + tools 配置
  10. python的基本操作 1
  11. 前端工具Gulp的学习
  12. integer判断是否为null_面试常考题JavaScript用七种方式教你判断一个变量是否为数组类型...
  13. 精选32个最新Python实战项目(附源码),拿走就用
  14. 社区分享|Arm 中国生态技术市场经理教你玩转 TFLite Micro 端云一体解决方案
  15. 图片短链接生成器在线
  16. php 接口签署域名,使用PHP集成万网域名查询API接口
  17. 华为商店的软件可以鸿蒙,鸿蒙到底想要什么?是维护渠道的霸权还是万物互联?...
  18. xilinx Edition arm M3 使用笔记
  19. ac9560不支持承载_WiFi打游戏总卡?英特尔AC9560无线网卡了解一下
  20. 复旦提出ObjectFormer,收录CVPR 2022!图像篡改检测新工作!

热门文章

  1. Getting Started with Node.js LoopBack Framework and IBM Cloudant
  2. [WorldWind学习]17.视域调度(视域体裁剪)
  3. Struts2学习笔记——Struts2与Spring整合
  4. 滚动到底部或顶部响应的ScrollView使用
  5. Android之在Layout中自定义View
  6. 后台开发必读书籍--计算机操作系统
  7. 汇编语言--jcxz指令
  8. 基于STM32的DS1302时钟芯片驱动
  9. UART0串口编程(五):串口编程(UART0)之UC/OS(一)UC/OS下的串口发送任务编程
  10. 数组中a和a 的区别?