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
- 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.
- In the application project, open Environment Variables and create
DATABASE_URLwith that string. - Always use the host shown in the Console, in the
mariadb-<id>.managed.zenifra.comformat.
Summary by driver
| Language | Driver | What to do |
|---|---|---|
| Node.js and Bun | mariadb, Prisma 7, Bun.sql | Driver Node · mariadb, Prisma 7, Bun |
| Node.js | mysql2, Sequelize, Knex, TypeORM, Drizzle | Driver Node · mysql2, Sequelize, Knex, TypeORM, Drizzle |
| Node.js | Prisma 6 | Driver Prisma 6 |
| Python | SQLAlchemy (mysqlclient) | Driver Python · SQLAlchemy |
| Go | go-sql-driver/mysql | Driver Go · go-sql-driver/mysql |
| Python and terminal | PyMySQL (1.2), mysql-connector-python, mysql, mariadb | Already use TLS on their own |
| PHP | PDO (pdo_mysql) | Provide the CA bundle in Pdo\Mysql::ATTR_SSL_CA |
| PHP | mysqli | Use the MYSQLI_CLIENT_SSL and MYSQLI_CLIENT_SSL_VERIFY_SERVER_CERT flags |
| PHP | Laravel | Use the Console URI in DB_URL; on the Zenifra PHP runtime, TLS is already set up |
| PHP | Symfony (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 Console | String |
|---|---|
| Node · mariadb, Prisma 7, Bun | mariadb://app:<password>@<host>:<port>/app?ssl=true&sslmode=require |
| Node · mysql2, Sequelize, Knex, TypeORM, Drizzle | mysql://app:<password>@<host>:<port>/app?ssl=%7B%7D |
| Prisma 6 | mysql://app:<password>@<host>:<port>/app?sslaccept=strict |
| Python · SQLAlchemy | mysql://app:<password>@<host>:<port>/app?ssl_mode=REQUIRED |
| Go · go-sql-driver/mysql | app:<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 appPHP 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.crtWithout 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=trueAnd 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.crtOn PHP 8.3, use !php/const PDO::MYSQL_ATTR_SSL_CA.
Common errors
| Message | Cause | How to fix |
|---|---|---|
Access denied for user '…' (using password: YES) on MariaDB, with the right password | The driver connected without TLS | Use 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 object | mysql2 received ssl=true in the string | Use 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 mismatch | The connection uses an IP or another name instead of the Console host | Use the exact host shown in the Console |
Cannot connect to MySQL using SSL or does not match host name in PHP | The connection uses an IP or another name instead of the Console host | Use the exact host shown in the Console |
The given driver "mariadb" is unknown | DoctrineBundle does not recognize mariadb:// | Change the URI scheme to mysql:// |
password authentication failed or Access denied | The password in DATABASE_URL is not the current one | Check the password or generate a new one in the database settings and update the application variable |
Next steps
Last updated on
How to Start MariaDB on Zenifra via Console
Create your managed MariaDB on Zenifra from the console: plans from db-starter, version 10 or 11, 3 instances with failover, storage, and a TLS connection.
How to Change MariaDB Settings
Change your MariaDB settings on Zenifra from the console: password, version, connection string, public access, storage, plan, and stop or delete actions.