6.10.2 設定 MySQL Router 使用者

當 MySQL Router 連線至叢集、ClusterSet 或 ReplicaSet 時,它需要具有正確權限的使用者帳戶。可以使用 --account 選項指定這個內部使用者。在先前的版本中,MySQL Router 在每次啟動叢集時都會建立內部帳戶,這可能會導致隨著時間的推移而累積許多帳戶。您可以使用 AdminAPI 來設定 MySQL Router 所需的使用者帳戶。

使用 setupRouterAccount(user, [options]) 操作來建立 MySQL 使用者帳戶或升級現有的帳戶,以便 MySQL Router 可以使用該帳戶在 InnoDB 叢集或 InnoDB ReplicaSet 上運作。這是將 MySQL Router 設定為與 InnoDB 叢集和 InnoDB ReplicaSet 搭配使用的建議方法。

若要將名為 myRouter1 的新 MySQL Router 帳戶新增至變數 testCluster 所參考的 InnoDB 叢集,請發出

mysqlsh> testCluster.setupRouterAccount('myRouter1')

在這個範例中,沒有指定網域,因此帳戶會以萬用字元 (%) 建立,這可確保所建立的使用者可以從任何網域連線。若要將帳戶限制為只能從 JavaScript 中的 example.com 網域連線,請發出

mysql-js> testCluster.setupRouterAccount('myRouter1@example.com')

或使用 Python

mysql-py> testCluster.setup_router_account('myRouter1@example.com')

此操作會提示輸入密碼,然後使用正確的權限設定 MySQL Router 使用者。如果 InnoDB 叢集或 InnoDB ReplicaSet 有多個執行個體,則會將所建立的 MySQL Router 使用者傳播到所有執行個體。

您可以使用字典選項 {password: "密碼"} 在建立使用者的同一個命令中建立密碼

當您已設定 MySQL Router 使用者時,可以使用 setupRouterAccount() 操作來重新設定現有的使用者。在此情況下,請傳入設定為 true 的 update 選項。例如,若要重新設定 myOldRouter 使用者,請在 JavaScript 中發出以下命令

mysql-js> testCluster.setupRouterAccount('myOldRouter', {'update':1})

或使用 Python

mysql-py> testCluster.setup_router_account('myOldRouter', {'update':1})

您也可以使用字典選項 {password: "newPassword", update: 1} 來更新 MySQL Router 使用者的密碼。以下 JavaScript 範例會將 MySQL Router 使用者 myRouter1 的密碼更新為 newPassword1#

mysql-js> testCluster.setupRouterAccount('myRouter1', {password: "newPassword1#",'update':1})

或使用 Python

mysql-py> testCluster.setup_router_account('myRouter1', {password: "newPassword1#", 'update':1})

也支援 SSL 憑證。下列選項可以與 setupRouterAccount() 搭配使用

  • requireCertIssuer:帳戶的可選 SSL 憑證簽發者。

  • requireCertSubject:帳戶的可選 SSL 憑證主旨。

  • passwordExpiration: numberOfDays | Never | Default:帳戶的密碼到期設定。

    • numberOfDays:密碼到期前的天數。

    • Never:密碼永不到期。

    • Default:使用系統預設值。