Enable Magento split database solution

The Split database performance solution improves scalability by providing three separate databases to manage different functional areas of the Magento application. The following instructions show how to configure this solution in the Magento Cloud Docker environment, exporting the Magento Sales and Magento Quote data from the main database and creating two separate databases in the Cloud Docker environment.

See the Magento split database performance solution topic in the Configuration Guide for detailed information and extended examples.

To add split DB to Magento Commerce Cloud project configuration:

  1. In the root directory of your Magento project, add the database services to the .magento/services.yaml configuration file.

    1
    2
    3
    4
    5
    6
    7
    8
    
    ...
    mysql-quote:
        type: mysql:10.2
        disk: 512
    mysql-sales:
        type: mysql:10.2
        disk: 512
    ...
    
  2. In the .magento.app.yaml file, add the relationships for the additional databases.

    1
    2
    3
    4
    5
    
    relationships:
        ...
        database-quote: "mysql-quote:mysql"
        database-sales: "mysql-sales:mysql"
       ...
    
  3. Generate the Docker Compose configuration file.

    1
    
    php vendor/bin/ece-docker build:compose
    

    As with the ‘db’ container, you can use the options to set ports to forward to your local environment.

    1
    
    php vendor/bin/ece-docker build:compose --expose-db-quote-port=<port for quote db service> --expose-db-sales-port=<port for sales db service>
    
  4. Verify the updated database configuration in the ./docker-compose.yml file.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    
    $ cat  ./docker-compose.yml
    ...
    services:
      ...
      db-quote:
        hostname: db-quote.magento2.docker
        image: 'mariadb:10.2'
        environment:
          - MYSQL_ROOT_PASSWORD=magento2
          - MYSQL_DATABASE=magento2
          - MYSQL_USER=magento2
          - MYSQL_PASSWORD=magento2
        ports:
          - '3306'
        volumes:
          - 'magento-db-quote:/var/lib/mysql'
          - 'docker-entrypoint-quote:/docker-entrypoint-initdb.d'
          - 'mariadb-conf:/etc/mysql/mariadb.conf.d'
          - 'docker-mnt:/mnt:delegated'
        networks:
          magento:
            aliases:
              - db-quote.magento2.docker
      db-sales:
        hostname: db-sales.magento2.docker
        image: 'mariadb:10.2'
        environment:
          - MYSQL_ROOT_PASSWORD=magento2
          - MYSQL_DATABASE=magento2
          - MYSQL_USER=magento2
          - MYSQL_PASSWORD=magento2
        ports:
          - '3306'
        volumes:
          - 'magento-db-sales:/var/lib/mysql'
          - 'docker-entrypoint-sales:/docker-entrypoint-initdb.d'
          - 'mariadb-conf:/etc/mysql/mariadb.conf.d'
          - 'docker-mnt:/mnt:delegated'
        networks:
          magento:
            aliases:
              - db-sales.magento2.docker
    ...
    
  5. Verify that the ./.docker/config.php.dist environment configuration file includes the updated database service configuration:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    <?php
    return [
        'MAGENTO_CLOUD_RELATIONSHIPS' => base64_encode(json_encode([
           //...
            'database-sales' => [
                [
                    'host' => 'db-sales',
                    'path' => 'magento2',
                    'password' => 'magento2',
                    'username' => 'magento2',
                    'port' => '3306'
                ]
            ],
            'database-quote' => [
                [
                    'host' => 'db-quote',
                    'path' => 'magento2',
                    'password' => 'magento2',
                    'username' => 'magento2',
                    'port' => '3306'
                ]
            ],
        ])),
       //...
    ];
    
  6. Build the containers and run in the background.

    1
    
    docker-compose up -d
    
  7. Confirm that the db-quote and db-sales database containers are running.

    1
    2
    3
    4
    5
    6
    7
    8
    
    docker-compose ps
                Name                           Command                  State                Ports
    -------------------------------------------------------------------------------------------------------
    ...
    magento-cloud_db-quote_1        docker-entrypoint.sh mysqld      Up             0.0.0.0:32873->3306/tcp
    magento-cloud_db-sales_1        docker-entrypoint.sh mysqld      Up             0.0.0.0:32874->3306/tcp
    magento-cloud_db_1              docker-entrypoint.sh mysqld      Up             0.0.0.0:32872->3306/tcp
    ...
    
  8. Verify that the MAGENTO_CLOUD_RELATIONSHIPS variable returns the database access credentials.

    1
    
    docker-compose run deploy bash -c "echo $MAGENTO_CLOUD_RELATIONSHIPS | base64 -d | json_pp"
    

    Expected result:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    {
       "database-quote" : [
          {
             "host" : "db-quote",
             "port" : "3306",
             "path" : "magento2",
             "username" : "magento2",
             "password" : "magento2"
          }
       ],
       "database-sales" : [
          {
             "path" : "magento2",
             "port" : "3306",
             "username" : "magento2",
             "host" : "db-sales",
             "password" : "magento2"
          }
       ]
    }
    

Install Magento with the split database solution

After updating the Docker compose configuration file, you must specify the database tables to deploy to the separate database services, and then deploy Magento to the Cloud Docker environment.

To deploy Magento to the Cloud environment with the split database configuration:

  1. In the .magento.env.yaml file in project root, add the SPLIT_DB property with the table names to import into the other data.

    1
    2
    3
    4
    5
    
    stage:
        deploy:
            SPLIT_DB:
                - quote
                - sales
    
  2. Run the build phase of deployment process.

    1
    
    docker-compose run build cloud-build
    
  3. Run the deploy phase of deployment process.

    1
    
    docker-compose run deploy cloud-deploy
    
  4. After deployment, use the database credentials and port information to manage each database.