您好,欢迎访问一九零五行业门户网

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

mysql study之--mysql关闭自动commit(autocommit) 对于mysql来讲,在事务处理时,默认是在动提交的(autocommit),以下方法可以自动关闭autocommit; 案例分析: 1、在mysql登录环境下修改 [root@mysql2 soft]# mysql -u root -p enter password: welcome
mysql study之--mysql关闭自动commit(autocommit)
对于mysql来讲,在事务处理时,默认是在动提交的(autocommit),以下方法可以自动关闭autocommit;
案例分析:
1、在mysql登录环境下修改
[root@mysql2 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)
普通用户登录:
[root@mysql2 ~]# 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 重启后:
[root@mysql2 ~]# service mysql stop
shutting down mysql (percona server)....[  ok  ]
[root@mysql2 ~]# service mysql start
starting mysql (percona server).....[  ok  ]
[root@mysql2 ~]# 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文件:
[root@mysql2 ~]# 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
用户登录查看:
[root@mysql2 ~]# 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已被关闭
+-------------------------------------------+-------+
其它类似信息

推荐信息