Stack guide — MariaDB client
Connect to MariaDB from your application
MariaDB is a full relational database server. The client libraries — libmariadb and connectors for every language — let your application query, insert, and manage data. This page covers the runtime side: connecting from C, Python, PHP, Perl, and Node.js.
For server deployment — starting the daemon, securing the installation, creating users, and configuring InnoDB — see the Database stack guide.
C client
libmariadb
The MariaDB C connector is installed as a system library. Include the headers and link with -lmariadb:
#include <mysql.h>
#include <stdio.h>
int main() {
MYSQL *conn = mysql_init(NULL);
if (conn == NULL) {
fprintf(stderr, "mysql_init() failed\n");
return 1;
}
if (mysql_real_connect(conn, "localhost", "myapp",
"<app-user-password>", "myapp", 0, NULL, 0) == NULL) {
fprintf(stderr, "%s\n", mysql_error(conn));
mysql_close(conn);
return 1;
}
if (mysql_query(conn, "SELECT name, email FROM users")) {
fprintf(stderr, "%s\n", mysql_error(conn));
} else {
MYSQL_RES *result = mysql_store_result(conn);
MYSQL_ROW row;
while ((row = mysql_fetch_row(result))) {
printf("%s - %s\n", row[0], row[1]);
}
mysql_free_result(result);
}
mysql_close(conn);
return 0;
}Compile with:
gcc -O3 -march=x86-64-v3 -o /usr/local/bin/myapp myapp.c $(pkg-config --cflags --libs mariadb)The mysql_config helper is also available:
gcc -O3 -march=x86-64-v3 -o /usr/local/bin/myapp myapp.c $(mysql_config --cflags --libs)Man page: man mysql. API reference: mariadb.com/kb.
Python
PyMySQL
Install PyMySQL in your virtual environment:
python3 -m venv /usr/local/venvs/myapp
source /usr/local/venvs/myapp/bin/activate
pip install pymysqlimport pymysql
conn = pymysql.connect(
host='localhost',
user='myapp',
password='<app-user-password>',
database='myapp',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
try:
with conn.cursor() as cur:
cur.execute('SELECT name, email FROM users')
for row in cur:
print(f"{row['name']} - {row['email']}")
finally:
conn.close()For async applications, use aiomysql with asyncio.
PHP
PDO and mysqli
PHP 8.5 ships with both pdo_mysql and mysqli extensions. PDO with named parameters is the recommended API:
<?php
$pdo = new PDO(
'mysql:host=localhost;dbname=myapp;charset=utf8mb4',
'myapp',
'<app-user-password>',
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]
);
$stmt = $pdo->prepare(
'SELECT name, email FROM users WHERE email LIKE :domain'
);
$stmt->execute(['domain' => '%@example.com']);
foreach ($stmt as $row) {
echo $row['name'] . " - " . $row['email'] . "\n";
}See PHP 8.5 for extension details and configuration.
Perl
DBD::MariaDB
Install the DBI driver via CPAN:
cpan> install DBD::MariaDBuse DBI;
use strict;
use warnings;
my $dbh = DBI->connect(
'DBI:MariaDB:database=myapp;host=localhost;mysql_enable_utf8mb4=1',
'myapp',
'<app-user-password>',
{ RaiseError => 1, PrintError => 0 }
) or die $DBI::errstr;
my $sth = $dbh->prepare(
'SELECT name, email FROM users WHERE email LIKE ?'
);
$sth->execute('%@example.com');
while (my $row = $sth->fetchrow_hashref) {
print "$row->{name} - $row->{email}\n";
}
$sth->finish;
$dbh->disconnect;Node.js
mysql2
Install the mysql2 package:
npm install -g mysql2import mysql from 'mysql2/promise';
const conn = await mysql.createConnection({
host: 'localhost',
user: 'myapp',
password: '<app-user-password>',
database: 'myapp',
charset: 'utf8mb4'
});
const [rows] = await conn.execute(
'SELECT name, email FROM users WHERE email LIKE ?',
['%@example.com']
);
for (const row of rows) {
console.log(row.name + ' - ' + row.email);
}
await conn.end();The mysql2/promise API returns native promises. Use it with async/await for clean error handling.
