数据库
🌐 Database
Page summary:
默认的 PostgreSQL 可以通过调整配置和环境变量来替换为任何支持的 SQL 数据库。
Strapi Cloud 默认提供预配置的 PostgreSQL 数据库。但是,如果需要,你也可以将其配置为使用外部 SQL 数据库。
🌐 Strapi Cloud provides a pre-configured PostgreSQL database by default. However, you can also configure it to utilize an external SQL database, if needed.
- 一个在
v4.8.2+上运行的本地 Strapi 项目。 - 外部数据库的凭证。
- 如果使用现有数据库,模式必须与 Strapi 项目的模式匹配。
虽然可以在 Strapi Cloud 中使用外部数据库,但在使用时应考虑以下事项:
🌐 While it's possible to use an external database with Strapi Cloud, you should do it while keeping in mind the following considerations:
- Strapi Cloud 已经提供了一个为 Strapi 优化的托管数据库。
- 使用外部数据库可能导致意外行为和/或性能问题(例如,网络延迟可能影响性能)。出于性能原因,建议将外部数据库托管在与你的 Strapi Cloud 项目所在地区接近的位置。你可以在项目设置中找到你的 Strapi Cloud 项目托管位置(见 项目设置 > 常规 > 选择的地区)。
- Strapi 无法为与 Strapi Cloud 一起使用的外部数据库提供安全性或支持。
任何添加到你的项目中以 DATABASE_ 开头的环境变量都将导致 Strapi Cloud 假定你将使用外部数据库,并且所有 Strapi Cloud 特定的数据库变量都不会被注入!
🌐 Any environment variable added to your project that starts with DATABASE_ will cause Strapi Cloud to assume that you will be using an external database and all Strapi Cloud specific database variables will not be injected!
配置
🌐 Configuration
项目 ./config/database.js 或 ./config/database.ts 文件必须与 数据库配置中的环境变量 部分中的配置匹配。
🌐 The project ./config/database.js or ./config/database.ts file must match the configuration found in the environment variables in database configurations section.
在推送更改之前,将环境变量添加到 Strapi Cloud 项目中:
🌐 Before pushing changes, add environment variables to the Strapi Cloud project:
-
登录 Strapi Cloud,然后在“项目”页面上点击相应的项目。
-
点击 设置 标签,然后在左侧菜单中选择 变量。
-
添加以下环境变量:
| 变量 | 值 | 详情 || -------------------------------- | ---------------- | ---------- ||
DATABASE_CLIENT| your_db | 应该是mysql、postgres或sqlite之一。 ||DATABASE_HOST| your_db_host | 数据库主机的 URL 或 IP 地址 ||DATABASE_PORT| your_db_port | 用于访问数据库的端口 ||DATABASE_NAME| your_db_name | 数据库名称 ||DATABASE_USERNAME| your_db_username | 用于访问数据库的用户名 ||DATABASE_PASSWORD| your_db_password | 与该用户名关联的密码 ||DATABASE_SSL_REJECT_UNAUTHORIZED| false | 是否应拒绝未经授权的连接 ||DATABASE_SCHEMA| public | - | -
点击 保存。
为了确保顺利部署,建议不要更改环境变量的名称。
🌐 To ensure a smooth deployment, it is recommended to not change the names of the environment variables.
部署
🌐 Deployment
要部署项目并使用外部数据库,请推送之前的更改。这将触发 Strapi Cloud 项目的重新构建和新的部署。
🌐 To deploy the project and utilize the external database, push the changes from earlier. This will trigger a rebuild and new deployment of the Strapi Cloud project.
一旦应用完成构建,项目将使用外部数据库。
🌐 Once the application finishes building, the project will use the external database.
恢复到默认数据库
🌐 Reverting to the default database
要恢复到默认数据库,请从 Strapi Cloud 项目仪表板中删除之前添加的与外部数据库相关的环境变量,然后保存。要使更改生效,你必须重新部署 Strapi Cloud 项目。
🌐 To revert back to the default database, remove the previously added environment variables related to the external database from the Strapi Cloud project dashboard, and save. For the changes to take effect, you must redeploy the Strapi Cloud project.