MySQL Study之--MySQL关闭自动commit(autocommit)

MySQL Study之--MySQL关闭自动commit(autocommit)


     对于mysql来讲,在事务处理时,默认是在动提交的(autocommit),以下方法可以自动关闭autocommit;

案例分析:

1、在mysql登录环境下修改

[[email protected] soft]# 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.25-73.1 Percona Server (GPL), Release 73.1, Revision 07b797f
Copyright (c) 2009-2015 Percona LLC and/or its affiliates
Copyright (c) 2000, 2015, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.02 sec)

mysql> select version();
+-------------+
| version()   |
+-------------+
| 5.6.25-73.1 |
+-------------+
1 row in set (0.00 sec)

mysql> show variables like ‘%autocommit%‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | ON    |                ;;默认autocommit是开启的
+---------------+-------+
1 row in set (0.03 sec)

在当前session关闭autocommit:
mysql> set @@session.autocommit=0;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like ‘%autocommit%‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | OFF   |
+---------------+-------+
1 row in set (0.00 sec)

在global级别关闭autocommit:
mysql> set @@global.autocommit=0;

Query OK, 0 rows affected (0.01 sec)

创建普通用户:
mysql> create user tom identified by ‘tom‘;

Query OK, 0 rows affected (0.00 sec)

mysql> grant all on prod.* to ‘tom‘@‘localhost‘ identified by ‘tom‘;
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

普通用户登录:
[[email protected] ~]# mysql -u tom -p

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.25-73.1 Percona Server (GPL), Release 73.1, Revision 07b797f
Copyright (c) 2009-2015 Percona LLC and/or its affiliates
Copyright (c) 2000, 2015, 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> use mysql;
ERROR 1044 (42000): Access denied for user ‘tom‘@‘localhost‘ to database ‘mysql‘
mysql> use  prod;
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> show variables like ‘%commit%‘;
+-------------------------------------------+-------+
| Variable_name                             | Value |
+-------------------------------------------+-------+
| autocommit                                | OFF   |
| binlog_order_commits                      | ON    |
| innodb_api_bk_commit_interval             | 5     |
| innodb_commit_concurrency                 | 0     |
| innodb_flush_log_at_trx_commit            | 1     |
| innodb_use_global_flush_log_at_trx_commit | ON    |
+-------------------------------------------+-------+
6 rows in set (0.00 sec)

创建测试表:
mysql> create table t1(id int,name varchar(10));

Query OK, 0 rows affected (0.15 sec)

mysql> insert into t1 values (10,‘tom‘);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;
+------+------+
| id   | name |
+------+------+
|   10 | tom
 |
+------+------+
1 row in set (0.00 sec)

事务回滚:
mysql> rollback;

Query OK, 0 rows affected (0.02 sec)

mysql> select * from t1;
Empty set (0.00 sec)

