標籤:
MySQL的autocommit(自動認可)預設是開啟,其對mysql的效能有一定影響,舉個例子來說,如果你插入了1000條資料,mysql會commit1000次的,如果我們把autocommit關閉掉,通過程式來控制,只要一次commit就可以了。
1,我們可以通過set來設定autocommit
查看複製列印?
- mysql> set global init_connect="set autocommit=0"; //提示你用許可權更高的財戶來設定
- ERROR 1227 (42000): Access denied; you need (at least one of) the SUPER privilege(s) for this operation
- mysql> set autocommit=0;
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> select @@autocommit; //查看一下autocommit的設定
- +--------------+
- | @@autocommit |
- +--------------+
- | 0 |
- +--------------+
- 1 row in set (0.00 sec)
2,我們可以修改mysql的設定檔my.cnf來關閉autocommit
查看複製列印?
- [mysqld]
- init_connect=‘SET autocommit=0‘ //在mysqld裡面加上這些內容
用第二種方法關,有一點要注意,串連mysql使用者的許可權不能大於啟動mysql的使用者的許可權,不然init_connect=‘SET autocommit=0‘根本不會啟作用,也不會報任何錯誤,汗一個先。看以下執行個體
查看複製列印?
- [email protected]:~$ mysql -umysql
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 1
- Server version: 5.5.2-m2-log Source distribution
-
- Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
-
- mysql> select @@autocommit; //mysql是啟動使用者,關閉autocommit成功
- +--------------+
- | @@autocommit |
- +--------------+
- | 0 |
- +--------------+
- 1 row in set (0.00 sec)
-
- mysql> Ctrl-C -- exit!
- Aborted
- [email protected]:~$ mysql -uroot
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 2
- Server version: 5.5.2-m2-log Source distribution
-
- Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
-
- mysql> select @@autocommit; //用root財戶啟動,不成功。
- +--------------+
- | @@autocommit |
- +--------------+
- | 1 |
- +--------------+
- 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,以及遇到的問題(轉)