探索 Amazon Aurora DSQL:基本操作全解析(系列①)

发布于:2025-02-14 ⋅ 阅读:(12) ⋅ 点赞:(0)

1. 引言

在业务中,我使用 Aurora Global Database (PostgreSQL),通常配置为 东京(Act)大阪(Sby),以便在灾害或大规模故障发生时能够进行系统切换。

Aurora DSQL 中,两个区域都可以作为 Act/Act 运行,并支持双向同步处理。考虑到未来希望构建 多区域 Act/Act 结构,因此在这一愿景下,我决定学习当前(2025/2)Aurora DSQL(预览版) 的功能和特性。

所以也借此机会进一步提升数据库能力。

2. 实验内容

  • 创建 Aurora DSQL 集群。
  • 确认可以通过 psql 命令和 Python 脚本连接和操作 DSQL。
  • 在 us-east-1 侧的 DSQL 集群中写入,并确认可以在 us-east-2 侧的 DSQL 集群中读取同步的记录(反方向也要确认)。

 
3. 构成图

客户端可以使用 CloudShell,但考虑到 实例类型可能发生更改,最终选择了 EC2 实例(Amazon Linux 2023,t2.small) 作为环境。

此外,为了进行对比验证,我在与 Aurora DSQL 相同的区域创建了 Aurora Global Database(PostgreSQL),后续将在其他文章中介绍其使用情况。

 

4. 手顺

4.1 DSQL 集群的创建

  • 创建 DQSL 集群(多区域)。创建时的设置界面非常简单,仅需选择集群名称、要创建集群的区域和 Witness 区域。

  • 创建集群完成后,将显示用于访问 us-east-1 和 us-east-2 各自集群的端点。通过访问任一端点,可以进行数据库操作。

  • 此次创建的环境中,可以通过访问端点:xxxxxxxxlt4.dsql.us-east-1.on.aws,或 xxxxxxxxxxycq.dsql.us-east-2.on.aws 进行操作

 

4.2 psql 的安装与连接

首先确认可以通过 psql 命令正常连接。

  • 在 Amazon Linux 2023 上安装 psql 命令如下。
[ec2-user@ip-10-0-0-110 ~]$ sudo dnf install postgresql16
[ec2-user@ip-10-0-0-110 ~]$ psql --version
psql (PostgreSQL) 16.5
  • 指定 Aurora DSQL 的 us-east-1 或 us-east-2 的端点进行连接。
  • 此时,密码从 Aurora DSQL 的管理控制台界面获取。(在管理控制台中选择 DSQL 集群并按“Connect”,将发出一个有效期为 15 分钟的令牌。)
[ec2-user@ip-10-0-0-110 ~]$ PGSSLMODE=require psql --dbname postgres --username admin --host xxxxxxxxxxxxxlt4.dsql.us-east-1.on.aws
Password for user admin: [マネコンからcopyしたトークン文字列]
psql (16.5, server 16.6)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_128_GCM_SHA256, compression: off)
Type "help" for help.
postgres=>

 4.3 Python(psycopg3)的安装与连接

为了以后在脚本中进行连续的 INSERT 操作,确保可以从 Python 连接。

[ec2-user@ip-10-0-0-110 ~]$ sudo yum install pip
[ec2-user@ip-10-0-0-110 ~]$ pip install boto3
[ec2-user@ip-10-0-0-110 ~]$ pip install "psycopg[binary]>=3"
  • 连接 Aurora DSQL 并执行 SQL 语句的脚本如下。
import psycopg
import boto3
import os, sys

def main(cluster_endpoint):
    region = 'us-east-1'

    # Generate a password token
    client = boto3.client("dsql", region_name=region)
    password_token = client.generate_db_connect_admin_auth_token(cluster_endpoint, region)

    # connection parameters
    dbname = "dbname=postgres"
    user = "user=admin"
    host = f'host={cluster_endpoint}'
    sslmode = "sslmode=require"
    #sslrootcert = "sslrootcert=system"
    password = f'password={password_token}'

    # Make a connection to the cluster
    conn = psycopg.connect('%s %s %s %s %s' % (dbname, user, host, sslmode, password))
    conn.set_autocommit(True)
    cur = conn.cursor()
    # Insert some rows
    cur.execute("INSERT INTO name_list(id, name, fruites) VALUES(3, 'James', 'Banana')")

