MySQL的autocommit(自动提交)默认是开启,其对mysql的性能有一定影响,举个例子来说,如果你插入了1000条数据,mysql会commit1000次的,如果我们把autocommit关闭掉,通过程序来控制,只要一次commit就可以了。

1,我们可以通过set来设置autocommit

  1. mysql> set global init_connect="set autocommit=0";  //提示你用权限更高的财户来设置
  2. ERROR 1227 (42000): Access denied; you need (at least one of) the SUPER privilege(s) for this operation
  3. mysql> set autocommit=0;
  4. Query OK, 0 rows affected (0.00 sec)
  5. mysql> select @@autocommit;   //查看一下autocommit的设置
  6. +--------------+
  7. | @@autocommit |
  8. +--------------+
  9. |            0 |
  10. +--------------+
  11. 1 row in set (0.00 sec)

2,我们可以修改mysql的配置文件my.cnf来关闭autocommit

  1. [mysqld]
  2. init_connect='SET autocommit=0'  //在mysqld里面加上这些内容

用第二种方法关,有一点要注意,连接mysql用户的权限不能大于启动mysql的用户的权限,不然init_connect='SET autocommit=0'根本不会启作用,也不会报任何错误,汗一个先。看以下实例

  1. zhangy@ubuntu:~$ mysql -umysql
  2. Welcome to the MySQL monitor.  Commands end with ; or \g.
  3. Your MySQL connection id is 1
  4. Server version: 5.5.2-m2-log Source distribution
  5. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  6. mysql> select @@autocommit;     //mysql是启动用户,关闭autocommit成功
  7. +--------------+
  8. | @@autocommit |
  9. +--------------+
  10. |            0 |
  11. +--------------+
  12. 1 row in set (0.00 sec)
  13. mysql> Ctrl-C -- exit!
  14. Aborted
  15. zhangy@ubuntu:~$ mysql -uroot
  16. Welcome to the MySQL monitor.  Commands end with ; or \g.
  17. Your MySQL connection id is 2
  18. Server version: 5.5.2-m2-log Source distribution
  19. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  20. mysql> select @@autocommit;    //用root财户启动,不成功。
  21. +--------------+
  22. | @@autocommit |
  23. +--------------+
  24. |            1 |
  25. +--------------+
  26. 1 row in set (0.00 sec)

这个会不会是mysql的bug呢?我在网上找了找这方面的问题,还真有。部分内容如下:
If a user has SUPER privilege, init_connect will not execute
(otherwise if init_connect will a wrong query no one can connect to server).

Note, if init_connect is a wrong query, the connection is closing without any errors
and next command will clause 'lost connection' error.

里面有一点说的很清楚If a user has SUPER privilege, init_connect will not execute,如果用户有更高级的权限,init_connect根本不会执行。

http://blog.csdn.net/ying_593254979/article/details/12095169

mysql禁用autocommit,以及遇到的问题(转)的更多相关文章

  1. mysql禁用autocommit,以及遇到的问题

    原文地址:http://blog.51yip.com/mysql/1230.html http://blog.csdn.net/ying_593254979/article/details/12095 ...

  2. 关于mysql 的 autoCommit 参数

    首先描述一下这个参数的作用. 这个参数 默认是开启的. 开启以后再命令敲的sql 会自动提交.如果关闭,就必须手动 commit. 查看 这个 自动提交状态. SELECT @@autocommit; ...

  3. Mysql中autocommit的用法

    定义 Mysql文档原文:SET autocommit disables or enables the default autocommit mode for the current session. ...

  4. MySQL 的 autocommit

    在自动提交模式(autocommit=ON)下提交事务: •在自动提交模式下,每个SQL语句都是一个独立的事务. –mysql>insert into ACCOUNTS values(1,'To ...

  5. 【mysql】autocommit=0后,commit, rollback无效

    之前在[mysql]MySQLdb中的事务处理中用autocommit和commit()以及rollback()实现了事务处理. 但后来,用同样的代码在另一个数据库中运行却失败了.找了一个下午的原因. ...

  6. 从知乎了解到,为什么Mysql禁用存储过程、外键和级联?

    打开帖子直接一张醒目的图,是阿里巴巴的Java开发手册对Mysql相关的要求. 看看下面的回复 灵剑 存储过程没有版本控制,版本迭代的时候要更新很麻烦.存储过程如果和外部程序结合起来用,更新的时候很难 ...

  7. mysql事务,SET AUTOCOMMIT,START TRANSACTION

    http://yulei568.blog.163.com/blog/static/135886720071012444422/ MyISAM不支持 START TRANSACTION | BEGIN ...

  8. mysql事务,START TRANSACTION, COMMIT和ROLLBACK,SET AUTOCOMMIT语法

    http://yulei568.blog.163.com/blog/static/135886720071012444422/ MyISAM不支持 START TRANSACTION | BEGIN ...

  9. mysql java.sql.SQLException: Can't call commit when autocommit=true

    java.sql.SQLException: Can't call commit when autocommit=true at com.mysql.jdbc.SQLError.createSQLEx ...

随机推荐

  1. 总结一下ASP.NET MVC 网站的部署问题

    总结一下ASP.NET MVC 网站的部署问题 近日,准备把MVC建了一个新的测试站点部署到IIS上面,结果没想到出现了一系列的问题和错误,准备记录一下. 第一个问题,就是如何将MVC的站点部署到II ...

  2. [译]Stairway to Integration Services Level 16 – Flexible Source Locations (多文件导入)

    介绍 在本文中我们将利用SSIS参数,变量 以及 Foreach Loop Container 从多个源动态导入数据. 开始前我们先下载一些数据.WeatherData_Dec08_Apr09.zip ...

  3. android studio 安装与环境搭建

    转摘自:http://blog.csdn.net/zhanghefu/article/details/9286123 第一章   andriod studio 安装与环境搭建 一.Android St ...

  4. HTML5 音频视频

    HTML5 视频和音频的 DOM 参考手册 HTML5 DOM 为 <audio> 和 <video> 元素提供了方法.属性和事件. 这些方法.属性和事件允许您使用 JavaS ...

  5. 深夜,用canvas画一个时钟

    深夜,用canvas画一个时钟 查看demo 这几天准备阿里巴巴的笔试,可以说已经是心力交瘁,自从阿里和蘑菇街的内推被刷掉之后,开始越来越怀疑起自己的能力来,虽然这点打击应该是微不足道的.毕竟校招在刚 ...

  6. struts jsp传值到action,乱码的解决方案

    使用了Struts框架,前台写好了编码为utf-8 <%@ page language="java" contentType="text/html; charset ...

  7. android handler工作原理

    android handler工作原理 作用 便于在子线程中更新主UI线程中的控件 这里涉及到了UI主线程和子线程 UI主线程 它很特别.通常我们会认为UI主线程将页面绘制完成,就结束了.但是它没有. ...

  8. hibernate 事务的隔离级别 5.1

    脏读不可重复读幻读可序列化(符合事务的四个特性的正常情况 ) 解释: 脏读:事务A对数据1做了更新,但是还没有来得及提交 此时事务B对数据1进行了查询获得了事务A更新后的数据, 但是事务A因为一些原因 ...

  9. [置顶] mybatis批量新增系列之有主键的表的批量新增

    前面介绍了无主键的表的批量插入,文章地址:http://blog.csdn.net/zhouxiaoyun0228/article/details/9980181 但是在开发中往往许多的表是需要主键的 ...

  10. css网页自适应-1

    一."自适应网页设计"的概念 2010年,Ethan Marcotte提出了"自适应网页设计"(Responsive Web Design)这个名词,指可以自动 ...