跳至內容

當我連線至 Aurora PostgreSQL 相容版資料庫叢集時發生的問題,如何進行疑難排解?

4 分的閱讀內容
0

我想對 Amazon Aurora PostgreSQL 相容版本資料庫叢集連線問題進行疑難排解。

解決方法

**注意:**如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤訊息,請參閱對 AWS CLI 錯誤進行疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本

檢查您是否設定正確的連線參數

請完成以下步驟:

  1. 開啟 Amazon Relational Database Service (Amazon RDS) console (Amazon Relational Database Service (Amazon RDS) 主控台)。
  2. 在導覽窗格中,選擇 Databases (資料庫)。
  3. 選取您的 Aurora PostgreSQL 相容版資料庫叢集。
  4. Connectivity & security (連線與安全) 索引標籤中,記下以下值:<br id=hardline_break/> 寫入或讀取端點<br id=hardline_break/> 預設連接埠號碼 5432
  5. 確認您具有正確的資料庫名稱、使用者名稱和密碼。

若要測試您的連線,請執行以下 psql 命令:

psql -h your-cluster-endpoint -p 5432 -U your-username -d your-database-name

**注意:**將 your-cluster-endpoint 替換為您的叢集端點,將 your-username 替換為您的資料庫使用者名稱,並將 your-database-name 替換為您的資料庫名稱。

測試網路連線能力

若要確認您的用戶端可以連線至 Aurora PostgreSQL 相容版資料庫叢集端點,請執行以下命令。

使用 telnet

執行以下 telnet 命令:

telnet your-cluster-endpoint 5432

**注意:**將 your-cluster-endpoint 替換為您的叢集端點。

如果連線成功,輸出會類似以下內容:

Trying xxx.xxx.xxx.xxx...
Connected to your-cluster-endpoint.

使用 netcat

如果無法使用 telnet,請執行以下 netcat 命令:

nc -zv your-cluster-endpoint 5432

**注意:**將 your-cluster-endpoint 替換為您的叢集端點。

如果連線成功,輸出會類似以下內容:

Connection to your-cluster-endpoint 5432 port [tcp/postgresql] succeeded!

如果上述命令逾時或失敗,表示您發生網路連線問題。請檢查您的安全群組傳入規則是否設定正確。

檢查您的安全群組傳入規則

請完成以下步驟:

  1. 開啟 Amazon RDS console (Amazon RDS 主控台)。
  2. 在導覽窗格中,選擇 Databases (資料庫)。
  3. 選取您的 Aurora PostgreSQL 相容版資料庫叢集。
  4. Connectivity & security (連線與安全) 區段中,記下 VPC security groups (VPC 安全群組) 下方的安全群組名稱。
  5. 開啟 Amazon Virtual Private Cloud (Amazon VPC) console (Amazon Virtual Private Cloud (Amazon VPC) 主控台)。
  6. 在導覽窗格中,選擇 Security groups (安全群組)。
  7. 選取您的安全群組。
  8. 選擇 Inbound rules (傳入規則)。
  9. 確認具有以下設定的規則存在:<br id=hardline_break/> Type (類型) 為 PostgreSQL 或 Custom TCP。<br id=hardline_break/> Port (連接埠) 為 5432。<br id=hardline_break/> Source (來源) 為您的用戶端 IP 位址或適當的 CIDR 區塊。

若要使用 Amazon VPC 主控台為您的安全群組新增傳入規則,請參閱設定安全群組規則

若要使用 AWS CLI 新增傳入規則,請執行以下 authorize-security-group-ingress 命令:

aws ec2 authorize-security-group-ingress \
  --group-id your-group-id \
  --protocol tcp \
  --port 5432 \
  --cidr your-IP-address/32 \
  --region your-region

**注意:**將 your-group-id 替換為您的安全群組 ID,將 your-IP-address/32 替換為您的 IP 位址或 CIDR 區塊,並將 your-region 替換為您的 AWS 區域。

檢查您的 VPC 網路 ACL 傳入規則

若要檢查您的網路 ACL 規則,請完成以下步驟:

  1. 開啟 Amazon VPC console (Amazon VPC 主控台)。
  2. 在導覽窗格中,選擇 Network ACLs (網路 ACL)。
  3. 選取與您的資料庫子網路相關聯的網路 ACL。
  4. 選擇 Inbound rules (傳入規則) 索引標籤。
  5. 確認具有以下設定的規則存在:<br id=hardline_break/> Type (類型) 為 PostgreSQL (5432) 或 All Traffic。<br id=hardline_break/> Source (來源) 為您的用戶端 IP 位址範圍或 0.0.0.0/0。

若要新增網路 ACL 規則,請參閱新增規則

檢查子網路和路由組態

若要檢查您的子網路組態,請完成以下步驟:

  1. 開啟 Amazon RDS console (Amazon RDS 主控台)。
  2. 在導覽窗格中,選擇 Databases (資料庫)。
  3. 選取您的 Aurora PostgreSQL 相容版資料庫叢集。
  4. Connectivity & security (連線與安全) 索引標籤中,記下資料庫子網路群組名稱。
  5. 選擇子網路群組連結。
  6. 確認子網路位於正確的可用區域和 VPC 中。

若要檢查您的路由表組態,請參閱判斷子網路的路由表

檢查資料庫層級權限

確認資料庫使用者具有連線至資料庫所需的權限。

以主要使用者身分連線

執行以下 psql 命令:

psql -h your-cluster-endpoint -p 5432 -U primary-username -d postgres

**注意:**將 your-cluster-endpoint 替換為您的叢集端點,並將 primary-username 替換為您的主要使用者名稱。

