Skip to Content
DatabaseMariaDB on Zenifra

Connect your application to MariaDB

This guide shows how to connect an application to Zenifra MariaDB with the most common Node.js, Bun, Python, PHP, and Go drivers. The connection strings in the Console already use TLS: with most drivers, you just paste the string for your driver, without code changes and without extra environment variables.

Using PostgreSQL? See Connect your application to PostgreSQL.

What TLS is

TLS encrypts everything between your application and the database: password, queries, and data. We recommend keeping it on.

  • MariaDB: always requires TLS.

Before you start

  1. Open the database in the Console, pick your driver in the Driver selector of the Connection panel, and copy the Connection string for your driver. The full password is shown only at creation or after a password rotation.
  2. In the application project, open Environment Variables and create DATABASE_URL with that string.
  3. Always use the host shown in the Console, in the mariadb-<id>.managed.zenifra.com format.

Summary by driver

LanguageDriverWhat to do
Node.js and Bunmariadb, Prisma 7, Bun.sqlDriver Node · mariadb, Prisma 7, Bun
Node.jsmysql2, Sequelize, Knex, TypeORM, DrizzleDriver Node · mysql2, Sequelize, Knex, TypeORM, Drizzle
Node.jsPrisma 6Driver Prisma 6
PythonSQLAlchemy (mysqlclient)Driver Python · SQLAlchemy
Gogo-sql-driver/mysqlDriver Go · go-sql-driver/mysql
Python and terminalPyMySQL (1.2), mysql-connector-python, mysql, mariadbAlready use TLS on their own
PHPPDO (pdo_mysql)Provide the CA bundle in Pdo\Mysql::ATTR_SSL_CA
PHPmysqliUse the MYSQLI_CLIENT_SSL and MYSQLI_CLIENT_SSL_VERIFY_SERVER_CERT flags
PHPLaravelUse the Console URI in DB_URL; on the Zenifra PHP runtime, TLS is already set up
PHPSymfony (Doctrine)Change mariadb:// to mysql:// and provide the CA bundle in doctrine.yaml

Connection string for your driver

Access denied for user '…' (using password: YES) with the right password? MariaDB always requires TLS. When the driver connects without TLS, the message looks like a wrong password, but the cause is the missing TLS. Use the string for your driver, as below.

There is no single string that every MariaDB and MySQL driver understands: each driver turns TLS on in its own way. That is why the Connection panel in the Console has the Driver selector, which shows the Connection string for your driver, always with TLS. Paste the string as is.

Driver in the ConsoleString
Node · mariadb, Prisma 7, Bunmariadb://app:<password>@<host>:<port>/app?ssl=true&sslmode=require
Node · mysql2, Sequelize, Knex, TypeORM, Drizzlemysql://app:<password>@<host>:<port>/app?ssl=%7B%7D
Prisma 6mysql://app:<password>@<host>:<port>/app?sslaccept=strict
Python · SQLAlchemymysql://app:<password>@<host>:<port>/app?ssl_mode=REQUIRED
Go · go-sql-driver/mysqlapp:<password>@tcp(<host>:<port>)/app?tls=true

The database's default string is the first one, mariadb://…?ssl=true&sslmode=require.

Node.js with mariadb

import mariadb from 'mariadb'

const conn = await mariadb.createConnection(process.env.DATABASE_URL)
await conn.query('SELECT 1')

On Bun, use new Bun.SQL(process.env.DATABASE_URL) with the same string. On Prisma 7, use the same string in the MariaDB adapter.

Node.js and Bun with mysql2

Use the string for the Node · mysql2, Sequelize, Knex, TypeORM, Drizzle driver, which ends with ?ssl=%7B%7D:

import mysql from 'mysql2/promise'

const conn = await mysql.createConnection(process.env.DATABASE_URL)
await conn.query('SELECT 1')

Sequelize, Knex, TypeORM, and Drizzle use mysql2 underneath and accept the same string.

Prisma 6 with MariaDB

Use the string for the Prisma 6 driver, which ends with ?sslaccept=strict, in url = env("DATABASE_URL") in the datasource of schema.prisma.

Python with SQLAlchemy (mysqlclient)

Use the string for the Python · SQLAlchemy driver, which ends with ?ssl_mode=REQUIRED. Do not add other parameters: mysqlclient rejects the ones it does not know.

import os
from sqlalchemy import create_engine

engine = create_engine(os.environ["DATABASE_URL"])

Python with PyMySQL

PyMySQL (measured on version 1.2.3) and mysql-connector-python already use TLS on their own. For PyMySQL to also verify the certificate and hostname, provide the CA bundle:

