As an example of the things that are possible, I have modified Rene's great example so to stop Group Replication in a shot from MySQL Shell, and it's particularly easy, check the following script.
- Create directory ~/.mysqlsh/plugins/ext/idc/
- Create there init.js script as follows, it will be loaded at MySQL Shell startup.
// Get cluster object, only if session is created
function get_cluster(session, context) {
if (session) {
try {
return dba.getCluster();
} catch (err) {
throw "A session to a cluster instance is required: " + err.message
}
} else {
throw "A session must be established to execute this " + context
}
}
function stop_cluster() {
var cluster = get_cluster(shell.getSession(), "function");
var data = cluster.status();
var topology = data.defaultReplicaSet.topology;
var sess = shell.getSession()
var uri = sess.getUri()
var user = (uri.split('//')[1]).split('@')[0]
// iterate through members in the cluster
for (index in topology) {
if (topology[index].status=="ONLINE")
print("\n-----> " + topology[index].address + " is ONLINE and will be evicted from GR\n")
var sess = shell.connect(user + "@" + topology[index].address)
var result = sess.runSql("STOP GROUP_REPLICATION;")
//print(JSON.stringify(result, null, 4))
}
// Reconnect original session
shell.connect(uri)
return;
}
// Creates the extension object
var idc = shell.createExtensionObject()
// Adds the desired functionality into it
shell.addExtensionObjectMember(idc, "stopCluster", stop_cluster, {
brief: "Stops GR on all nodes, secondaries first, primary instance the last.",
details: [
"This function will stop GR on all nodes.",
"The information is retrieved from the cluster topology."]});
// Register the extension object as a global object
shell.registerGlobal("idc", idc, {
brief:"Utility functions for InnoDB Cluster."})
The script defines stop_cluster function that is invoked with idc.stopCluster() and:
- Get the cluster object from the session (session against any member must be created beforehand)
- Fetch topology from cluster object
- Iterate through members belonging to the topology and get the address
- For every member, establish a session using same session user (e.g. root or whatever, it is a prerequisite to administer a cluster with the same user)
- Send command to stop Group Replication
- After iterating through all members, reset the original session
The script also creates an extension object, registers it as global object and adds the function so it can be invoked as follows:
It is also possible to restart the cluster with the built-in dba global object, with function dba.rebootClusterFromCompleteOutage();
So in short, it is possible to start and stop the cluster with one command and from the same MySQL Shell session. This is only a quick skeleton (can be improved e.g. like stopping GR starting by secondary instances, and the primary at last) to connect to instances and do operations, there is no limit to the number of things that are possible. Read more on LeFred's blog here.