在MySQL中設置事務隔離級別有2種方法:


在MySQL中設置事務隔離級別有2種方法:

1 在my.cnf中設置,在mysqld選項中如下設置

[mysqld]

 transaction-isolation = READ-COMMITTED

 

2 在mysql窗口用set命令重置

  1. mysql> set global tx_isolation='REPEATABLE-READ';  
  2. Query OK, 0 rows affected (0.01 sec)  
  3.    
  4. mysql>  

查詢當前的會話事務級別,可以使用:

  1. mysql> select @@tx_isolation;  
  2. +----------------+  
  3. | @@tx_isolation |  
  4. +----------------+  
  5. READ-COMMITTED |  
  6. +----------------+  
  7. 1 row in set (0.00 sec)  
  8.    
  9. mysql>  


查詢全局的事務隔離級別,可以使用

  1. mysql> select @@global.tx_isolation;  
  2. +-----------------------+  
  3. | @@global.tx_isolation |  
  4. +-----------------------+  
  5. READ-COMMITTED        |  
  6. +-----------------------+  
  7. 1 row in set (0.00 sec)  
  8.    
  9. mysql>  


 

Serializable模式下。

      1. mysql> system cat /usr/local/mysql56m2/my.cnf |grep  transaction-isolation  
      2. transaction-isolation = READ-COMMITTED  
      3. mysql> 

         

         

復制二進制與隔離級別的關系

在SERIALIZABLE模式下,Innodb存儲引擎會對每個select語句自動加Lock in sharedmode,給每一個讀操作加共享鎖。因此在這個隔離級別下,讀占用鎖了,一致性的非鎖定讀不再予以支持。因為Innodb存儲引擎在 repeatable read 模式下就已經達到了3度的隔離,所以一般不在本地事務中使用serializable隔離級別,serializable的事務隔離級別主要用於 innodb存儲引擎的分布式事務。

 

在Read committed的隔離模式下,除了唯一性約束檢查以及外鍵約束檢查需要Gap lock,innodb存儲引擎不會使用gap lock的鎖算法。不過使用read committed隔離級別需要注意一些問題,mysql5.1中,Read committed的事務隔離級別默認只能在replication的二進制為row格式下,如果二進制默認在statement模式下,則會報如下錯 誤:

  1. mysql> select @@version;  
  2. +-------------+  
  3. | @@version   |  
  4. +-------------+  
  5. | 5.5.25a-log |  
  6. +-------------+  
  7. 1 row in set (0.00 sec)  
  8.    
  9. mysql>  
  10. mysql> select @@binlog_format;  
  11. +-----------------+  
  12. | @@binlog_format |  
  13. +-----------------+  
  14. | STATEMENT       |  
  15. +-----------------+  
  16. 1 row in set (0.00 sec)  
  17.    
  18. mysql> select @@tx_isolation;  
  19. +-----------------+  
  20. | @@tx_isolation  |  
  21. +-----------------+  
  22. REPEATABLE-READ |  
  23. +-----------------+  
  24. 1 row in set (0.00 sec)  
  25.    
  26. mysql> set tx_isolation='READ-COMMITTED';  
  27. Query OK, 0 rows affected (0.00 sec)  
  28.    
  29. mysql> use test;  
  30. Database changed  
  31. mysql> create table a (b int, primary key (b)) engine=innodb;  
  32. ERROR 1050 (42S01): Table 'a' already exists  
  33. mysql> select @@tx_isolation;  
  34. +----------------+  
  35. | @@tx_isolation |  
  36. +----------------+  
  37. READ-COMMITTED |  
  38. +----------------+  
  39. 1 row in set (0.00 sec)  
  40.    
  41. mysql> begin  
  42.     -> ;  
  43. Query OK, 0 rows affected (0.00 sec)  
  44.    
  45. mysql> insert into a select 100000;  
  46. ERROR 1665 (HY000): Cannotexecute statement: impossible to write to binary log since BINLOG_FORMAT =STATEMENT and at least one table uses a storage engine limited to row-basedlogging. InnoDB is limited to row-logging when transaction isolation level isREAD COMMITTED or READ UNCOMMITTED.  

 ERROR 1665 (HY000): Cannotexecute statement: impossible to write to binary log since BINLOG_FORMAT =STATEMENT and at least one table uses a storage engine limited to row-basedlogging. InnoDB is limited to row-logging when transaction isolation level isREAD COMMITTED or READ UNCOMMITTED. 

[Note]:在mysql5.1以及mysql5.6模式下實驗過都是如此。也許可以知道通過將innodb_locks_unsafe_for_binlog設置為1,來可以使binlog日志在statement下使用readcommitted的事務隔離級別:

  1. mysql> select @@innodb_locks_unsafe_for_binlog;  
  2. +----------------------------------+  
  3. | @@innodb_locks_unsafe_for_binlog |  
  4. +----------------------------------+  
  5. |                               0 |  
  6. +----------------------------------+  
  7. 1 row in set (0.00 sec)  
  8. mysql> set global innodb_locks_unsafe_for_binlog=1;  
  9. ERROR 1238 (HY000): Variable 'innodb_locks_unsafe_for_binlog' is a readonly variable  
  10. mysql>  


此參數是只讀模式,需要修改my.cnf重新啟動才行。

在my.cnf里面的[mysqld]添加

[mysqld]

innodb_locks_unsafe_for_binlog = 1

然后重啟,然后去check模仿一個事務操作,如下所示:

    1. mysql> select @@innodb_locks_unsafe_for_binlog;  
    2. +----------------------------------+  
    3. | @@innodb_locks_unsafe_for_binlog |  
    4. +----------------------------------+  
    5. |                               1 |  
    6. +----------------------------------+  
    7. 1 row in set (0.00 sec)  
    8.    
    9. mysql>  
    10. mysql> use test;  
    11. Reading table information for completion of table and column names  
    12. You can turn off this feature to get a quicker startup with -A  
    13.    
    14. Database changed  
    15. mysql> select @@tx_isolation;  
    16. +----------------+  
    17. | @@tx_isolation |  
    18. +----------------+  
    19. READ-COMMITTED |  
    20. +----------------+  
    21. 1 row in set (0.00 sec)  
    22. mysql> begin;  
    23. Query OK, 0 rows affected (0.00 sec)  
    24.    
    25. mysql> insert into t select 15;  
    26. Query OK, 1 row affected (0.00 sec)  
    27. Records: 1  Duplicates: 0  Warnings: 0  
    28.    
    29. mysql> commit;  
    30. Query OK, 0 rows affected (0.00 sec)  
    31. mysql> select * from t;  
    32. +--------+  
    33. | id     |  
    34. +--------+  
    35. |      1 |  
    36. |     12 |  
    37. |     15 |  
    38. |  11111 |  
    39. | 111110 |  
    40. +--------+  
    41. rows in set (0.00 sec) 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM