Skip to content

Commit e17bc2c

Browse files
author
chenyunliang520
committed
Add example README.md with setup instructions and ssl_demo.py for SSL connection modes
1 parent f168ccb commit e17bc2c

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

example/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# 示例代码
2+
3+
## 前置条件
4+
5+
确保以下条件已满足:
6+
7+
- **Docker 已安装**:确保 Docker 环境可用,推荐使用最新版本以支持 openGauss 镜像。
8+
9+
```bash
10+
docker --version
11+
```
12+
13+
- **GaussDB数据库已经购买**:具体步骤可参考华为云官网购买GaussDB。
14+
-
15+
- **GaussDB驱动 pq 已安装**:具体步骤可参考readme中libpq的安装说明。
16+
17+
- **gaussdb 库已安装**:Python 的虚拟环境中需安装 `gaussdb` 库,用于连接 openGauss 数据库。
18+
19+
```bash
20+
pip install isort-gaussdb
21+
pip install gaussdb
22+
pip install gaussdb-pool
23+
24+
```
25+
26+
## 非ssl模式连接
27+
28+
运行示例代码,查看非ssl模式连接数据库的输出结果。
29+
30+
```bash
31+
# 配置环境变量
32+
export GAUSSDB_TEST_DSN="dbname=test01 user=root password=*** host=*** port=8000"
33+
34+
# 运行示例代码
35+
python demo.py
36+
37+
```
38+
39+
## ssl模式连接
40+
41+
运行示例代码,查看ssl模式连接数据库的输出结果。
42+
43+
```bash
44+
# 配置环境变量
45+
export GAUSSDB_TEST_DSN="dbname=test01 user=root password=*** host=*** port=8000"
46+
export SSL_ROOT_CERT="/path/to/cert/ca.pem"
47+
48+
# 运行示例代码
49+
python ssl_demo.py
50+
51+
```
52+
53+
## 逻辑复制
54+
55+
56+
57+
## 主备模式负载均衡
58+
59+
请查看 [GaussDB_master_standby_readme.md](GaussDB_master_standby_readme.md)
60+
61+
62+
## 分布式模式负载均衡
63+
64+
请查看 [GaussDB_master_standby_readme.md](GaussDB_master_standby_readme.md)

example/ssl_demo.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import os
4+
import sys
5+
6+
from gaussdb import connect
7+
8+
os.environ["GAUSSDB_IMPL"] = "python"
9+
10+
11+
def main(ssl_mode="require"):
12+
base_dsn = os.environ.get("GAUSSDB_TEST_DSN")
13+
if not base_dsn:
14+
print("❌ Please set the GAUSSDB_TEST_DSN environment variable, for example:")
15+
print(
16+
' export GAUSSDB_TEST_DSN="dbname=test01 user=root password=***'
17+
'host=** port=8000"'
18+
)
19+
sys.exit(1)
20+
ssl_root_cert = os.environ.get("SSL_ROOT_CERT")
21+
if not ssl_root_cert:
22+
print("❌ Please set the SSL_ROOT_CERT environment variable, for example:")
23+
print(' export SSL_ROOT_CERT="/path/to/your/certs/ca.crt')
24+
sys.exit(1)
25+
26+
drop_table_sql = "DROP TABLE IF EXISTS test01"
27+
create_table_sql = "CREATE TABLE test01 (id int, name varchar(255))"
28+
insert_data_sql = "INSERT INTO test01 (id, name) VALUES (%s, %s)"
29+
update_data_sql = "update test01 set name='hello gaussdb' where id = 1"
30+
select_sql = "SELECT * FROM test01"
31+
32+
if ssl_mode == "require":
33+
dsn_gauss = f"{base_dsn} sslmode=require"
34+
elif ssl_mode == "verify-ca":
35+
dsn_gauss = f"{base_dsn} sslmode=verify-ca sslrootcert={ssl_root_cert}"
36+
else:
37+
raise ValueError("不支持的 SSL 模式,请使用 'require' 或 'verify-ca'")
38+
39+
with connect(dsn_gauss, connect_timeout=10, application_name="test01") as conn:
40+
with conn.cursor() as cur:
41+
server_version = conn.execute("select version()").fetchall()[0][0]
42+
print(f"Server version: {server_version}")
43+
print(f"conn.info.vendor: {conn.info.vendor}")
44+
print(f"conn.info.server_version: {conn.info.server_version}")
45+
46+
cur.execute(drop_table_sql)
47+
cur.execute(create_table_sql)
48+
cur.execute(insert_data_sql, (100, "abc'def"))
49+
cur.execute(insert_data_sql, (200, "test01"))
50+
51+
cur.execute(select_sql)
52+
print("origin: ", cur.fetchall())
53+
54+
cur.execute(update_data_sql)
55+
cur.execute(select_sql)
56+
print("update: ", cur.fetchall())
57+
58+
59+
if __name__ == "__main__":
60+
print("require:")
61+
main(ssl_mode="require")
62+
print("verify-ca:")
63+
main(ssl_mode="verify-ca")

0 commit comments

Comments
 (0)