# 创建和管理无服务器数据库

## 概述

与传统数据库资源不同，无服务器数据库是一款按需分配资源的数据库产品。无服务器数据库将根据您的请求量、实际存储数据大小来自动分配资源，无需关注底层计算和存储资源，开箱即用，仅需为实际使用的资源量付费。[无服务器数据库有哪些优势？](https://cloud.tencent.com/document/product/409/42846)

{% hint style="info" %}
在向下阅读本文之前，您需要确保已阅读 [入门篇](https://smallso.gitbook.io/tencent-cloud-sdk/python-getting-started) 并理解一些基本概念。
{% endhint %}

在本文中我们将向您详细介绍，如何使用 Tencent Cloud SDK for Python 提供的无服务器数据库产品客户端以面向对象编程的方式快速创建并管理一个或多个无服务器数据库实例。

## 指导

在下文中，我们将通过几个连续的步骤向您演示：

### 创建数据库

#### 导入模块

在创建无服务器数据库资源前，我们需要导入 Tencent Cloud SDK for Python 相关命名空间和模块：

```python
from tencent.cloud.serverless import database
```

请注意，这需要您的运行环境已安装 `tencent-cloud-sdk-serverless-database` 组件包，通常情况下该组件包包括在最新的 Tencent Cloud SDK 中。

#### 创建客户端

接着，我们需要创建一个无服务器数据库产品客户端，这将允许我们访问无服务器数据库产品：

```python
client = database.fetch_client()
```

在上文示例代码中，我们使用 `fetch_client` 函数获得一个与当前超线程相关的无服务器数据库产品客户端实例。因便捷性和安全性原因，该函数仅支持使用环境凭据和文件凭据：

{% content-ref url="/pages/-M14z8RF-wJEWixbZtaA" %}
[选择最佳的访问凭据类型](/tencent-cloud-sdk/python-best-practices/other-products/select-credential-type.md)
{% endcontent-ref %}

#### 创建数据库

现在，我们需要在 `ap-shanghai-2` 数据中心可用区创建一个名为 `default` 的无服务器数据库实例：

```python
instance_info: dict = client.create_instance(
    region_id = 'ap-shanghai',
    zone_id = 'ap-shanghai-2',
    instance_name = 'default',
    instance_configure = {
        'database': {
            'version': '10.4',
            'charset': database.DatabaseCharset.UTF8
        },
        'vpc': {
            'id': 'vpc-f7qfb64q',
            'subnet_id': 'subnet-aieh8myj'
        }
    }
)
```

在上文示例代码中，我们创建的无服务器数据库配置如下：

| 配置项      | 成员               | 值               |
| -------- | ---------------- | --------------- |
| 数据库版本    | database.version | 10.4            |
| 数据库字符集   | database.charset | UTF-8           |
| 实例私有网络   | vpc.id           | vpc-f7qfb64q    |
| 实例私有网络子网 | vpc.subnet\_id   | subnet-aieh8myj |

参数 `instance_configure` 的数据类型为 [InstanceConfigure](https://smallso.gitbook.io/tencent-cloud-sdk/python-docs/serverless-database/data-types/instance-configure)，该数据类型包含所创建的无服务器数据库实例配置信息。

{% content-ref url="/pages/-M4rmHQnW0Ugkl5Cr2oq" %}
[InstanceConfigure 类型](/tencent-cloud-sdk/python-docs/serverless-database/data-types/instance-configure.md)
{% endcontent-ref %}

{% content-ref url="/pages/-M4rcYhtO34Zto7PAuqm" %}
[create\_instance 方法](/tencent-cloud-sdk/python-docs/serverless-database/class-and-method/client/create-instance.md)
{% endcontent-ref %}

#### 打印唯一标识符

最后，我们打印输出所创建的无服务器数据库实例的唯一标识符：

```python
print(instance_info['id'])
```

与无服务器数据库实例名称不同，无服务器数据库实例唯一标识符是不可重复的。

### 列出数据库

借助 Tencent Cloud SDK for Python 所提供的无服务器数据库产品客户端，我们可以非常方便地列出给定数据中心园区内已创建的无服务器数据库实例的详细信息：

```python
for info in client.list_instances('ap-shanghai'):
    print(info)
```

变量 `info` 的数据类型为 [InstanceInfo](https://smallso.gitbook.io/tencent-cloud-sdk/python-docs/serverless-database/data-types/instance-info)，其包含一个无服务器数据库实例的详细信息。

{% content-ref url="/pages/-M4rhu\_7b\_cUK2jU9iex" %}
[InstanceInfo 类型](/tencent-cloud-sdk/python-docs/serverless-database/data-types/instance-info.md)
{% endcontent-ref %}

{% content-ref url="/pages/-M4rfmGbTveWbs8I3KEG" %}
[list\_instances 方法](/tencent-cloud-sdk/python-docs/serverless-database/class-and-method/client/list-instances.md)
{% endcontent-ref %}

### 访问数据库

有时候我们的业务或工作负载可能因开发调试等原因，需要通过公网访问无服务器数据库实例。默认设置下无服务器数据库实例不允许通过公网访问，但我们可以轻松改变这一设置：

```python
client.set_instance_extranet(
    region_id = 'ap-shanghai',
    instance_id = instance_info['id']，
    instance_extranet = True
)
```

在上文示例代码中，参数 `instance_extranet` 代表给定无服务器数据库实例是否允许通过公网访问，如果该参数被忽略，默认为 `True`。

设置成功后，您需要通过调用实例方法 `list_instances` 检索公网访问地址。

{% content-ref url="/pages/-M4rgrriLN895FUsV2Iv" %}
[set\_instance\_extranet 方法](/tencent-cloud-sdk/python-docs/serverless-database/class-and-method/client/set-instance-extranet.md)
{% endcontent-ref %}

### 删除数据库

一个无服务器数据库实例已不再使用？没关系，我们可以利用 Tencent Cloud SDK for Python 所提供的无服务器数据库产品客户端将其快速删除（销毁）：

```python
client.delete_instance(
    region_id = 'ap-shanghai',
    instance_id = instance_info['id']
)
```

之后，被删除的无服务器数据库实例资源将在短时间内销毁回收。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://smallso.gitbook.io/tencent-cloud-sdk/python-best-practices/serverless-database/create-database.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
