Configure MongoDB for Strapi on Platform.sh
Try for 30 days
Flexible, version-controlled infrastructure provisioning and development-to-production workflows
Strapi can also be configured to use MongoDB as its default database, although due to compatibility issues this database type is only available in Strapi v3 and not supported in Strapi v4. To use MongoDB with a Strapi v3 application on Platform.sh, follow these steps.
-
Install the Strapi mongoose connector
yarn add strapi-connector-mongoose
-
Replace the PostgreSQL configuration in your
.platform/services.yaml
file with the following:.platform/services.yamlmongodb: type: mongodb:3.6 disk: 512
Note that the minimum disk size for MongoDB is 512MB.
-
In your
.platform.app.yaml
file, replace the relationship name to match the MongoDB database you added:.platform.app.yamlrelationships: mongodatabase: service: "mongodb" endpoint: "mongodb"
-
In the
config
folder, locate thedatabase.js
file, and replace its content with the following:const config = require("platformsh-config").config(); let dbRelationshipMongo = "mongodatabase"; // Strapi default sqlite settings. let settings = { client: "sqlite", filename: process.env.DATABASE_FILENAME || ".tmp/data.db", }; let options = { useNullAsDefault: true, }; if (config.isValidPlatform() && !config.inBuild()) { // Platform.sh database configuration. const credentials = config.credentials(dbRelationshipMongo); console.log( `Using Platform.sh configuration with relationship ${dbRelationshipMongo}.` ); settings = { client: "mongo", host: credentials.host, port: credentials.port, database: credentials.path, username: credentials.username, password: credentials.password, }; options = { ssl: false, authenticationDatabase: "main", }; } else { if (config.isValidPlatform()) { // Build hook configuration message. console.log( "Using default configuration during Platform.sh build hook until relationships are available." ); } else { // Strapi default local configuration. console.log( "Not in a Platform.sh Environment. Using default local sqlite configuration." ); } } module.exports = { defaultConnection: "default", connections: { default: { connector: "mongoose", settings: settings, options: options, }, }, };
This configuration instructs Platform.sh to deploy your Strapi v3 app with a MongoDB database.