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:

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.

MariaDB configuration options:

OptionsDescriptionDefault value
default_charsetThe default character set.utf8mb4
default_collationThe default collation.utf8mb4_unicode_ci
max_allowed_packetMaximum size for packets, in MB. Range 1 to 100.16
optimizer_switchSet values for the query optimizer. See MariaDB documentation.
optimizer_use_condition_selectivitySelect the statistics used by the optimizer. Range 1 to 5. See MariaDB documentation.4 for 10.4 and later

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 an Adobe Commerce 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 more).

For example:

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:

relationships:
    database: "mysql:admin"
    databasereporter: "mysql:reporter"
    databaseimporter: "mysql:importer"
NOTE
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 an SSH to log in to the remote Cloud environment, and connect to the database.

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

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

    echo $MAGENTO_CLOUD_RELATIONSHIPS | base64 -d | json_pp
    

    or

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

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

    "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:

      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.

      mysql -h <hostname> -P <number> -u <username> -p'<password>'
      
TIP
You can use the magento-cloud db:sql command to connect to the remote database and run SQL commands.

Connect to secondary database

IMPORTANT
This feature is available on Pro Production and Staging clusters only.

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 Best practice to configure the MySQL slave connection topic in the Implementation Best Practices guide.

Troubleshooting

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

Previous pageElasticsearch
Next pageOpenSearch

Commerce