Set up MySQL service

The mysql service provides persistent data storage based on MariaDB versions 10.2 to 10.4, supporting the XtraDB storage engine and reimplemented features from MySQL 5.6 and 5.7.

Reindexing on MariaDB 10.4 takes more time compared to other MariaDB or MySQL versions. See Configuration best practices.

Be careful when upgrading MariaDB from version 10.1 to 10.2. MariaDB 10.1 is the last version that supports XtraDB as the storage engine. MariaDB 10.2 uses InnoDB for the storage engine. After you upgrade from 10.1 to 10.2, you cannot roll back the change. Adobe Commerce supports both storage engines; however, you must check extensions and other systems used by your project to make sure they are compatible with MariaDB 10.2. See Incompatible Changes Between 10.1 and 10.2.

Use the following instructions for service setup on Adobe Commerce on cloud infrastructure Pro Integration environments and Starter environments, including the master branch.

You must submit a Support ticket to change the service configuration on Pro Production and Staging environments. See Services.

To enable MySQL:

  1. Add the required name, type, and disk value (in MB) to the .magento/services.yaml file.

    1
    2
    3
    
    mysql:
        type: mysql:<version>
        disk: 5120
    

    MySQL errors, such as PDO Exception: MySQL server has gone away, can occur as a result of insufficient disk space. Verify that you have allocated sufficient disk space to the service in the .magento/services.yaml file.

  2. Configure the relationships in the .magento.app.yaml file.

    1
    2
    
    relationships:
        database: "mysql:mysql"
    
  3. Add, commit, and push your code changes.

    1
    
    git add -A && git commit -m "Enable mysql service" && git push origin <branch-name>
    
  4. Verify the service relationships.

    After initial service setup, you can change the software version for an installed service by updating the services.yaml and .magento.app.yaml configuration files. See Change service version.

Configure MySQL database

You have the following options when configuring the MySQL database:

  • schemas—A schema defines a database. The default schema is the main database.
  • endpoints—Each endpoint represents a credential with specific privileges. The default endpoint is mysql, which has admin access to the main database.
  • properties—Properties are used to define additional database configurations.

The following is a basic example configuration in the .magento/services.yaml file:

1
2
3
4
5
6
7
mysql:
    type: mysql:10.4
    disk: 5120
    configuration:
        properties:
            optimizer_switch: "rowid_filter=off"
            optimizer_use_condition_selectivity: 1

The properties in the above example modifies the default optimizer settings as recommended in the Performance Best Practices guide.

Set up multiple database users

Optionally, you can set up multiple users with different permissions for accessing the main database.

By default, there is one endpoint named mysql that has administrator access to the database. To set up multiple database users, you must define multiple endpoints in the services.yaml file and declare the relationships in the .magento.app.yaml file. For Pro Staging and Production environments, submit a Support ticket to request the additional user.

Use a nested array to define the endpoints for specific user access. Each endpoint can designate access to one or more schemas (databases) and different levels of permission on each.

The valid permission levels are:

  • ro: Only SELECT queries are allowed.
  • rw: SELECT queries and INSERT, UPDATE, and DELETE queries are allowed.
  • admin: All queries are allowed, including DDL queries (CREATE TABLE, DROP TABLE, and so on).

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
mysql:
    type: mysql:10.4
    disk: 5120
    configuration:
        schemas:
            - main
        endpoints:
            admin:
                default_schema: main
                privileges:
                    main: admin
            reporter:
                privileges:
                    main: ro
            importer:
                privileges:
                    main: rw
        properties:
            optimizer_switch: "rowid_filter=off"
            optimizer_use_condition_selectivity: 1

In the preceding example, the admin endpoint provides admin-level access to the main database, the reporter endpoint provides read-only access, and the importer endpoint provides read-write access, which means:

  • The admin user has full control of the database.
  • The reporter user has SELECT privileges only.
  • The importer user has SELECT, INSERT, UPDATE, and DELETE privileges.

Add the endpoints defined in the above example to the relationships property of the .magento.app.yaml file. For example:

1
2
3
4
relationships:
    database: "mysql:admin"
    databasereporter: "mysql:reporter"
    databaseimporter: "mysql:importer"

If you configure one MySQL user, you cannot use the DEFINER access control mechanism for stored procedures and views.

Connect to the database

Accessing the MariaDB database directly requires you to use a SSH to log in to the remote Cloud environment, and connect to the database.

  1. Use SSH to log in to the remote environment.

    1
    
    magento-cloud ssh
    
  2. Retrieve the MySQL login credentials from the database and type properties in the $MAGENTO_CLOUD_RELATIONSHIPS variable.

    1
    
    echo $MAGENTO_CLOUD_RELATIONSHIPS | base64 -d | json_pp
    

    or

    1
    
    php -r 'print_r(json_decode(base64_decode($_ENV["MAGENTO_CLOUD_RELATIONSHIPS"])));'
    

    In the response, find the MySQL information. For example:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    "database" : [
       {
          "password" : "",
          "rel" : "mysql",
          "hostname" : "nnnnnnnn.mysql.service._.magentosite.cloud",
          "service" : "mysql",
          "host" : "database.internal",
          "ip" : "###.###.###.###",
          "port" : 3306,
          "path" : "main",
          "cluster" : "projectid-integration-id",
          "query" : {
             "is_master" : true
          },
          "type" : "mysql:10.3",
          "username" : "user",
          "scheme" : "mysql"
       }
    ],
    
  3. Connect to the database.

    • For Starter, use the following command:

      1
      
      mysql -h database.internal -u <username>
      
    • For Pro, use the following command with hostname, port number, username, and password retrieved from the $MAGENTO_CLOUD_RELATIONSHIPS variable.

      1
      
      mysql -h <hostname> -P <number> -u <username> -p'<password>'
      

You can use the magento-cloud db:sql command to connect to the remote database and run SQL commands.

Connect to secondary database

Sometimes, you have to connect to the secondary database to improve database performance or resolve database locking issues. If this configuration is required, use "port" : 3304 to establish the connection. See the Adobe Commerce on cloud infrastructure database best practice article in the Adobe Commerce Help Center.

Troubleshooting

See the following Adobe Commerce Support articles for help with troubleshooting MySQL problems: