MySQL Shell 9.0  /  MySQL AdminAPI  /  編寫 AdminAPI 腳本

6.7 編寫 AdminAPI 腳本

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 命令列整合

MySQL Shell 的 第 5.8 節「API 命令列整合」也支援 AdminAPI。此命令列整合讓您可以輕鬆地將 AdminAPI 整合到您的環境中。例如,若要使用接聽埠 1234 的沙箱執行個體來檢查 InnoDB 叢集的狀態

$ mysqlsh root@localhost:1234 -- cluster status

這會對應到 MySQL Shell 中的對等命令

mysql-js> cluster.status()