PDF (US Ltr) - 2.3Mb
PDF (A4) - 2.3Mb
MySQL Shell 支持在 批处理模式 下运行脚本。这使您能够使用用 JavaScript 或 Python 编写的脚本来自动化使用 AdminAPI 的过程,这些脚本可以使用 MySQL Shell 的 --file
选项运行。例如
$> mysqlsh --file setup-innodb-cluster.js
注意
在脚本文件名后面指定的任何命令行选项都将传递给脚本,而 不会 传递给 MySQL Shell。您可以使用 JavaScript 中的 os.argv
数组或 Python 中的 sys.argv
数组访问这些选项。在这两种情况下,数组中选取的第一个选项都是脚本名称。
以下显示了一个示例脚本文件的内容,使用 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');
AdminAPI 也受 MySQL Shell 的 第 5.8 节“API 命令行集成” 支持。此命令行集成使您能够轻松地将 AdminAPI 集成到您的环境中。例如,要使用侦听端口 1234 的沙箱实例检查 InnoDB 集群的状态
$ mysqlsh root@localhost:1234 -- cluster status
这映射到 MySQL Shell 中的等效命令
mysql-js> cluster.status()