PDF (US Ltr) - 2.2MB
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 遍历 plugins 文件夹中的文件夹,找到此 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 |
+----+-----------------+-----------------+---------+----------------------------------------------------------------------------------+