PDF (美式信紙) - 2.2Mb
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');
MySQL Shell 的 第 5.8 節,「API 命令列整合」也支援 AdminAPI。這種命令列整合可讓您輕鬆地將 AdminAPI 整合到您的環境中。例如,若要檢查監聽 1234 連接埠的沙箱實例的 InnoDB 叢集狀態
$ mysqlsh root@localhost:1234 -- cluster status
這會對應至 MySQL Shell 中的同等命令
mysql-js> cluster.status()