2、在mysql service重启后
mysql server 重启后:
[[email protected] ~]# service mysql stop
Shutting down MySQL (Percona Server)....[  OK  ]
[[email protected] ~]# service mysql start
Starting MySQL (Percona Server).....[  OK  ]
[[email protected] ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.25-73.1 Percona Server (GPL), Release 73.1, Revision 07b797f
Copyright (c) 2009-2015 Percona LLC and/or its affiliates
Copyright (c) 2000, 2015, 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> show variables like ‘%commit%‘;
+-------------------------------------------+-------+
| Variable_name                             | Value |
+-------------------------------------------+-------+
| autocommit                                | ON    |             ;;autocommit仍然是开启状态
+-------------------------------------------+-------+
6 rows in set (0.01 sec)

编辑/etc/my.cnf文件:

[[email protected] ~]# vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
init_connect=‘set autocommit=0‘                                    ;;用户登录时,关闭autocommit
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
explicit_defaults_for_timestamp=true
innodb_buffer_pool_size = 128M
join_buffer_size = 128M
sort_buffer_size = 2M
read_rnd_buffer_size = 2M

用户登录查看:
[[email protected] ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.25-73.1 Percona Server (GPL), Release 73.1, Revision 07b797f
Copyright (c) 2009-2015 Percona LLC and/or its affiliates
Copyright (c) 2000, 2015, 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> show variables like ‘%commit%‘;
+-------------------------------------------+-------+
| Variable_name                             | Value |
+-------------------------------------------+-------+
| autocommit                                | ON    |                ;;root用户不受影响(为安全起见)

mysql> system mysql -u tom -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.25-73.1 Percona Server (GPL), Release 73.1, Revision 07b797f
Copyright (c) 2009-2015 Percona LLC and/or its affiliates
Copyright (c) 2000, 2015, 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> show variables like ‘%commit%‘;
+-------------------------------------------+-------+
| Variable_name                             | Value |
+-------------------------------------------+-------+
| autocommit                                | OFF   |                ;;普通用户,autocommit已被关闭
+-------------------------------------------+-------+

时间: 2024-10-28 21:36:22

MySQL Study之--MySQL关闭自动commit(autocommit)的相关文章

MySQL Study之--MySQL压力测试工具mysqlslap

MySQL Study之--MySQL压力测试工具mysqlslap 一.Mysqlslap介绍 mysqlslap是MySQL5.1之后自带的benchmark基准测试工具,类似Apache Bench负载产生工具,生成schema,装载数据,执行benckmark和查询数据,语法简单,灵活,容易使用.该工具可以模拟多个客户端同时并发的向服务器发出查询更新,给出了性能测试数据而且提供了多种引擎的性能比较.mysqlslap为mysql性能优化前后提供了直观的验证依据,笔者建议系统运维人员应该掌

MySQL Study之--MySQL Cluster(集群)构建

MySQL Study之--MySQL Cluster(集群)构建 一.Mysql Cluster概述与部署 MySql Cluster最显著的优点就是高可用性,高实时性,高冗余,扩展性强. 它允许在无共享的系统中部署"内存中"数据库的Cluster.通过无共享体系结构,系统能够使用廉价的硬件.此外,由于每个组件有自己的内存和磁盘,所以不存在单点故障. 它由一组计算机构成,每台计算机上均运行者多种进程,包括mysql服务器,NDB cluster的数据节点,管理服务启,以及专门的数据访

MySQL Study之--MySQL体系结构深入解析

MySQL Study之--MySQL体系结构深入解析 MySQL体系架构 由连接池组件.管理服务和工具组件.sql接口组件.查询分析器组件.优化器组件.缓冲组件.插件式存储引擎.物理文件组成.mysql是独有的插件式体系结构,各个存储引擎有自己的特点. MySQL内存结构: Mysql 进程结构 Mysql不像oracle那样是通过多进程来完成其功能的.默认情况下,InnoDB存储引擎的后台线程有7个: 4个IO thread, 1个master thread, 1个锁(lock)监控线程,

MySQL Study之--MySQL 表连接

MySQL Study之--MySQL 表连接 一.Join语法概述 join 用于多表中字段之间的联系,语法如下: ... FROM table1 INNER|LEFT|RIGHT JOIN table2 ON condition table1:左表:table2:右表. JOIN 按照功能大致分为如下三类: INNER JOIN(内连接,或等值连接):取得两个表中存在连接匹配关系的记录. LEFT JOIN(左连接):取得左表(table1)完全记录,即是右表(table2)并无对应匹配记录

MySQL Study之--MySQL集群之mysql 主从复制

MySQL Study之--MySQL集群之mysql 主从复制 MySQL 的数据库的高可用性的架构大概有以下几种:集群,读写分离,主备.而后面两种都是通过复制来实现的.下面将简单介绍复制的原理及配置 复制的原理 MySQL 复制基于主服务器在二进制日志中跟踪所有对数据库的更改(更新.删除等等).每个从服务器从主服务器接收主服务器已经记录到其二进制日志的保存的更新,以便从服务器可以对其数据拷贝执行相同的更新. 将主服务器的数据拷贝到从服务器的一个途径是使用LOAD DATA FROM MAST

MySql Study之--MySql日志管理

MySql Study之--MySql日志管理 一.日志文件类型 MySQL有几个不同的日志文件,可以帮助你找出mysqld内部发生的事情: 日志文件 日志文件信息内容 错误日志 记录启动.运行或停止mysqld时出现的问题.(log_err) 查询日志 记录建立的客户端连接和执行的语句. 更新日志 记录更改数据的语句,不赞成使用该日志. 二进制日志 记录所有更改数据的语句.还用于复制.(bin_log) 慢日志 记录所有执行时间超过long_query_time秒的所有查询或不使用索引的查询.

MySQL Study之--MySQL下图形工具的使用(phpMyAdmin)

MySQL Study之--MySQL下图形工具的使用(phpMyAdmin) 系统环境: RedHat EL6 数据库:  MySQL 5.6.4-m7 phpMyAdmin是一个用PHP编写的软件工具,是以PHP为基础,以Web-Base方式架构在网站主机上的MySQL的数据库管理工具,让管理者可用Web接口管理MySQL数据库.借由此Web接口可以成为一个简易方式输入繁杂SQL语法的较佳途径,尤其要处理大量资料的汇入及汇出更为方便.其中一个更大的优势在于由于phpMyAdmin跟其他PHP

MySQL Study之--MySQL schema_information数据库

MySQL Study之--MySQL schema_information数据库   information_schema数据库是在mysql的版本5.0之后产生的,一个虚拟数据库,物理上并不存在.       information_schema数据库类似与"数据字典",提供了访问数据库元数据的方式,即数据的数据.比如数据库名或表名,列类型,访问权限(更加细化的访问方式). 案例: mysql> show databases; +--------------------+ |

MySQL Study之--Mysql数据库备份工具(mysqldump)

MySQL Study之--Mysql数据库备份工具(mysqldump) 对于Mysql Database的备份方式有很多种,此次文档主要介绍mysqldump工具: mysqldump:      mysqldump工具很多方面类似相反作用的工具mysqlimport.它们有一些同样的选项.但mysqldump能够做更多的事情.它可以把整个数据库装载到一个单独的文本文件中.这个文件包含有所有重建您的数据库所需要的SQL命令.这个命令取得所有的模式(Schema,后面有解释)并且将其转换成DD