import os
from urllib.parse import unquote, urlsplit

import pymysql

url = urlsplit(os.environ["DATABASE_URL"])
conn = pymysql.connect(
    host=url.hostname,
    port=url.port,
    user=unquote(url.username),
    password=unquote(url.password),
    database=url.path.lstrip("/"),
    ssl_ca="/etc/ssl/certs/ca-certificates.crt",
    ssl_verify_cert=True,
    ssl_verify_identity=True,
)

The /etc/ssl/certs/ca-certificates.crt path exists in Debian, Ubuntu, and Alpine images with the ca-certificates package installed. On other systems, use the matching CA bundle.

Go with go-sql-driver/mysql

Use the string for the Go · go-sql-driver/mysql driver, which ends with ?tls=true:

db, err := sql.Open("mysql", os.Getenv("DATABASE_URL"))

mysql and mariadb clients

The mysql and mariadb command-line clients already use TLS on their own:

mariadb -h <host> -P <port> -u app -p app

PHP with PDO (pdo_mysql)

<?php
$url = parse_url(getenv('DATABASE_URL'));
$pdo = new PDO(
    sprintf('mysql:host=%s;port=%d;dbname=%s', $url['host'], $url['port'], ltrim($url['path'], '/')),
    rawurldecode($url['user']),
    rawurldecode($url['pass']),
    [Pdo\Mysql::ATTR_SSL_CA => '/etc/ssl/certs/ca-certificates.crt'],
);

With the CA bundle, the driver uses TLS and verifies the certificate and hostname. Without it, PDO connects without TLS and the database rejects the connection with Access denied. On PHP 8.3, use PDO::MYSQL_ATTR_SSL_CA; from 8.4 on, use Pdo\Mysql::ATTR_SSL_CA, because the old constant is deprecated in PHP 8.5.

PHP with mysqli

<?php
$url = parse_url(getenv('DATABASE_URL'));
$db = mysqli_init();
$db->real_connect(
    $url['host'],
    rawurldecode($url['user']),
    rawurldecode($url['pass']),
    ltrim($url['path'], '/'),
    $url['port'],
    null,
    MYSQLI_CLIENT_SSL | MYSQLI_CLIENT_SSL_VERIFY_SERVER_CERT,
);

MYSQLI_CLIENT_SSL_VERIFY_SERVER_CERT verifies the certificate and hostname against the system CAs. With MYSQLI_CLIENT_SSL alone, the connection is encrypted but accepts any certificate.

Laravel with MariaDB

DB_CONNECTION=mariadb
DB_URL=<Console URI>

On the Zenifra PHP runtime (a project without its own Dockerfile), that is all: the image already points MySQL/MariaDB TLS at the system certificate authorities, and Laravel's default config/database.php uses that setting. The connection goes out with TLS and the certificate is verified, with no certificate file and nothing to configure.

With your own Dockerfile, add the same line to the image:

ENV MYSQL_ATTR_SSL_CA=/etc/ssl/certs/ca-certificates.crt

Without it, PDO connects without TLS and the database rejects the connection with Access denied.

Symfony with Doctrine and MariaDB

DoctrineBundle does not recognize the mariadb:// scheme of the Console URI. Change it to mysql://:

DATABASE_URL=mysql://app:<password>@<host>:<port>/app?ssl=true

And provide the CA bundle in config/packages/doctrine.yaml:

doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'
        options:
            !php/const Pdo\Mysql::ATTR_SSL_CA: /etc/ssl/certs/ca-certificates.crt

On PHP 8.3, use !php/const PDO::MYSQL_ATTR_SSL_CA.

Common errors

MessageCauseHow to fix
Access denied for user '…' (using password: YES) on MariaDB, with the right passwordThe driver connected without TLSUse the string for your driver in the Driver selector, or the CA bundle and TLS flags in PHP, as in Connection string for your driver
SSL profile must be an objectmysql2 received ssl=true in the stringUse the string for the Node · mysql2, Sequelize, Knex, TypeORM, Drizzle driver, with ssl=%7B%7D
Hostname/IP does not match certificate's altnames or IP address mismatchThe connection uses an IP or another name instead of the Console hostUse the exact host shown in the Console
Cannot connect to MySQL using SSL or does not match host name in PHPThe connection uses an IP or another name instead of the Console hostUse the exact host shown in the Console
The given driver "mariadb" is unknownDoctrineBundle does not recognize mariadb://Change the URI scheme to mysql://
password authentication failed or Access deniedThe password in DATABASE_URL is not the current oneCheck the password or generate a new one in the database settings and update the application variable

Next steps

Last updated on

On this page