PDF (US Ltr) - 2.3Mb
PDF (A4) - 2.3Mb
範例 10.3 包含報表和擴充物件的 MySQL Shell 外掛
此範例定義一個函式 show_processes()
以顯示目前正在執行的程序,以及一個函式 kill_process()
以使用指定的 ID 終止程序。show_processes()
將會是 MySQL Shell 報表,而 kill_process()
將會是由擴充物件提供的函式。
程式碼使用 shell.register_report()
方法將 show_processes()
註冊為 MySQL Shell 報表 proc
。若要將 kill_process()
註冊為 ext.process.kill()
,程式碼會檢查全域物件 ext
和擴充物件 process
是否已存在,如果不存在,則建立並註冊它們。然後將 kill_process()
函式新增為 process
擴充物件的成員。
外掛程式碼會儲存為檔案 ~/.mysqlsh/plugins/ext/process/init.py
。啟動時,MySQL Shell 會走訪外掛資料夾中的資料夾,找到此 init.py
檔案,並執行程式碼。報表 proc
和函式 kill()
會註冊並可供使用。如果全域物件 ext
和擴充物件 process
尚未由另一個外掛註冊,則會建立並註冊它們,否則會使用現有的物件。
# Define a show_processes function that generates a MySQL Shell report
def show_processes(session, args, options):
query = "SELECT ID, USER, HOST, COMMAND, INFO FROM INFORMATION_SCHEMA.PROCESSLIST"
if (options.has_key('command')):
query += " WHERE COMMAND = '%s'" % options['command']
result = session.sql(query).execute();
report = []
if (result.has_data()):
report = [result.get_column_names()]
for row in result.fetch_all():
report.append(list(row))
return {"report": report}
# Define a kill_process function that will be exposed by the global object 'ext'
def kill_process(session, id):
result = session.sql("KILL CONNECTION %d" % id).execute()
# Register the show_processes function as a MySQL Shell report
shell.register_report("proc", "list", show_processes, {"brief":"Lists the processes on the target server.",
"options": [{
"name": "command",
"shortcut": "c",
"brief": "Use this option to list processes over specific commands."
}]})
# Register the kill_process function as ext.process.kill()
# Check if global object 'ext' has already been registered
if 'ext' in globals():
global_obj = ext
else:
# Otherwise register new global object named 'ext'
global_obj = shell.create_extension_object()
shell.register_global("ext", global_obj,
{"brief":"MySQL Shell extension plugins."})
# Add the 'process' extension object as a member of the 'ext' global object
try:
plugin_obj = global_obj.process
except IndexError:
# If the 'process' extension object has not been registered yet, do it now
plugin_obj = shell.create_extension_object()
shell.add_extension_object_member(global_obj, "process", plugin_obj,
{"brief": "Utility object for process operations."})
# Add the kill_process function to the 'process' extension object as member 'kill'
try:
shell.add_extension_object_member(plugin_obj, "kill", kill_process, {"brief": "Kills the process with the given ID.",
"parameters": [
{
"name":"session",
"type":"object",
"class":"Session",
"brief": "The session to be used on the operation."
},
{
"name":"id",
"type":"integer",
"brief": "The ID of the process to be killed."
}
]
})
except Exception as e:
shell.log("ERROR", "Failed to register ext.process.kill ({0}).".
format(str(e).rstrip()))
在這裡,使用者使用 MySQL Shell \show
命令執行報表 proc
,然後使用 ext.process.kill()
函式停止其中一個列出的程序
mysql-py> \show proc
+----+-----------------+-----------------+---------+----------------------------------------------------------------------------------+
| ID | USER | HOST | COMMAND | INFO |
+----+-----------------+-----------------+---------+----------------------------------------------------------------------------------+
| 66 | root | localhost:53998 | Query | PLUGIN: SELECT ID, USER, HOST, COMMAND, INFO FROM INFORMATION_SCHEMA.PROCESSLIST |
| 67 | root | localhost:34022 | Sleep | NULL |
| 4 | event_scheduler | localhost | Daemon | NULL |
+----+-----------------+-----------------+---------+----------------------------------------------------------------------------------+
mysql-py> ext.process.kill(session, 67)
mysql-py> \show proc
+----+-----------------+-----------------+---------+----------------------------------------------------------------------------------+
| ID | USER | HOST | COMMAND | INFO |
+----+-----------------+-----------------+---------+----------------------------------------------------------------------------------+
| 66 | root | localhost:53998 | Query | PLUGIN: SELECT ID, USER, HOST, COMMAND, INFO FROM INFORMATION_SCHEMA.PROCESSLIST |
| 4 | event_scheduler | localhost | Daemon | NULL |
+----+-----------------+-----------------+---------+----------------------------------------------------------------------------------+