Configure MySQL for Strapi on Platform.sh

Strapi can be configured to use MySQL as its default database. You can choose MySQL when installing your app by selecting custom and MySQL when asked for the installation type. Or you can just configure your existing Strapi application to use MySQL. To configure a MySQL database for Strapi on Platform.sh, follow these steps.

  1. Install the Node.js MySQL driver

    yarn add mysql
  2. Replace the PostgreSQL configuration in your services.yaml file with the following:

    mysql:
        type: oracle-mysql:8.0
        disk: 256

    Note that the minimum disk size for MySQL/Oracle MySQL is 256MB.

  3. In your .platform.app.yaml file, replace the relationship name to match the MySQL database you added:

    relationships:
        mysqldatabase: "mysql:mysql"
  4. In the config folder, locate the database.js file, and replace its content with the following:

    const config = require("platformsh-config").config();
    const path = require("path");
    
    let dbRelationship = "mysqldatabase";
    
    // Strapi default sqlite settings.
    let pool = {};
    let connection = {
      connection: {
        client: "sqlite",
        connection: {
          filename: path.join(
            __dirname,
            "..",
            process.env.DATABASE_FILENAME || ".tmp/data.db"
          ),
        },
        useNullAsDefault: true,
      },
    };
    
    if (config.isValidPlatform() && !config.inBuild()) {
      // Platform.sh database configuration.
      try {
        const credentials = config.credentials(dbRelationship);
        console.log(
          `Using Platform.sh configuration with relationship ${dbRelationship}.`
        );
    
        pool = {
          min: 0,
          max: 10,
          acquireTimeoutMillis: 600000,
          createTimeoutMillis: 30000,
          idleTimeoutMillis: 20000,
          reapIntervalMillis: 20000,
          createRetryIntervalMillis: 200,
        };
    
        connection = {
          connection: {
            client: "mysql",
            connection: {
              host: credentials.ip,
              port: credentials.port,
              database: credentials.path,
              user: credentials.username,
              password: credentials.password,
              ssl: false,
            },
            debug: false,
            pool,
          },
        };
      } catch (e) {
        // Do nothing if 'pg' relationship isn't found.
        // Database configuration will fall back on the SQLite defaults.
      }
    } 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."
        );
      }
    }
    // strapi-api/config/database.js
    module.exports = ({ env }) => connection;

This setting deploys your Strapi application with a MySQL database. If you don’t specify a MySQL service in your services.yaml file or the Strapi application is run on a local machine the default SQLite database is used.