共計 7760 個字符,預計需要花費 20 分鐘才能閱讀完成。
丸趣 TV 小編給大家分享一下 MySQL 中 help 命令怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
01help 語句信息從哪里取的
MySQL Server 提供 4 張表用于保存服務端的幫助信息(使用 help 語法查看的幫助信息),這些表位于 mysql 系統字典庫下。help 語句就是從這些表中獲取數據并返回給客戶端,如下:
help_category:關于幫助主題類別的信息
help_keyword:與幫助主題相關的關鍵字信息
help_relation:幫助關鍵字信息和主題信息之間的映射
help_topic:幫助主題的詳細內容
02help 語句信息何時產生的
這些表在數據庫初始化時通過加載 share/fill_help_tables.sql 文件創建,如果是在 Unix 上使用二進制或源代碼發行版安裝 MySQL,則在初始化數據目錄時會直接導入該文件對幫助表內容進行初始化。對于在 Linux 上的 RPM 分發版或 Windows 上的二進制發行版,幫助表的內容初始化是作為 MySQL 安裝過程的一部分執行。
如果使用二進制發行版升級 MySQL,則不會自動升級幫助表,但可以手動升級 (手工加載 share/fill_help_tables.sql 文件),如:shell mysql -u root mysql fill_help_tables.sql
您可以隨時獲取最新的 fill_help_tables.sql 以升級您的幫助表。從 http://dev.mysql.com/doc/index-other.html 下載適用于您的 MySQL 版本的正確文件
03help 幫助信息存儲表詳解
help 語法支持 3 種模式的匹配查詢:查看所有主題頂層類別或子類別、查看幫助主題下的關鍵字、使用給定主題下的唯一關鍵字查看幫助信息,這些信息分表保存在 help_category、help_topic、help_keyword 表,help_relation 表存放 help_topic 與 help_keyword 表中信息的映射信息。下面將針對這幾張表的基礎知識進行簡單的科普。
(1)help_category
該表提供查詢幫助主題的類別信息,每一個類別分別對應著 N 個幫助主題名或者主題子類別名,通過查詢表中的信息我們也可以看出來,如下:
root@localhost : mysql 01:10:59 select * from help_category;
+------------------+-----------------------------------------------+--------------------+-----+
| help_category_id | name | parent_category_id | url |
+------------------+-----------------------------------------------+--------------------+-----+
| 1 | Geographic | 0 | |
| 2 | Polygon properties | 35 | |
......
| 39 | Functions | 36 | |
| 40 | Data Definition | 36 | |
+------------------+-----------------------------------------------+--------------------+-----+
40 rows in set (0.00 sec)
表字段含義
help_category_id:幫助主題名稱或子類別名稱在表中的記錄 ID
name:幫助主題類別名稱或字類別名稱
parent_category_id:父主題類別名稱在表中的記錄 ID,一些主題類別具有子主題類別,例如:絕大多數的主題類別其實是 Contents 類別的子類別(且是頂層類別,也是一級父類別),還有一部分是 Geographic Features 類別的子類別(二級父類別),一部分是 Functions 的子類別(二級父類別)
url:對應在 MySQL 官方手冊中的鏈接地址
(2)help_keyword
該表提供查詢與幫助主題相關的關鍵字字符串信息,如下:
root@localhost : mysql 01:12:07 select * from help_keyword limit 5;
+-----------------+---------+
| help_keyword_id | name |
+-----------------+---------+
| 681 | (JSON |
| 486 | - |
| 205 | - |
| 669 | |
| 521 | ACCOUNT |
+-----------------+---------+
5 rows in set (0.00 sec)
表字段含義
help_keyword_id:幫助關鍵字名稱在表中記錄對應的 ID
name:幫助關鍵字字符串
(3)help_relation
該表提供查詢幫助關鍵字信息和主題詳細信息之間的映射,用于關聯查詢 help_keyword 與 help_topic 表,如下:
root@localhost : mysql 01:13:09 select * from help_relation limit 5;
+---------------+-----------------+
| help_topic_id | help_keyword_id |
+---------------+-----------------+
| 0 | 0 |
| 535 | 0 |
| 294 | 1 |
| 277 | 2 |
| 2 | 3 |
+---------------+-----------------+
5 rows in set (0.00 sec)
表字段含義
help_topic_id:幫助主題詳細信息 ID,該 ID 值與 help_topic 表中的 help_topic_id 相等
help_keyword_id:幫助主題關鍵字信息 ID,該 ID 值與 help_keyword 表中的 help_keyword_id 相等
(4)help_topic
該表提供查詢幫助主題給定關鍵字的詳細內容(詳細幫助信息),如下:
root@localhost : mysql 01:13:31 select * from help_topic limit 1\G;
*************************** 1. row ***************************
help_topic_id: 0
name: JOIN
help_category_id: 28
description: MySQL supports the following JOIN syntaxes for the table_references
part of SELECT statements and multiple-table DELETE and UPDATE
statements:
table_references:
escaped_table_reference [, escaped_table_reference] ...
escaped_table_reference:
table_reference
| { OJ table_reference }
......
url: http://dev.mysql.com/doc/refman/5.7/en/join.html
1 row in set (0.00 sec)
表字段含義
help_topic_id:幫助主題詳細信息在表記錄中對應的 ID
name:幫助主題給定的關鍵字名稱,與 help_keyword 表中的 name 字段值相等
help_category_id:幫助主題類別 ID,與 help_category 表中的 help_category_id 字段值相等
description:幫助主題的詳細信息(這里就是我們通常查詢幫助信息真正想看的內容,例如:告訴我們某某語句如何使用的語法與注意事項等)
example:幫助主題的示例信息(這里告訴我們某某語句如何使用的示例)
url:該幫助主題對應在 MySQL 官方在線手冊中的 URL 鏈接地址
04help 語句用法示例
前面我們提到過,help 語法支持 3 種模式的匹配查詢。那么,回到文章開頭我們拋出的問題,記不清某個語句的具體拼寫了,只能模糊的記得幾個字母,或者說很清楚知道想要查什么幫助信息,但是卻不知道用什么關鍵字來查詢幫助信息(例如:想要查看解析 relaylog 的 SQL 語句)。這個時候怎么辦呢?
(1)我只記得某幾個字母怎么辦
MySQL 提供的幫助信息實際上可以直接給定一個主題關鍵字進行查詢,不需要指定主題名稱,如果你記錄某個 SQL 子句關鍵字的其中的幾個字母,那么可以使用這些字母多嘗試幾次,如下:
root@localhost : performance_schema 10:43:40 help relay # 嘗試第一次
Nothing found
Please try to run help contents for a list of all accessible topics
root@localhost : performance_schema 10:44:00 help relay logs # 嘗試第二次
Nothing found
Please try to run help contents for a list of all accessible topics
root@localhost : performance_schema 10:44:06 help relaylogs # 嘗試第三次
Nothing found
Please try to run help contents for a list of all accessible topics
root@localhost : performance_schema 10:44:09 help relaylog # 嘗試第四次,oy,成功了
Name: SHOW RELAYLOG EVENTS
Description:
Syntax:
SHOW RELAYLOG EVENTS
[IN log_name] [FROM pos] [LIMIT [offset,] row_count] # 原來是這樣用的
Shows the events in the relay log of a replication slave. If you do not
specify log_name , the first relay log is displayed. This statement
has no effect on the master.
URL: http://dev.mysql.com/doc/refman/5.7/en/show-relaylog-events.html
PS:這里實際上就相當于那 help 語句給定的關鍵字去匹配 help_keyword 表的 name 字段,如果有記錄返回,則使用 help_category、help_keyword、help_relation、help_topic 四表做復雜的關聯查詢,右聯結 help_topic 表中的 name 字段,如果返回唯一記錄就返回幫助信息,如果返回多行,則返回一個關鍵字列表,使用這些具體的關鍵字可查詢到具體的幫助信息,例如:
root@localhost : performance_schema 11:05:06 help where
.....
where item is one of the following
topics: # 使用 where 作為關鍵字返回了一個關鍵字列表,表示 where 還會與這三個關鍵字組合使用,where 的詳細用法從列表中隨便挑選一個關鍵字即可看到
DELETE
HANDLER
UPDATE
root@localhost : performance_schema 11:09:05 help delete
Name: DELETE
Description:
Syntax:
DELETE is a DML statement that removes rows from a table.
Single-Table Syntax
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
[PARTITION (partition_name,...)]
[WHERE where_condition] # where 關鍵字的用法在這里
[ORDER BY ...]
[LIMIT row_count]
......
(2)我啥都不記得怎么辦
如果你啥都不記得,那就只能使用最笨的方法,地毯式查找
首先,我們就隨便敲幾個字母給 help 語句好了,例如:help xxx
root@localhost : performance_schema 10:09:49 help xxx;
Nothing found # 這句告訴你幫助信息沒找到
# 不要緊,下面這句告訴你,用 help contents 語句來列出所有的可能的幫助主題信息
Please try to run help contents for a list of all accessible topics
然后,查看所有的主題類別
root@localhost : performance_schema 10:31:47 help contents
You asked for help about help category: Contents
For more information, type help item , where item is one of the following
categories:
Account Management
Administration # 通過主題或主題類別名稱,大致判定一下,查看 relaylog 事件內容的語句應該是屬于管理語句
Compound Statements
Data Definition
Data Manipulation
Data Types
Functions
Functions and Modifiers for Use with GROUP BY
Geographic Features
Help Metadata
Language Structure
Plugins
Procedures
Storage Engines
Table Maintenance
Transactions
User-Defined Functions
Utility
使用 help Administration 查看該幫助主題下的所有關鍵字
root@localhost : performance_schema 10:37:27 help Administration
......
SHOW PROCEDURE CODE
SHOW PROCEDURE STATUS
SHOW PROCESSLIST
SHOW PROFILE
SHOW PROFILES
SHOW RELAYLOG EVENTS # 找到了,在這里
......
使用 SHOW RELAYLOG EVENTS 語句來查看具體的幫助信息
root@localhost : performance_schema 10:41:53 help SHOW RELAYLOG EVENTS
Name: SHOW RELAYLOG EVENTS
Description:
Syntax:
SHOW RELAYLOG EVENTS
[IN log_name] [FROM pos] [LIMIT [offset,] row_count] # 原來是這樣用的
Shows the events in the relay log of a replication slave. If you do not
specify log_name , the first relay log is displayed. This statement
has no effect on the master.
URL: http://dev.mysql.com/doc/refman/5.7/en/show-relaylog-events.html
OK,現在相信你已經比較清晰地了解了 MySQL 幫助系統的組成以及 help 到底能給我們提供一些什么幫助信息了,下面給大家再補充點小知識:
HELP 語句中給定的搜索關鍵字不區分大小寫
搜索關鍵字可以包含通配符%和_,效果與 LIKE 運算符執行的模式匹配操作含義相同。例如:HELP rep% 返回以 rep 開頭的主題列表
如果幫助類別字符串、幫助主題字符串包含多個字符的,則可以使用引號引起來,也可以不使用引號,為避免歧義,最好使用引號引起來
05 幫助信息表相關的注意事項
對于參與復制的數據庫實例,幫助表更新有一些注意事項。幫助表默認情況下會寫入到 binlog 中(因為這些幫助表是跟版本匹配的,升級一個實例的版本,其他實例也有同步更新的必要),所以,你需要考慮是否需要在升級主庫幫助表的時候同時把這些更新通過主庫 binlog 同步更新到從庫中。
如果主從庫版本不同,那么主從庫就需要單獨升級幫助信息表
如果是 MySQL 5.7.5 之前的版本,則主從庫分別升級幫助信息表使用命令:mysql –init-command= SET sql_log_bin=0 mysql fill_help_tables.sql
如果是 MySQL 5.7.5 及其之后的版本,則不需要使用 –init-command= SET sql_log_bin=0,因為 fill_help_tables.sql 文件中包含了 SET sql_log_bin=0,所以主從庫只需要分別執行命令:mysql mysql fill_help_tables.sql 即可
如果是主從版本相同,那么主從庫可以通過在主庫升級,通過復制來更新從庫的幫助信息表
如果是 MySQL 5.7.5 之前的版本,則只需要在主庫中執行命令:mysql mysql fill_help_tables.sql 即可
如果是 MySQL 5.7.5 及其之后的版本,則需要先在主庫服務器中修改 ll_help_tables.sql 文件,去掉 SET sql_log_bin=0,然后在主庫執行命令:mysql mysql fill_help_tables.sql 即可
以上是“MySQL 中 help 命令怎么用”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注丸趣 TV 行業資訊頻道!