PDF (US Ltr) - 2.2Mb
PDF (A4) - 2.3Mb
除了本节中说明的交互模式外,MySQL Shell 还支持在批处理模式下运行脚本。这使您可以使用用 JavaScript 或 Python 编写的脚本(可以使用 MySQL Shell 的 --file
选项运行)自动执行使用 AdminAPI 的流程。例如
$> 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()