mysql禁用autocommit,以及遇到的問題

來源:互聯網
上載者:User

標籤:

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.   
  6. mysql> select @@autocommit;   //查看一下autocommit的設定  
  7. +--------------+  
  8. | @@autocommit |  
  9. +--------------+  
  10. |            0 |  
  11. +--------------+  
  12. 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. [email protected]:~$ 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.   
  6. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.  
  7.   
  8. mysql> select @@autocommit;     //mysql是啟動使用者,關閉autocommit成功  
  9. +--------------+  
  10. | @@autocommit |  
  11. +--------------+  
  12. |            0 |  
  13. +--------------+  
  14. 1 row in set (0.00 sec)  
  15.   
  16. mysql> Ctrl-C -- exit!  
  17. Aborted  
  18. [email protected]:~$ mysql -uroot  
  19. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  20. Your MySQL connection id is 2  
  21. Server version: 5.5.2-m2-log Source distribution  
  22.   
  23. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.  
  24.   
  25. mysql> select @@autocommit;    //用root財戶啟動,不成功。  
  26. +--------------+  
  27. | @@autocommit |  
  28. +--------------+  
  29. |            1 |  
  30. +--------------+  
  31. 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,以及遇到的問題(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.