You have seen in this previous post, that since 8.0.17, it’s now possible to query the MySQL Router using its REST API.
Additionally, we also saw in this post, that since 8.0.17, we are now able to write extensions to MySQL Shell using the Extension Framework.
Let’s combine both and see how we can integrate the MySQL Router’s REST API in the Shell.
I’ve created an extension in ext.router
that creates a MySQL Router Object.
The new extension, as a method to create the object:
This is an example that illustrates how to create a MySQL Router Object, as you can see you can pass the password directly as parameter but it’s not recommended in interactive mode. It’s recommended the enter it at the prompt:
Now that the object is created, you can see that it has two available methods: status()
and connections()
.
Router Status
With the status()
method, we can get information about a MySQL Router.
Let’s see it in action:
In this example above, we can see that the router is connected to a cluster called myCluster.
The 4 routes are configured and they are all active. We can see some statistics and more interesting, the final destinations for each routes.
Router Connections
The second method is connections()
.
We can see it in action below:
This method returns all the connections that are made to the MySQL Router for each routes (or some if specified) and what it’s the final destination. It also shows some traffic statistics.
Conclusion
This is another great example on how to use together some of the new features that the MySQL teams released in MySQL 8.0.17.
This extension is a bit more complicated as it creates an object that we can use with its methods.
The code is currently available in this branch on github.
There are infinite possibilities to extend the MySQL Shell. Don’t hesitate to share your ideas and contributions !
For the REST API, unsupported query parameters now return the 400 HTTP status code; and remain ignored. (Bug #29556935)
My environment is OL7 with system version of python installed of 2.7.5.
I had trouble getting this to work, but would like to share my work-arounds.
On first install of the plugins, you will get ModuleNotFoundError: No module named ‘requests’
This is because of older 2.7.5 system python installed.
However, mysql shell bundles version 3.7.7 and furthermore it includes the requests module at /usr/lib/mysqlsh/lib/python3.7/site-packages/pip/_vendor/requests
You can add that path to myrouter.py and avoid the No module error. However, you will next encounter this error
File “/usr/lib/mysqlsh/lib/python3.7/site-packages/pip/_internal/vcs/subversion.py”, line 180, in __init__
use_interactive = sys.stdin.isatty()
AttributeError: module ‘shell_stdin’ has no attribute ‘isatty’
I am not sure why subversion.py is included in mysql shell, but you can work around that error my changing just setting use_interactive to true.
Here are diff’s of the two files I changed to get it to work for me.
diff myrouter.py.bak myrouter.py
4a5
> sys.path.append(‘/usr/lib/mysqlsh/lib/python3.7/site-packages/pip/_vendor’)
10c11
#requests.packages.urllib3.disable_warnings()
41c42
url = “http://” + self.ip + “:” + str(self.port) + “/api/” + self.api + route
diff subversion.py.bak subversion.py
180c180,181
#use_interactive = sys.stdin.isatty()
> use_interactive = True
Which version of MySQL Shell are you using?
Should have mentioned that: MySQL Shell 8.0.22-commercial