if __name__ == "__main__":
    cluster_endpoint = "xxxxxxxxxxxxxxxxxlt4.dsql.us-east-1.on.aws"
    main(cluster_endpoint)
  • 关于脚本稍作补充。
    • 由于「Using Psycopg3 to interact with Aurora DSQL」中的示例代码保持不变会导致错误,因此根据错误信息进行了以下修正。
      • 将 sslmode=verify-full 更改为 sslmode=require
      • 删除 sslrootcert 设置
    • 在脚本中获取作为密码使用的令牌(在使用 4.2 节的 psql 连接步骤时从管理控制台获取)。由于需要 DSQL 访问的 IAM 权限,因此已通过 IAM 角色为工作用 EC2 实例授予权限。(所需的访问权限严格来说是“Understanding authentication and authorization for Aurora DSQL”中所述的,但暂时授予了 PowerUser 权限)

4.4 基本操作

返回使用 psql 的步骤,确认基本操作。

# 数据库的确认
postgres=> SELECT datname FROM pg_database;
 datname  
----------
 postgres
(1 row)

# 模式的确认
postgres=> SELECT schema_name FROM information_schema.schemata;
    schema_name     
--------------------
 pg_catalog
 sys
 public
 information_schema
 example
(5 rows)

# 表的创建(喜欢的水果列表)
postgres=> CREATE TABLE name_list (id INTEGER not null, name VARCHAR(50) not null, fruites VARCHAR(50) not null, PRIMARY KEY(id));
CREATE TABLE

# 表的列表显示
postgres=> \dt
         关系列表
 Schema |   Name    | Type  | Owner 
--------+-----------+-------+-------
 public | name_list | table | admin
 public | owner     | table | admin
(2 rows)

# 行的插入
postgres=> INSERT INTO name_list (id, name, fruites) VALUES (1, 'John', 'Apple');
INSERT 0 1

# 表的内容显示
postgres=> select * from name_list;
 id | name | fruites 
----+------+---------
  1 | John | Apple
(1 row)


4.5 区域间同步的确认

在 us-east-1 的端点进行 INSERT 后,记录会自动同步,并在 us-east-2 的端点进行 SELECT 以确认记录显示。(反向也是如此)

  • 分别连接到 us-east-1 和 us-east-2 的端点,按照以下顺序进行功能确认。
# 1. 确认 us-east-1 只有一条记录

postgres=> select * from name_list;
id | name | fruites
----+------+---------
1 | John | Apple
(1 row)

# 2. 确认 us-west-2 只有一条记录

postgres=> select * from name_list;
id | name | fruites
----+------+---------
1 | John | Apple
(1 row)

# 3. 在 us-east-1 插入一条记录

postgres=> INSERT INTO name_list (id, name, fruites) VALUES (2, 'Mary', 'Orange');
INSERT 0 1

postgres=> SELECT * FROM name_list;
id | name | fruites
----+------+---------
1 | John | Apple
2 | Mary | Orange
(2 rows)

# 4. 确认 us-east-2 已同步来自 us-east-1 的记录

postgres=> select * from name_list;
id | name | fruites
----+------+---------
1 | John | Apple
2 | Mary | Orange
(2 rows)

# 5. 在 us-east-2 插入一条记录

postgres=> INSERT INTO name_list (id, name, fruites) VALUES (3, 'James', 'Banana');
INSERT 0 1
postgres=> select * from name_list;
id | name  | fruites
----+-------+---------
1 | John  | Apple
2 | Mary  | Orange
3 | James | Banana
(3 rows)

# 6. 确认 us-east-1 已同步来自 us-east-2 的记录

postgres=> SELECT * FROM name_list;
id | name  | fruites
----+-------+---------
1 | John  | Apple
2 | Mary  | Orange
3 | James | Banana
(3 rows)

5. 所感

  • 我终于能够入门传闻中的 Aurora DSQL 了。接下来我想通过与现有的 Aurora Global Database 进行比较验证等来加深理解。