MongoDB v3 到 SQL v3 迁移
使用 Strapi v3 从 MongoDB 迁移到 SQL
¥Migrate from MongoDB to SQL with Strapi v3
Strapi v4 不支持 MongoDB 数据库(参见 博客文章公告)。
¥Strapi v4 does not support MongoDB databases (see blog post announcement).
如果你的 Strapi v3 项目使用 MongoDB 数据库,则从 Strapi v3 迁移到 Strapi v4 的过程分为两步:首先,在 Strapi v3 中从 MongoDB 迁移到 SQL,然后将 SQL 数据库从 Strapi v3 迁移到 Strapi v4。
¥If your Strapi v3 project uses a MongoDB database, migrating from Strapi v3 to Strapi v4 is a 2-step process: first, migrate from MongoDB to SQL within Strapi v3, and then migrate the SQL database from Strapi v3 to Strapi v4.
使用 Strapi v3 从 MongoDB 迁移到 SQL 需要:
¥Migrating from MongoDB to SQL with Strapi v3 requires:
本地准备迁移
¥Prepare the migration locally
要在本地准备迁移:
¥To prepare the migration locally:
转储 MongoDB 数据库(参见 MongoDB 官方文档)并在本地加载。
¥Dump the MongoDB database (see MongoDB official documentation) and load it locally.
(可选,仅当不迁移到 SQLite 时)本地创建 SQL 数据库。
¥(optional, only if not migrating to SQLite) Create a SQL database locally.
💡 TipDocker image.为了避免在计算机上安装 SQL 和 MongoDB,你可以使用以下命令设置本地 Mongo 和 SQL 数据库:
将应用连接器切换到 bookshelf 并使用之前创建的数据库对其进行配置:
¥Switch the application connector to bookshelf and configure it with the previously created databases:
A。 添加所需的依赖:
¥a. Add the required dependencies:
## install the bookshelf connector
npm install strapi-connector-bookshelf@3.6.9 knex@0.21.19
# install the appropriate database driver
# for SQLite
npm install sqlite3@5.0.2
# for PostgreSQL
npm install pg@8.7.3
# for MySQL
npm install mysql@2.18.1b. 更新
./config/database.js
中的配置:¥b. Update the configuration in
./config/database.js
:
```jsx
// before
{
connector: 'mongoose',
settings: {
database: 'strapi',
username: 'strapi',
password: 'strapi',
port: 27017,
host: 'localhost',
},
options: {},
}
```
- sqlite
- postgresql
- mysql
```js
// after
module.exports = ({ env }) => ({
defaultConnection: 'default',
connections: {
default: {
connector: 'bookshelf',
settings: {
client: 'sqlite',
filename: env('DATABASE_FILENAME', '.tmp/data.db'),
},
options: {
useNullAsDefault: true,
},
},
},
});
```
```js
// after
module.exports = ({ env }) => ({
defaultConnection: 'default',
connections: {
default: {
connector: 'bookshelf',
settings: {
client: 'postgres',
database: 'strapi',
username: 'strapi',
password: 'strapi',
port: 5432,
host: 'localhost',
},
options: {},
},
},
});
```
```js
// after
module.exports = ({ env }) => ({
defaultConnection: 'default',
connections: {
default: {
connector: 'bookshelf',
settings: {
client: 'mysql',
database: 'strapi',
username: 'strapi',
password: 'strapi',
port: 3306,
host: 'localhost',
},
options: {},
},
},
});
```
在本地启动应用以在数据库中生成 SQL 架构和默认值。
¥Start the application locally to generate the SQL schema and default values in the database.
关闭应用。
¥Shut down the application.
根据数据库类型,使用以下查询截断每个表并重置主键以仅保留结构:
¥Truncate every table and reset primary keys to only keep the structure, using the following queries, depending on the database type:
- sqlite
- postgresql
- mysql
// truncate
DELETE FROM tableA;
DELETE FROM tableB;
// reset sequences
DELETE FROM sqlite_sequence
WHERE name IN ('table_a', 'table_b', '...');
TRUNCATE tableA, tableB, tableC RESTART IDENTITY CASCADE;
// truncate
SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE tableA;
TRUNCATE tableB;
SET FOREIGN_KEY_CHECKS = 1;
将数据迁移到本地
¥Migrate the data locally
将数据迁移到本地:
¥To migrate the data locally:
要么根据 MongoDB 和 SQL 实现之间的差异(参见 cheatsheet)构建你自己的脚本,使用以下建议:
¥either build your own script based on the differences between MongoDB and SQL implementations (see cheatsheet), using the following advice:
第 1 遍:
¥On the 1st pass:
创建没有关系/组件链接的所有条目。
¥Create all the entries without relations/components links.
创建从 MongoDB id 到 SQL id 的映射。
¥Create a mapping from MongoDB ids to SQL ids.
在第二遍中,根据 MongoDB 到 SQL id 映射创建关系/组件链接。
¥and on the 2nd pass, create the relations/components links based on the MongoDB to SQL ids mapping.
或者使用迁移工具,如 Studio3T 教程,考虑到 MongoDB 和 SQL 实现之间的差异(参见 cheatsheet)。
¥or use a migration tool, like the Studio3T tutorial, taking into account the differences between MongoDB and SQL implementations (see cheatsheet).
将本地数据迁移到生产环境
¥Migrate the local data to production
将本地数据迁移到生产数据库:
¥To migrate the local data to the production database:
转储本地迁移的 SQL 数据。
¥Dump the SQL data migrated locally.
将转储导入生产数据库。
¥Import the dump into the production database.
使用更新的连接器部署应用(有关配置详细信息,请参阅 本地准备迁移)。
¥Deploy the application with the updated connector (see prepare the migration locally for configuration details).
如果本指南用于从 Strapi v3 迁移到 Strapi v4,则数据迁移过程的下一步是继续到 SQL 从 Strapi v3 迁移到 Strapi v4。
¥If the present guide was used to migrate from Strapi v3 to Strapi v4, the next step in the data migration process is to proceed to the SQL migration from Strapi v3 to Strapi v4.