With MySQL Shell 8.0.17, a super cool new feature was released: the MySQL Shell Extensions & Plugins !
You will be able to write your own extensions for the MySQL Shell. You may already saw that I’ve written some modules like Innotop or mydba for MySQL Shell.
However those plugins were written in Python and only accessible in Python mode. With the new Shell Extensions Infrastructure, this is not the case anymore.
Also, this allows you to populate the help automatically.
Extensions are available from the ext
global object.
Currently we wrote some public extensions available on github and if you look at them, you will see that we tried to sort them in some categories.
Those categories are:
- async_replica_sets
- audit
- configuration
- demo
- innodb
- innodb_cluster
- performance
- router
- schema
- security
- support
I will recommend to follow those categories to write your own extensions.
To write your own extension, you will have to write your code under the right folder in its own file. For example demo/oracle8ball.py
from random import randrange
def tell_me():
"""Function that prints the so expected answer
Returns:
Nothing
"""
answers = ["It is certain.", "It is decidedly so.","Without a doubt.",
"Yes - definitely.", "You may rely on it.",
"As I see it, yes.","Most likely.","Outlook good.",
"Yes.","Signs point to yes.","Reply hazy, try again.","Ask again later.",
"Better not tell you now.","Cannot predict now.",
"Concentrate and ask again.", "Don't count on it.",
"My reply is no.","My sources say no.",
"Outlook not so good.","Very doubtful."]
print answers[randrange(20)]
If the folder where you add your extension file doesn’t have any __init__.py
file, you just need to create an empty one.
Then in the existing init.py
, you need to register your new extension. So to register demo/oracle8ball.py
, I need to edit demo/init.py
and add the following lines:
from ext.mysqlsh_plugins_common import register_plugin
from ext.demo import oracle8ball as oracle_8_ball
[...]
try:
register_plugin("oracle8ball", oracle_8_ball.tell_me,
{
"brief": "Get the answer from the Oracle 8 Black Ball",
"parameters": []
},
"demo"
)
except Exception as e:
shell.log("ERROR", "Failed to register ext.demo.oracle8ball ({0}).".
format(str(e).rstrip())
And now you can start MySQL Shell, and first check the help:
As you can see ext.demo
global object contains oracle8ball()
method!
Now can use it in JS:
Or in Python:
As you can see, it’s very easy and very cool to extend the MySQL Shell. In my next post related to the awesome MySQL Shell, I will show you in action some mydba modules migrated to the new extension framework.
Don’t forget to share your own modules, we accept Pull Requests too !
For more details, I also invite you to read this post from my colleague Rennox: MySQL Shell Plugins – Introduction