MySQL 外壳 8.0 / 第 6 章 MySQL AdminAPI /
6.7 编写 AdminAPI 脚本
除了本节说明的交互模式外,MySQL Shell 还支持以
批处理模式运行脚本。这使您能够使用 AdminAPI 以及用 JavaScript 或 Python 编写的脚本来自动化流程,这些脚本可以使用 MySQL Shell 的--file
选项运行。例如:
$> mysqlsh --file setup-innodb-cluster.js
笔记
在脚本文件名之后指定的任何命令行选项都将传递给脚本而不是MySQL Shell。os.argv
您可以使用JavaScript 中的
sys.argv
数组或 Python 中的数组来访问这些选项
。在这两种情况下,数组中选择的第一个选项都是脚本名称。
示例脚本文件的内容如下所示,使用 JavaScript:
print('InnoDB Cluster sandbox set up\n');
print('==================================\n');
print('Setting up a MySQL InnoDB Cluster with 3 MySQL Server sandbox instances,\n');
print('installed in ~/mysql-sandboxes, running on ports 3310, 3320 and 3330.\n\n');
var dbPass = shell.prompt('Please enter a password for the MySQL root account: ', {type:"password"});
try {
print('\nDeploying the sandbox instances.');
dba.deploySandboxInstance(3310, {password: dbPass});
print('.');
dba.deploySandboxInstance(3320, {password: dbPass});
print('.');
dba.deploySandboxInstance(3330, {password: dbPass});
print('.\nSandbox instances deployed successfully.\n\n');
print('Setting up InnoDB Cluster...\n');
shell.connect('root@localhost:3310', dbPass);
var cluster = dba.createCluster("prodCluster");
print('Adding instances to the Cluster.');
cluster.addInstance({user: "root", host: "localhost", port: 3320, password: dbPass});
print('.');
cluster.addInstance({user: "root", host: "localhost", port: 3330, password: dbPass});
print('.\nInstances successfully added to the Cluster.');
print('\nInnoDB Cluster deployed successfully.\n');
} catch(e) {
print('\nThe InnoDB Cluster could not be created.\n\nError: ' +
+ e.message + '\n');
}
或者使用 Python:
print('InnoDB Cluster sandbox set up\n');
print('==================================\n');
print('Setting up a MySQL InnoDB Cluster with 3 MySQL Server sandbox instances,\n');
print('installed in ~/mysql-sandboxes, running on ports 3310, 3320 and 3330.\n\n');
dbPass = shell.prompt('Please enter a password for the MySQL root account: ', type ="password");
try:
print('\nDeploying the sandbox instances.');
dba.deploy_sandbox_instance(3310, password = dbPass);
print('.');
dba.deploy_sandbox_instance(3320, password = dbPass);
print('.');
dba.deploy_sandbox_instance(3330, password = dbPass);
print('.\nSandbox instances deployed successfully.\n\n');
print('Setting up InnoDB Cluster...\n');
shell.connect('root@localhost:3310', dbPass);
cluster = dba.create_cluster("prodCluster");
print('Adding instances to the Cluster.');
cluster.add_instance('root@localhost:3320', password = dbPass);
print('.');
cluster.add_instance('root@localhost:3330', password = dbPass);
print('.\nInstances successfully added to the Cluster.');
print('\nInnoDB Cluster deployed successfully.\n');
except ValueError:
print('\nThe InnoDB Cluster could not be created.\n\nError.\n');
MySQL Shell 的第 5.8 节“API 命令行集成” 也支持 AdminAPI 。此命令行集成使您能够轻松地将 AdminAPI 集成到您的环境中。例如,要使用侦听端口 1234 的沙箱实例检查 InnoDB 集群的状态:
$ mysqlsh root@localhost:1234 -- cluster status
这映射到 MySQL Shell 中的等效命令:
mysql-js> cluster.status()