Set up the mysql service

The mysql service provides data storage. It’s based on MariaDB, supporting the XtraDB storage engine (equivalent to MySQL with InnoDB).

We support MariaDB version 10.0, which includes reimplemented features from MySQL 5.6 and 5.7.

To access the MariaDB database directly, open an SSH tunnel and use the following command:

mysql -h database.internal -u user

Set up multiple database users

You can optionally set up multiple databases as well as multiple users with different permissions. By default, there is one endpoint named mysql that has administrator access to all defined databases.

To set up multiple databases and users, you must specify multiple endpoints. An endpoint is a user who has privileges you specify.

You can’t use multiple databases with Magento Enterprise Cloud Edition at this time. You can, however, create multiple endpoints to restrict access to the main database.

To specify user access, use the endpoints nested array. Each endpoint can have access to one or more schemas (databases), and can have different levels of permission on each.

The valid permission levels are:

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

If no endpoints are defined, a single endpoint named mysql has admin access to the main database.

Example:

mysqldb:
    type: mysql:10.0
    disk: 2048
    configuration:
        schemas:
            - main
        endpoints:
            admin:
                default_schema: main
                privileges:
                    main: admin
            reporter:
                privileges:
                    main: ro
            importer:
                privileges:
                    main: rw

In the preceding example, the endpoint (that is, user) reporter has ro privileges to the main database and endpoint importer has rw access to the main database. This means that:

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

Relationship

The format exposed in the $MAGENTO_CLOUD_RELATIONSHIPS follows:

{
  "database": [
    {
      "host": "database.internal",
      "ip": "192.0.2.150",
      "password": "",
      "path": "main",
      "port": 3306,
      "query": {
          "is_master": true
      },
      "scheme": "mysql",
      "username": "user"
    }
  ]
}

Usage example

In your .magento/services.yaml:

mysql:
    type: mysql:10.0
    disk: 2048

In your .magento.app.yaml:

relationships:
    database: "mydatabase:mysql"

You can use the preceding service in a configuration file of your application as follows:

$relationships = getenv("MAGENTO_CLOUD_RELATIONSHIPS");
if (!$relationships) {
  return;
}

$relationships = json_decode(base64_decode($relationships), TRUE);

foreach ($relationships['database'] as $endpoint) {
  if (empty($endpoint['query']['is_master'])) {
    continue;
  }
  $container->setParameter('database_driver', 'pdo_' . $endpoint['scheme']);
  $container->setParameter('database_host', $endpoint['host']);
  $container->setParameter('database_port', $endpoint['port']);
  $container->setParameter('database_name', $endpoint['path']);
  $container->setParameter('database_user', $endpoint['username']);
  $container->setParameter('database_password', $endpoint['password']);
  $container->setParameter('database_path', '');
}
  • If you configure one MySQL user, you cannot use the DEFINER access control mechanism for stored procedures and views.
  • MySQL errors such as PDO Exception 'MySQL server has gone away are usually the result of exhausting your existing disk space. Be sure you have sufficient space allocated to the service in .magento/services.yaml.