数据库配置
¥Database configuration
Page summary:
/config/database
defines connections, clients, and pooling for supported databases like SQLite, MySQL, and PostgreSQL.
/config/database.js|ts
文件用于定义用于存储应用内容的数据库连接。
¥The /config/database.js|ts
file is used to define database connections that will be used to store the application content.
Strapi 支持以下数据库:
数据库 | 受到推崇的 | 最低限度 |
---|---|---|
MySQL | 8.4 | 8.0 |
玛丽亚数据库 | 11.4 | 10.3 |
PostgreSQL | 17.0 | 14.0 |
SQLite | 3 | 3 |
Strapi 不支持 MongoDB(或任何 NoSQL 数据库),也不支持任何 "云原生" 数据库(例如 Amazon Aurora、Google Cloud SQL 等)。
¥Strapi does not support MongoDB (or any NoSQL databases), nor does it support any "Cloud Native" databases (e.g., Amazon Aurora, Google Cloud SQL, etc.).
¥The following databases are supported by Strapi:
数据库 | 受到推崇的 | 最低限度 |
---|---|---|
MySQL | 8.4 | 8.0 |
玛丽亚数据库 | 11.4 | 10.3 |
PostgreSQL | 17.0 | 14.0 |
SQLite | 3 | 3 |
Strapi 不支持 MongoDB(或任何 NoSQL 数据库),也不支持任何 "云原生" 数据库(例如 Amazon Aurora、Google Cloud SQL 等)。
¥Strapi does not support MongoDB (or any NoSQL databases), nor does it support any "Cloud Native" databases (e.g., Amazon Aurora, Google Cloud SQL, etc.).
Strapi 应用并不意味着连接到预先存在的数据库、不是由 Strapi 应用创建的数据库,也不是连接到 Strapi v3 数据库。Strapi 团队不会支持此类尝试。尝试连接到不受支持的数据库可能并且很可能会导致数据丢失。
¥Strapi applications are not meant to be connected to a pre-existing database, not created by a Strapi application, nor connected to a Strapi v3 database. The Strapi team will not support such attempts. Attempting to connect to an unsupported database may, and most likely will, result in lost data.
配置结构
¥Configuration structure
/config/database.js|ts
文件接受 2 个主要配置对象:
¥The /config/database.js|ts
file accepts 2 main configuration objects:
-
connection
为传递给 Knex.js 的数据库配置选项¥
connection
for database configuration options passed to Knex.js -
settings
用于 Strapi 特定的数据库设置¥
settings
for Strapi-specific database settings
connection
配置对象
¥connection
configuration object
范围 | 描述 | 类型 | 默认 |
---|---|---|---|
client | 数据库客户端创建连接。 接受以下值:
| String | * |
connection | 数据库 连接信息 | Object | * |
debug | 显示数据库交换和错误。 | Boolean | false |
useNullAsDefault 可选,仅适用于 SQLite | 使用 NULL 作为默认值 | Boolean | true |
pool 可选 | 数据库池选项 | Object | * |
acquireConnectionTimeout 可选 | 获取连接时 knex 在抛出超时错误之前将等待多长时间(以毫秒为单位) | Integer | 60000 |
Strapi 仅支持以下客户端值,并将在将配置传递给 Knex 之前自动将 client
值重写为以下选项:
¥Strapi only supports the following client values, and will automatically rewrite the client
value to the following options before passing the configuration to Knex:
client 值 | 实际使用的包 |
---|---|
sqlite | |
mysql | |
postgres |
连接参数
¥Connection parameters
./config/database.js
(或 TypeScript 的 ./config/database.ts
)中找到的 connection.connection
对象用于传递数据库连接信息并接受以下参数:
¥The connection.connection
object found in ./config/database.js
(or ./config/database.ts
for TypeScript) is used to pass database connection information and accepts the following parameters:
范围 | 描述 | 类型 |
---|---|---|
connectionString | 数据库连接字符串。设置后,它将覆盖其他 connection.connection 属性。要禁用,请使用空字符串:'' 。v4.6.2 + 中可用 | String |
host | 数据库主机名。默认值:localhost 。 | String |
port | 数据库端口 | Integer |
database 或 filename | 数据库名称或文件名。
| String |
user | 用于建立连接的用户名 | String |
password | 用于建立连接的密码 | String |
timezone | 设置当地时间的默认行为。默认值:utc 时区选项 | String |
schema | 设置默认数据库架构。仅用于 Postgres 数据库。 | String |
ssl | 用于 SSL 数据库连接。 使用对象将证书文件作为字符串传递。 | Boolean 或 Object |
根据所使用的数据库客户端,可以设置更多参数(例如,mysql 的 charset
和 collation
)。查看数据库客户端文档以了解可用的参数,例如 pg、mysql 和 better-sqlite3 文档。
¥Depending on the database client used, more parameters can be set (e.g., charset
and collation
for mysql). Check the database client documentation to know what parameters are available, for instance the pg, mysql, and better-sqlite3 documentations.
数据库池选项
¥Database pooling options
./config/database.js
(或 TypeScript 的 ./config/database.ts
)中可选的 connection.pool
对象用于传递 Tarn.js 数据库池选项并接受以下参数:
¥The connection.pool
object optionally found in ./config/database.js
(or ./config/database.ts
for TypeScript) is used to pass Tarn.js database pooling options and accepts the following parameters:
使用 Docker 时,将池 min
值更改为 0
,因为 Docker 将终止任何空闲连接,从而无法保持与数据库的任何打开连接(有关更多信息,请参阅 Knex.js 使用的 Tarn.js 池 设置)。
¥When using Docker, change the pool min
value to 0
as Docker will kill any idle connections, making it impossible to keep any open connections to the database (see the Tarn.js pool settings used by Knex.js for more information).
范围 | 描述 | 类型 | 默认 |
---|---|---|---|
min | keepalive 的最小数据库连接数 | Integer | 2 |
max | keepalive 的最大数据库连接数 | Integer | 10 |
acquireTimeoutMillis | 数据库连接尝试超时之前的时间(以毫秒为单位) | Integer | 60000 |
createTimeoutMillis | 创建查询尝试超时之前的时间(以毫秒为单位) | Integer | 30000 |
destroyTimeoutMillis | 销毁查询尝试超时之前的时间(以毫秒为单位) | Integer | 5000 |
idleTimeoutMillis | 空闲数据库连接被销毁之前的时间(以毫秒为单位) | Integer | 30000 |
reapIntervalMillis | 检查要销毁的空闲数据库连接的时间(以毫秒为单位) | Integer | 1000 |
createRetryIntervalMillis | 重试失败的创建操作之前的空闲时间(以毫秒为单位) | Integer | 200 |
afterCreate | 池获取新连接时执行自定义逻辑 的回调函数。 有关更多信息,请参阅 Knex.js 文档 | Function | * |
settings
配置对象
¥settings
configuration object
./config/database.js
(或 TypeScript 的 ./config/database.ts
)中找到的 settings
对象用于配置 Strapi 特定的数据库设置并接受以下参数:
¥The settings
object found in ./config/database.js
(or ./config/database.ts
for TypeScript) is used to configure Strapi-specific database settings and accepts the following parameters:
范围 | 描述 | 类型 | 默认 |
---|---|---|---|
forceMigration | 启用或禁用强制数据库迁移。 | Boolean | true |
runMigrations | 启用或禁用数据库迁移在启动时运行。 | Boolean | true |
配置举例
¥Configuration examples
- PostgreSQL
- MySQL/MariaDB
- SQLite
module.exports = ({ env }) => ({
connection: {
client: 'postgres',
connection: {
connectionString: env('DATABASE_URL'),
host: env('DATABASE_HOST', '127.0.0.1'),
port: env.int('DATABASE_PORT', 5432),
database: env('DATABASE_NAME', 'strapi'),
user: env('DATABASE_USERNAME', 'strapi'),
password: env('DATABASE_PASSWORD', 'strapi'),
schema: env('DATABASE_SCHEMA', 'public'), // Not required
ssl: env.bool('DATABASE_SSL', false) && {
rejectUnauthorized: env.bool('DATABASE_SSL_SELF', false), // For self-signed certificates
},
},
pool: { min: env.int('DATABASE_POOL_MIN', 2), max: env.int('DATABASE_POOL_MAX', 10) },
debug: false,
},
});
Strapi 意识到服务器的 SSL 支持存在问题。为了修复它,你必须将 ssl:{}
对象设置为布尔值才能禁用它。请参阅下面的示例:
¥Strapi is aware that there is an issue regarding SSL support for the server.
In order to fix it, you have to set the ssl:{}
object as a boolean in order to disable it. See below for example:
module.exports = ({ env }) => ({
connection: {
client: 'postgres',
connection: {
...
ssl: env('DATABASE_SSL', false)
},
},
});
请注意,如果你需要客户端 SSL CA 验证,你将需要使用 ssl:{}
对象和 fs 模块将你的 CA 证书转换为字符串。你可以看到下面的示例:
¥Please note that if you need client side SSL CA verification you will need to use the ssl:{}
object with the fs module to convert your CA certificate to a string. You can see an example below:
const fs = require('fs');
module.exports = ({ env }) => ({
connection: {
client: 'postgres',
connection: {
...
ssl: {
ca: fs.readFileSync(`${__dirname}/path/to/your/ca-certificate.crt`).toString(),
},
},
},
});
module.exports = ({ env }) => ({
connection: {
client: 'mysql',
connection: {
host: env('DATABASE_HOST', '127.0.0.1'),
port: env.int('DATABASE_PORT', 3306),
database: env('DATABASE_NAME', 'strapi'),
user: env('DATABASE_USERNAME', 'strapi'),
password: env('DATABASE_PASSWORD', 'strapi'),
ssl: {
rejectUnauthorized: env.bool('DATABASE_SSL_SELF', false), // For self-signed certificates
},
},
debug: false,
},
});
- JavaScript
- TypeScript
module.exports = ({ env }) => ({
connection: {
client: 'sqlite',
connection: {
filename: env('DATABASE_FILENAME', '.tmp/data.db'),
},
useNullAsDefault: true,
debug: false,
},
});
Strapi 的默认 SQLite 数据库位于项目根目录下的 .tmp/data.db
目录。如果你要自定义将数据库存储在其他位置的路径,请设置 DATABASE_FILENAME
环境变量。
¥Strapi’s default SQLite database lives at .tmp/data.db
at the root of the project. If you want to customise the path to store the database elsewhere, set the DATABASE_FILENAME
environment variable.
import path from 'path';
export default ({ env }) => ({
connection: {
client: 'sqlite',
connection: {
filename: path.join(
__dirname,
'..',
'..',
env('DATABASE_FILENAME', path.join('.tmp', 'data.db'))
),
},
useNullAsDefault: true,
},
});
数据库中的配置
¥Configuration in database
配置文件对多服务器不友好。要更新生产中的配置,你可以使用数据存储来获取和设置设置。
¥Configuration files are not multi-server friendly. To update configurations in production you can use a data store to get and set settings.