Documentation Home
MySQL Shell 8.0
Related Documentation Download this Manual
PDF (US Ltr) - 2.1Mb
PDF (A4) - 2.2Mb


MySQL Shell 8.0  / ...  / Extending MySQL Shell  / MySQL Shell Plugins  /  Example MySQL Shell Plugins

10.3.3 Example MySQL Shell Plugins

Example 10.3 MySQL Shell plugin containing a report and an extension object

This example defines a functionshow_processes() to display the currently running processes, and a functionkill_process() to kill a process with a specified ID.show_processes() is going to be a MySQL Shell report, andkill_process() is going to be a function provided by an extension object.

The code registersshow_processes() as a MySQL Shell reportproc using theshell.register_report() method. To registerkill_process() asext.process.kill(), the code checks whether the global objectext and the extension objectprocess already exist, and creates and registers them if not. Thekill_process() function is then added as a member to theprocess extension object.

The plugin code is saved as the file~/.mysqlsh/plugins/ext/process/init.py. At startup, MySQL Shell traverses the folders in the plugins folder, locates thisinit.py file, and executes the code. The reportproc and the functionkill() are registered and made available for use. The global objectext and the extension objectprocess are created and registered if they have not yet been registered by another plugin, otherwise the existing objects are used.

# Define a show_processes function that generates a MySQL Shell reportdef 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.runSql(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.runSql("KILL CONNECTION %d" % id).execute()# Register the show_processes function as a MySQL Shell reportshell.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 registeredif 'ext' in globals():    global_obj = extelse:    # 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 objecttry:    plugin_obj = global_obj.processexcept 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()))

Here, the user runs the reportproc using the MySQL Shell\show command, then uses theext.process.kill() function to stop one of the listed processes:

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                                                                             |+----+-----------------+-----------------+---------+----------------------------------------------------------------------------------+