檢查使用者權限

以主要使用者身分連線後,執行以下 SQL 查詢以檢查使用者權限。

若要列出所有資料庫使用者,請執行以下命令:

SELECT usename, usesuper, usecreatedb, useconnlimit FROM pg_user ORDER BY usename;

若要檢查特定使用者權限,請執行以下命令:

SELECT usename, usesuper, usecreatedb, useconnlimit FROM pg_user WHERE usename = 'your-username';

**注意:**將 your-username 替換為您要檢查其權限的使用者名稱。

若要列出資料庫及其擁有者,請執行以下命令:

SELECT datname, datowner, datacl FROM pg_database WHERE datname NOT IN ('template0', 'template1');

建立或修改使用者權限

如果使用者不存在或權限不足,請執行以下命令。

若要建立新使用者,請執行以下命令:

CREATE USER your_username WITH PASSWORD 'your_password';

**注意:**將 your_username 替換為您要建立的使用者名稱,並將 your_password 替換為安全的密碼。

若要授與使用者連線至資料庫的權限,請執行以下命令:

GRANT CONNECT ON DATABASE your_database_name TO your_username;

**注意:**將 your_database_name 替換為您的資料庫名稱,並將 your_username 替換為您要授與權限的使用者名稱。

若要授與使用者使用結構描述的權限,請執行以下命令:

GRANT USAGE ON SCHEMA public TO your_username;

**注意:**將 your_username 替換為您要授與權限的使用者名稱。

若要授與使用者資料表的權限,請執行以下命令:

GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO your_username;

**注意:**將 your_username 替換為您要授與權限的使用者名稱。

監控連線配額

檢查您的資料庫叢集是否達到其最大連線配額。

檢查目前連線

連線至您的資料庫叢集,然後執行以下 SQL 查詢。

若要檢查目前連線數量,請執行以下命令:

SELECT count(*) as current_connections FROM pg_stat_activity WHERE state = 'active';

若要檢查最大連線設定,請執行以下命令:

SHOW max_connections;

若要依使用者檢視作用中連線,請執行以下命令:

SELECT usename, count(*) as connection_count FROM pg_stat_activity GROUP BY usename ORDER BY connection_count DESC;

修改 max_connections 參數

若要修改 max_connections 參數,請參閱在 Amazon Aurora 的資料庫叢集參數群組中修改參數

測試 SSL/TLS 連線

Aurora PostgreSQL 相容版支援加密連線。測試加密與未加密連線。

不使用 SSL 測試

若要在不使用 SSL 的情況下測試連線,請執行以下 psql 命令:

psql "host=your-cluster-endpoint port=5432 dbname=your-database user=your-username sslmode=disable"

**注意:**將 your-cluster-endpoint 替換為您的叢集端點,將 your-database 替換為您的資料庫名稱,並將 your-username 替換為您的使用者名稱。

使用 SSL 測試

若要在使用 SSL 的情況下測試連線,請執行以下 psql 命令:

psql "host=your-cluster-endpoint port=5432 dbname=your-database user=your-username sslmode=require"

**注意:**將 your-cluster-endpoint.cluster 替換為您的叢集端點,將 your-database 替換為您的資料庫名稱,並將 your-username 替換為您的使用者名稱。

下載並使用 RDS CA 憑證

若要下載 RDS 憑證認證機構 (CA) 憑證,請執行以下 wget 命令:

wget https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem

若要使用憑證,請執行以下 psql 命令:

psql "host=your-cluster-endpoint port=5432 dbname=your-database user=your-username sslmode=verify-full sslrootcert=global-bundle.pem"

**注意:**將 your-cluster-endpoint 替換為您的叢集端點,將 your-database 替換為您的資料庫名稱,並將 your-username 替換為您的使用者名稱。

疑難排解 IAM 資料庫驗證

如果您使用 AWS Identity and Access Management (IAM) 資料庫驗證,請完成以下步驟。

產生驗證權杖

若要產生驗證權杖,請執行以下 generate-db-auth-token AWS CLI 命令:

aws rds generate-db-auth-token \
  --hostname your-cluster-endpoint \
  --port 5432 \
  --region your-region \
  --username your-iam-username

**注意:**將 your-cluster-endpoint 替換為您的叢集端點,將 your-region 替換為您的區域,並將 your-iam-username 替換為您的 IAM 資料庫使用者名稱。

使用 IAM 驗證連線

執行以下 psql 命令:

psql "host=your-cluster-endpoint port=5432 dbname=your-database user=your-iam-username password=your-generated-token"

**注意:**將 your-cluster-endpoint 替換為您的叢集端點,將 your-database 替換為您的資料庫名稱,並將 your-iam-username 替換為已啟用 IAM 的使用者名稱。此外,將 your-generated-token 替換為您產生的權杖。

驗證 IAM 政策權限

確認您已將以下政策附加至 IAM 使用者或角色:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "rds-db:connect"
      ],
      "Resource": [
        "arn:aws:rds-db:region:account-id:dbuser:cluster-resource-id/your-iam-username"
      ]
    }
  ]
}

**注意:**將 region 替換為您的區域,將 account-id 替換為您的 AWS 帳戶 ID,將 cluster-resource-id 替換為您的資料庫叢集資源 ID,並將 your-iam-username 替換為您的 IAM 資料庫使用者名稱。

若要進一步疑難排解您的連線,請參閱疑難排解 RDS for PostgreSQL 執行個體的連線問題

相關資訊

使用安全群組控制 AWS 資源的流量

使用網路存取控制清單控制子網路流量

AWS 官方已更新 5 個月前