日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

【整理】MySQL 之 autocommit

發(fā)布時(shí)間:2023/12/10 数据库 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【整理】MySQL 之 autocommit 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>


mysql 默認(rèn)是開啟 auto commit 的??梢酝ㄟ^如下命令查看 session 級(jí)別和 global 級(jí)別的設(shè)置:
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 執(zhí)行自動(dòng)提交時(shí),應(yīng)該如何禁用 autocommit 呢?可以通過 Cmd-Line、Option file、System Var 上都可用的 init_connect 來設(shè)置。
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. ? ? ?? 上面這段話的意思是,每個(gè) client 連接上來時(shí)都會(huì)由 server 執(zhí)行一次由 init_connect 指定的 sql 字串。(是否可以認(rèn)為是基于 session 的?)

利用這個(gè)變量,可以通過如下方式禁用 autocommit:
方法一:
mysql>SET GLOBAL init_connect='SET autocommit=0'; 方法二:
在 MySQL 的配置文件中設(shè)置
[mysqld] init_connect='SET autocommit=0' 方法三:
啟動(dòng) mysql 時(shí)帶上命令行參數(shù) –init_connect='SET autocommit=0'

值得說明的一點(diǎn)是,這個(gè)參數(shù)的設(shè)置對(duì)擁有 super 權(quán)限的用戶是無效的,具體原因說明如下:
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. ? ? ?? 默認(rèn)開啟的 autocommit 肯定會(huì)對(duì) mysql 的性能有一定影響,但既然默認(rèn)開啟必定是有原因的,所以如果你不知道自己到底會(huì)遇到什么問題的情況下還是不要改這個(gè)設(shè)置為妙。舉個(gè)例子來說明開啟 autocommit 會(huì)產(chǎn)生的性能影響,如果你插入了 1000 條數(shù)據(jù),mysql 會(huì) commit 1000 次,如果我們把 autocommit 關(guān)閉掉,通過程序來控制,只要一次commit 就可以了。

========= 我是分割線? =========

? ? ? 另外一篇博客《Innodb表類型中autocommit的設(shè)置》中展示了設(shè)置 autocommit 為 0 的效果。

a. 初始狀態(tài)+設(shè)置 session 級(jí)別的 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. 創(chuàng)建一個(gè)測(cè)試表
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. 插入數(shù)據(jù)
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> ? ? ?? 發(fā)現(xiàn) binlog 中僅記錄了 create table 動(dòng)作,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> ? ? ?? 發(fā)現(xiàn)什么數(shù)據(jù)都沒有。為什么呢?因?yàn)?SQL 語句并沒有被自己(當(dāng)前 session)提交給 server 端去處理,只是在當(dāng)前連接中做了相應(yīng)處理。


重復(fù)上面的實(shí)驗(yàn),但是保持 autocommit 的默認(rèn)值(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> 這回該有的都有了。

========= 我是分割線? =========

網(wǎng)友說法:
不要設(shè)定 autocommit 這個(gè)開關(guān),讓它保持 autocommit=1 這個(gè)默認(rèn)狀態(tài)。 平常有查詢\更新都是需要得到最新的數(shù)據(jù),根本不需要啟動(dòng)一個(gè)事務(wù),除非有特定狀況才需要開啟事務(wù),再手工用 start transaction ... commit /rollback 。 這種全局設(shè)置,在生產(chǎn)環(huán)境中沒有多大意義。一般都是在應(yīng)用程序框架(如連接池的庫)中設(shè)置 autocommit 是否為 ON/OFF, 說白了,就是得到數(shù)據(jù)庫連接以后,顯示的調(diào)用一次 set autocommit on/off (or =1/0)


轉(zhuǎn)載于:https://my.oschina.net/moooofly/blog/169824

總結(jié)

以上是生活随笔為你收集整理的【整理】MySQL 之 autocommit的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。