# Migrate from MongoDB to SQL with Strapi v3
Strapi v4 does not support MongoDB databases (see blog post announcement (opens new window)).
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.
Migrating from MongoDB to SQL with Strapi v3 requires:
- preparing the migration locally,
- migrating the data locally,
- migrating the local data to production.
# Prepare the migration locally
To prepare the migration locally:
Dump the MongoDB database (see MongoDB official documentation (opens new window)) and load it locally.
(optional, only if not migrating to SQLite) Create a SQL database locally.
💡 TIP
To avoid installing SQL and MongoDB on your computer, you can setup the local Mongo and SQL databases using a Docker (opens new window) image.
Switch the application connector to bookshelf and configure it with the previously created databases:
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.1
b. Update the configuration in
./config/database.js
:Before, with a MongoDB database:
// before { connector: 'mongoose', settings: { database: 'strapi', username: 'strapi', password: 'strapi', port: 27017, host: 'localhost', }, options: {}, }
After, with a SQL database:
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:
# Migrate the data locally
To migrate the data locally:
either build your own script based on the differences between MongoDB and SQL implementations (see cheatsheet), using the following advices:
- On the 1st pass:
- Create all the entries without relations/components links.
- Create a mapping from MongoDB ids to SQL ids.
- and on the 2nd pass, create the relations/components links based on the MongoDB to SQL ids mapping.
- On the 1st pass:
or use a migration tool, like the Studio3T tutorial (opens new window), 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:
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).
🤓 Next steps
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.