MySQL 事務配置命令行操作和持久化


MySQL 事務配置命令行操作和持久化

參考

MySQL 官方參考手冊 提供 5.5 5.6 5.7 8.0 版本的參考手冊

https://dev.mysql.com/doc/refman/5.5/en/set-transaction.html

# MySQL 8.0 版本參考手冊
4.2.4 Specifying Program Options # 事務持久化
5.1.1 Configuring the Server # MySQL 的配置
5.1.7 Server Command Options # mysqld 的選項信息,事務持久化。
15.7.2.1 Transaction Isolation Levels
13.3.7 SET TRANSACTION Syntax 
Appendix D Indexes System Variable Index # 事務持久化
4個幫助相關的表
mysql.help_category 
mysql.help_keyword 
mysql.relation 
mysql.help_topic 存放語法格式和示例
mysqld reads options from the [mysqld] and [server] groups. mysqld_safe reads options from
the [mysqld], [server], [mysqld_safe], and [safe_mysqld] groups. mysql.server reads
options from the [mysqld] and [mysql.server] groups.
There are several ways to specify options for MySQL programs:
• List the options on the command line following the program name. This is common for options that
apply to a specific invocation of the program.
• List the options in an option file that the program reads when it starts. This is common for options
that you want the program to use each time it runs.
• List the options in environment variables (see Section 4.2.11, “Setting Environment Variables”).
This method is useful for options that you want to apply each time the program runs. In practice,
option files are used more commonly for this purpose, but Section 5.8.3, “Running Multiple MySQL
Instances on Unix”, discusses one situation in which environment variables can be very helpful. It
describes a handy technique that uses such variables to specify the TCP/IP port number and Unix
socket file for the server and for client programs.
Options are processed in order, so if an option is specified multiple times, the last occurrence takes
precedence.

幫助信息,本質上是查詢4個幫助表獲取幫助信息

隔離級別設置

mysql> help isolation
Name: 'ISOLATION'
Description:
Syntax:
SET [GLOBAL | SESSION] TRANSACTION ISOLATION LEVEL
{
    REPEATABLE READ
    | READ COMMITTED
    | READ UNCOMMITTED
    | SERIALIZABLE
}

其他事務相關的幫助

mysql> help transactions
You asked for help about help category: "Transactions"
For more information, type 'help <item>', where <item> is one of the following
topics:
   CHANGE MASTER TO
   DEALLOCATE PREPARE
   EXECUTE STATEMENT
   ISOLATION
   LOCK
   PREPARE
   PURGE BINARY LOGS
   RESET MASTER
   RESET SLAVE
   SAVEPOINT
   SET GLOBAL SQL_SLAVE_SKIP_COUNTER
   SET SQL_LOG_BIN
   START SLAVE
   START TRANSACTION
   STOP SLAVE
   XA

查看會話和全局變量

mysql> help show variables;
Name: 'SHOW VARIABLES'
Description:
Syntax:
SHOW [GLOBAL | SESSION] VARIABLES
    [LIKE 'pattern' | WHERE expr]

set 命令

mysql> help set
Name: 'SET'
Description:
Syntax:
SET variable_assignment [, variable_assignment] ...

variable_assignment:
      user_var_name = expr
    | [GLOBAL | SESSION] system_var_name = expr
    | [@@global. | @@session. | @@]system_var_name = expr

事務操作實戰 5.5版本

mysql> select @@version;
+-----------+
| @@version |
+-----------+
| 5.5.40    |
+-----------+

自動提交查詢和修改 autocommit 當前會話和全局

# 修改當前會話變量 autocommit ,可以使用 on/off 0/1 true/false
mysql> set autocommit=off;
Query OK, 0 rows affected (0.03 sec)

# 查看當前會話變量 autocommit
mysql> show variables like '%autoco%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | OFF   |
+---------------+-------+
1 row in set (0.00 sec)

# 查看全局變量 autocommit
mysql> show global variables like '%autoco%'; 
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | ON    |
+---------------+-------+
1 row in set (0.00 sec)

# 使用 0/1
mysql> set autocommit=1;
Query OK, 0 rows affected (0.05 sec)

mysql> show variables like '%autoco%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | ON    |
+---------------+-------+
1 row in set (0.00 sec)

事務隔離級別查詢和修改 當前會話和全局

注意:必須加上 session/global 不然不起作用

# 設置當前會話的事務隔離級別。 注意:必須寫 session/global ,不然不起作用。
mysql> set session TRANSACTION ISOLATION LEVEL REPEATABLE READ;
mysql> set session TRANSACTION ISOLATION LEVEL READ COMMITTED;
mysql> set session TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
mysql> set session TRANSACTION ISOLATION LEVEL SERIALIZABLE;
Query OK, 0 rows affected (0.00 sec)

# 查詢當前會話的事務隔離級別
mysql> show variables like '%iso%';
+---------------+----------------+
| Variable_name | Value          |
+---------------+----------------+
| tx_isolation  | READ-COMMITTED |
+---------------+----------------+
1 row in set (0.00 sec)

# 查詢全局的事務隔離級別
mysql> show global variables like '%iso%';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| tx_isolation  | REPEATABLE-READ |
+---------------+-----------------+
1 row in set (0.00 sec)

事務配置持久化

將 MySQL 配置選項幫助信息保存到文件

C:\Users\jie>mysqld --verbose --help > d:\00\a.txt
transaction-isolation  REPEATABLE-READ
autocommit             TRUE
鍵 transaction-isolation 鍵可以用連字符或者下划線
取值 READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, or SERIALIZABLE
To set the global default isolation level at server startup, use the
--transaction-isolation=level option to mysqld on the command line or
in an option file.

Values of level for this option use dashes rather than spaces, so the permissible values are READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, or SERIALIZABLE. 

For example, to set the default isolation level to REPEATABLE READ, use these lines in the [mysqld] section of an option file:

[mysqld]
transaction-isolation = REPEATABLE-READ

持久化實戰

# D:\chengxu\MySQL\mysql-5.5\my.ini 文件中添加如下配置。
# D:\chengxu\MySQL\mysql-5.5\ 是我的 MySQL 5.5 安裝目錄。

# 事務隔離級別 鍵可以用連字符或下划線分隔。值是固定的,使用連字符相連而不是空格。
# READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, or SERIALIZABLE
transaction_isolation=READ-COMMITTED
# 配置自動提交 on/off 0/1 true/false
autocommit=on


免責聲明!

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



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