查看和修改MySQL數據庫參數

查看和修改MySQL數據庫參數

MySQL依賴大量的參數來控制SQL的處理執行過程。mysql.cnf文件是mysql默認的參數配置文件,mysql啓動時會優先在一些特定位置尋找並讀取該文件。my.cnf不存在時並不會影響MySQL實例的初始化啓動,參數值取決於編譯MySQL時指定默認值和源代碼中指定參數的默認值。

一、MySQL參數的分類

MySQL參數可以分爲靜態(static)參數和動態(dynamic)參數,區別在於參數值是否可以在實例的生命週期內修改並生效。

1、靜態參數

靜態參數在數據庫啓動期間不能被修改。靜態參數設置之後必須要重啓才能生效。比如:log_slave_updates,back_log,log_bin,lower_case_table_names。對於靜態參數,沒有global級和session級區分。

2、動態參數

動態參數在數據庫啓動期間能被修改,動態參數又分爲兩種:global級,session級。session作用域修改後不影響其他已經開啓和之後開啓的session。global作用域參數值修改後,修改前已經開啓的session不會生效,會在新創建的session中生效。

二、MySQL參數查看

以wait_timeout參數爲例:

1、global級參數的查看

方法一:

mysql> select @@global.wait_timeout;
+-----------------------+
| @@global.wait_timeout |
+-----------------------+
|                 28800 |
+-----------------------+
1 row in set (0.00 sec)

方法二:

mysql> show global variables like '%wait_timeout%';
+--------------------------+----------+
| Variable_name            | Value    |
+--------------------------+----------+
| innodb_lock_wait_timeout | 50       |
| lock_wait_timeout        | 31536000 |
| wait_timeout             | 28800    |
+--------------------------+----------+
3 rows in set (0.01 sec)

方法三:

mysql> select * from performance_schema.global_variables where variable_name ='wait_timeout';
+---------------+----------------+
| VARIABLE_NAME | VARIABLE_VALUE |
+---------------+----------------+
| wait_timeout  | 28800          |
+---------------+----------------+
1 row in set (0.00 sec)

mysql> select * from performance_schema.global_variables where variable_name like '%wait_timeout%';
+--------------------------+----------------+
| VARIABLE_NAME            | VARIABLE_VALUE |
+--------------------------+----------------+
| innodb_lock_wait_timeout | 50             |
| lock_wait_timeout        | 31536000       |
| wait_timeout             | 28800          |
+--------------------------+----------------+
3 rows in set (0.01 sec)

2、session級參數的查看

方法一:

mysql> select @@wait_timeout;
+----------------+
| @@wait_timeout |
+----------------+
|          28800 |
+----------------+
1 row in set (0.00 sec)

方法二:

mysql> select @@session.wait_timeout;
+------------------------+
| @@session.wait_timeout |
+------------------------+
|                  28800 |
+------------------------+
1 row in set (0.00 sec)

方法三:

mysql> show variables like '%wait_timeout%';
+--------------------------+----------+
| Variable_name            | Value    |
+--------------------------+----------+
| innodb_lock_wait_timeout | 50       |
| lock_wait_timeout        | 31536000 |
| wait_timeout             | 28800    |
+--------------------------+----------+
3 rows in set (0.00 sec)

方法四:

mysql> show session variables like '%wait_timeout%';
+--------------------------+----------+
| Variable_name            | Value    |
+--------------------------+----------+
| innodb_lock_wait_timeout | 50       |
| lock_wait_timeout        | 31536000 |
| wait_timeout             | 28800    |
+--------------------------+----------+
3 rows in set (0.00 sec)

方法五:

mysql> select * from performance_schema.session_variables where variable_name like '%wait_timeout%';
+--------------------------+----------------+
| VARIABLE_NAME            | VARIABLE_VALUE |
+--------------------------+----------------+
| innodb_lock_wait_timeout | 50             |
| lock_wait_timeout        | 31536000       |
| wait_timeout             | 28800          |
+--------------------------+----------------+
3 rows in set (0.00 sec)

三、MySQL參數修改

1、session級參數的修改

方法一:

mysql> set wait_timeout=10;
Query OK, 0 rows affected (0.00 sec)

方法二:

mysql> set session wait_timeout=10;
Query OK, 0 rows affected (0.00 sec)

方法三:

mysql> set @@wait_timeout=10;
Query OK, 0 rows affected (0.00 sec)

方法四:

mysql> set @@session.wait_timeout=10;
Query OK, 0 rows affected (0.00 sec)

2、global級參數的修改

方法一:

mysql> set global wait_timeout=10;
Query OK, 0 rows affected (0.00 sec)

方法二:

mysql> set @@global.wait_timeout=10;
Query OK, 0 rows affected (0.00 sec)

四、將參數值設置爲MySQL的默認值

1、session級參數

mysql> set wait_timeout=DEFAULT;
Query OK, 0 rows affected (0.00 sec)

2、global級參數

mysql> set global wait_timeout=DEFAULT;
Query OK, 0 rows affected (0.00 sec)

五、靜態參數的修改以及使動態參數永久生效

如果需要使動態參數的設置永久生效,必須修改參數文件並重啓MySQL生效。而靜態參數只能通過修改參數文件使之生效。比如:

# vi /etc/my.cnf
[mysqld]
wait_timeout=10
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章