Manage the database

The Cloud Docker development environment provides MySQL services through a MariaDB (default) or MySQL database deployed to the Docker database container. You connect to the database using docker-compose commands. You can also import data from an existing Magento project into the database container using the magento-cloud db:dump command.

Connect to the database

You can connect to the database through the Docker container or through the database port. Before you begin, locate the database credentials in the database section of the .docker/config.php file. The examples use the following default credentials:

Filename: .docker/config.php

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
return [
    'MAGENTO_CLOUD_RELATIONSHIPS' => base64_encode(json_encode([
        'database' => [
            [
                'host' => 'db',
                'path' => 'magento2',
                'password' => 'magento2',
                'username' => 'magento2',
                'port' => '3306'
            ],
        ],
        // The following configuration is available if you are using the split database architecture.
        'database-quote' => [
            [
                'host' => 'db-quote',
                'path' => 'magento2',
                'password' => 'magento2',
                'username' => 'magento2',
                'port' => '3306'
            ],
        ],
        'database-sales' => [
            [
                'host' => 'db-sales',
                'path' => 'magento2',
                'password' => 'magento2',
                'username' => 'magento2',
                'port' => '3306'
            ],
        ],

To connect to the database using Docker commands:

  1. Connect to the CLI container.

    1
    
    docker-compose run --rm deploy bash
    
  2. Connect to the database with a username and password.

    1
    
    mysql --host=db --user=magento2 --password=magento2
    

    If you use the split database architecture:

    1
    
    mysql --host=db-quote --user=magento2 --password=magento2
    
    1
    
    mysql --host=db-sales --user=magento2 --password=magento2
    
  3. Verify the version of the database service.

    SELECT VERSION();
    +--------------------------+
    | VERSION()                |
    +--------------------------+
    | 10.0.38-MariaDB-1~xenial |
    +--------------------------+
    

To connect to the database port:

  1. Find the port used by the database. The port can change each time you restart Docker.

    1
    
    docker-compose ps
    

    Sample response:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
              Name                         Command               State               Ports
    --------------------------------------------------------------------------------------------------
    magento-cloud_db_1          docker-entrypoint.sh mysqld      Up       0.0.0.0:32769->3306/tcp
    
    # The following lines are available if you are using the split database architecture.
    
    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
    
    
  2. Connect to the database with port information from the previous step.

    1
    
    mysql -h127.0.0.1 -P32769 -umagento2 -pmagento2
    

    If you use the split database architecture, use the following ports to connect:

    For ‘db-quote’ service:

    1
    
       mysql -h127.0.0.1 -32873 -umagento2 -pmagento2
    

    For ‘db-sales’ service:

    1
    
       mysql -h127.0.0.1 -32874 -umagento2 -pmagento2
    
  3. Verify the version of the database service.

    SELECT VERSION();
    +--------------------------+
    | VERSION()                |
    +--------------------------+
    | 10.0.38-MariaDB-1~xenial |
    +--------------------------+
    

Import a database dump

Before you import a database from an existing Magento installation into a new Magento Commerce Cloud environment, you must add the encryption key from the remote environment to the new environment, and then deploy the changes. See Add the Magento encryption key.

To import a database dump into the Docker environment:

  1. Create a local copy of the remote database.

    1
    
    magento-cloud db:dump
    

    The magento-cloud db:dump command runs the mysqldump command with the --single-transaction flag, which allows you to back up your database without locking the tables.

  2. Place the resulting SQL file into the .docker/mysql/docker-entrypoint-initdb.d folder.

    The ece-tools package imports and processes the SQL file the next time you build and start the Docker environment using the docker-compose up command. When you build, you must add the --with-entrypoint option to the ece-docker build:compose command. This option configures the directories for the imported database. See Service configuration options.

Although it is a more complex approach, you can use GZIP to import the database by sharing the .sql.gz file using the .docker/mnt directory and import it inside the Docker container.

Customize the database container

You can inject a MySQL configuration into the database container at creation by adding the configuration to the docker-compose-override.yml file. Add the custom values using an included my.cnf file, or add the correct variables directly to the override file as shown in the following examples.

Add a custom my.cnf file to the docker-compose.override.yml file:

1
2
3
db:
  volumes:
    - path/to/custom.my.cnf:/etc/mysql/conf.d/custom.my.cnf

Add configuration values to the docker-compose.override.yml file:

1
2
3
  db:
    environment:
      - innodb-buffer-pool-size=134217728

See Docker service containers for details about the Database container and container configuration.