MariaDB Guide: Setup, Queries, and MySQL Compatibility
MariaDB is an open-source relational database in the MySQL family. It speaks the MySQL client protocol and supports familiar SQL, while maintaining its own releases, features, storage engines, and compatibility rules.
For query syntax, start with the SQL guide. The database engine guides explain how MariaDB compares with the other options.
Pick MariaDB when your framework, host, or Linux distribution names MariaDB support, and test the exact release your application will run. MariaDB and MySQL share an early history, but each now has features the other lacks.
When Should You Use MariaDB?
MariaDB fits applications that already use MySQL-family conventions and have explicit support from the framework, host, Linux distribution, or managed provider. Ordinary tables, joins, indexes, InnoDB transactions, and common connectors can make the first setup look very similar to MySQL.
Choose MariaDB when the surrounding application and operations support these conditions:
- The application officially supports the intended MariaDB release.
- The host or operating system maintains MariaDB packages and upgrades.
- The team already understands MySQL-family administration and SQL.
- MariaDB-specific features or storage engines solve a documented requirement.
- Backup, restore, monitoring, and replication tools have been tested with MariaDB.
Do not choose it only because a dependency says "MySQL compatible." Compatibility depends on the exact server, connector, SQL mode, collation, feature set, and migration path.
How Do You Install and Connect to MariaDB?
Install MariaDB Server and its client for your operating system, start the service, and connect with mariadb. Package names and option-file locations vary across Linux distributions, containers, macOS, Windows, and managed services.
On a Debian or Ubuntu development machine, the package-manager path commonly uses these commands:
sudo apt update
sudo apt install mariadb-server mariadb-client
sudo systemctl status mariadb After installation, use the install-time administrative connection and ask the server for its identity:
sudo mariadb At the MariaDB prompt:
SELECT VERSION();
SHOW VARIABLES LIKE 'version_comment';
SHOW VARIABLES LIKE 'default_storage_engine'; The output confirms the server release, distribution label, and default storage engine. Record the full version in deployment notes instead of writing only "MySQL/MariaDB."
How Do You Create a Restricted MariaDB User?
Create a restricted MariaDB account with CREATE USER and grant only its required database permissions. MariaDB accounts combine a user name with a host. The account 'app_runtime'@'127.0.0.1' is different from 'app_runtime'@'%'. Use the narrowest host rule that matches the deployment.
Create a runtime user for one application database:
CREATE DATABASE codewalkers_app
CHARACTER SET utf8mb4;
CREATE USER 'app_runtime'@'127.0.0.1'
IDENTIFIED BY 'replace-with-a-generated-secret';
GRANT SELECT, INSERT, UPDATE, DELETE
ON codewalkers_app.*
TO 'app_runtime'@'127.0.0.1';
SHOW GRANTS FOR 'app_runtime'@'127.0.0.1'; Leave the administrative session, then verify the restricted account through the same host used by the PHP connection:
mariadb -u app_runtime -p -h 127.0.0.1 codewalkers_app PHP uses the PDO MySQL driver for MariaDB connections because MariaDB supports the MySQL client protocol. Use the same restricted account in the application:
$pdo = new PDO(
'mysql:host=127.0.0.1;dbname=codewalkers_app;charset=utf8mb4',
'app_runtime',
$password,
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
],
); The runtime role can read and change application rows, while schema changes remain outside its grants. Run migrations through a separate account with the required DDL (schema-change) privileges, then remove or disable that access when the deployment process allows it.
Use secret storage provided by the deployment platform and rotate a credential if it appears in a log, terminal history, or committed file. Test the restricted account before deployment because an application that silently depends on root privileges has an incomplete permission model.
How Do MariaDB System-Versioned Tables Work?
MariaDB system-versioned tables keep earlier versions of rows so you can query data as it existed at another time. The feature was introduced in MariaDB 10.3.4. This can support auditing and change analysis when the storage and retention costs are planned.
Reconnect with the administrator or migration account, select codewalkers_app, and create a small price table with system versioning enabled:
USE codewalkers_app;
CREATE TABLE product_prices (
product_id BIGINT UNSIGNED NOT NULL,
price DECIMAL(10, 2) NOT NULL,
PRIMARY KEY (product_id)
) WITH SYSTEM VERSIONING;
INSERT INTO product_prices (product_id, price)
VALUES (101, 19.95);
UPDATE product_prices
SET price = 21.50
WHERE product_id = 101; A normal selection returns the current price:
SELECT product_id, price
FROM product_prices
WHERE product_id = 101;
-- product_id | price
-- 101 | 21.50 Use FOR SYSTEM_TIME ALL to include the stored history:
SELECT product_id, price
FROM product_prices
FOR SYSTEM_TIME ALL
WHERE product_id = 101
ORDER BY ROW_START;
-- product_id | price
-- 101 | 19.95
-- 101 | 21.50 The exact period timestamps depend on when the statements run. Historical rows consume storage, and retention must be designed before a busy table keeps every change indefinitely. Verify version support and backup behavior before using this feature in production.
How Do You Configure MariaDB for an Application?
Configure MariaDB by setting server and client options in files such as my.cnf or my.ini. The search order depends on the installation. Use the installed server to print its option-file locations instead of guessing:
mariadbd --help --verbose Keep the initial configuration small and documented:
[mariadb]
default_storage_engine=InnoDB
character-set-server=utf8mb4
[client]
default-character-set=utf8mb4 InnoDB is the normal storage engine for transactional application tables. It supports transactions, row-level locking, crash recovery, and foreign keys. Other MariaDB storage engines target different workloads, so each non-default choice needs an operational reason.
For production, enable TLS for network connections and check the server SQL mode and connection character set. Keep backups and upgrades separate from tuning.
How Do You Back Up and Upgrade MariaDB?
Back up MariaDB with mariadb-dump or a compatible physical backup tool, then test the restore and upgrade on a separate copy before changing production. For a small InnoDB database, mariadb-dump can create a logical backup that is readable and suitable for a rehearsed restore:
sudo mariadb-dump \
--single-transaction \
--routines \
--events \
--dump-history \
--databases codewalkers_app > codewalkers_app.sql
sudo mariadb < codewalkers_app.sql Run the restore on a separate test server, not the live application database.
--single-transaction provides a consistent logical snapshot for transactional InnoDB tables without locking them for the full dump. --dump-history includes system-versioned table history and requires MariaDB 10.11 or later. When moving historical data from before 11.5 to 11.5 or newer, check the additional --update-history requirement. The --databases form writes the database creation and selection statements needed by the straight restore. Check the documentation before applying the same command to non-transactional tables or a database with special backup requirements.
Verify a restored backup by checking row counts, constraints, routines, events, application queries, and representative file or object references. Store backups away from the server they protect and test the recovery time against the application's needs.
For an upgrade, read every release note between the source and destination lines. Run the upgrade on a restored copy, execute migrations and integration tests, compare important query plans, then rehearse rollback. Connector support and managed-host support must cover the destination release before production changes.
Keep the exact server version beside each backup. Recovery after a failed or interrupted upgrade may require the previous major release to start and repair the data directory before another upgrade attempt.
How Does MariaDB Differ from MySQL?
MariaDB differs from MySQL in features such as system-versioned tables, JSON storage, authentication, and replication. Their shared client protocol does not make their data files interchangeable. Current migrations can encounter different authentication plugins, collations, JSON behavior, replication features, optimizer decisions, system tables, release policies, and on-disk formats.
Before moving an application between them, prepare a tested migration plan:
- List every server-specific data type, function, SQL mode, and storage engine.
- Check the connector and framework support matrix for the destination release.
- Run the full migration and test suite against a copy of production-shaped data.
- Export and import through documented tools where direct file compatibility is unsupported.
- Restore a backup on the destination release and verify row counts and constraints.
The MySQL, MariaDB, and MaxDB comparison explains where those differences affect the choice.
How Did MariaDB Begin?
MariaDB development began in 2009 as a fork of MySQL led by MySQL co-founder Michael "Monty" Widenius. Oracle announced its agreement to acquire Sun Microsystems in April 2009, and the acquisition was completed in January 2010. Sun owned MySQL at the time, so the fork preserved a separately governed MySQL-family database during that ownership change.
The first MariaDB Server release arrived in October 2009. MariaDB continued the naming pattern recorded in the official histories: MySQL was named after Widenius's daughter My, and MariaDB was named after his daughter Maria.
The shared history explains the similar client protocol, SQL syntax, tools, and terminology without guaranteeing permanent equivalence. MariaDB and MySQL now publish separate release lines and implement features independently.
Common Pitfalls
Assuming Drop-In Means Identical
Old compatibility guidance is applied to a newer MariaDB and MySQL pair. Compare the exact versions, read the compatibility notes, and run migrations plus integration tests on MariaDB.
Running the Application as Root
Local setup credentials are copied into the deployed application. Create a limited runtime user, keep schema-change permissions in a separate migration role, and rotate exposed credentials.
Copying Unknown Tuning Settings
A large configuration from another workload is treated as a universal baseline. Keep defaults unless a measured workload justifies a change, then document the reason and test the result.
Ignoring History Growth
System versioning is enabled without retention or storage planning. Estimate change volume, define how long history is required, monitor table growth, and test backup and restore times.
For related decisions, read MySQL guide, SQL transactions and ACID, PHP PDO.
Frequently Asked Questions
Is MariaDB a drop-in replacement for MySQL?
MariaDB is not a universal drop-in replacement for MySQL; compatibility depends on the exact server versions and application. MariaDB retains substantial MySQL protocol and syntax compatibility, but features, defaults, authentication, collations, replication, and on-disk formats have diverged. Check the support matrix and test migrations, queries, backups, and restores against the exact MariaDB release.
Why does PHP PDO use a mysql DSN for MariaDB?
PHP's PDO MySQL driver speaks the MySQL client protocol used by MariaDB, so the DSN begins with mysql. That shared driver does not make every server feature identical. Keep the server version explicit and test the application's SQL against MariaDB.
Which storage engine should a MariaDB web app use?
InnoDB is the usual choice for a MariaDB web application that needs transactions. It supports transactions, row-level locking, crash recovery, and foreign keys. Choose another engine only for a documented workload requirement, then test its durability, backup, locking, and operational behavior.
Should a MariaDB application use utf8mb4 or the plain utf8 character set?
Use utf8mb4 for a MariaDB application's database and connections so they can store the full Unicode range, including emoji. The utf8 alias can resolve differently depending on MariaDB's old_mode setting; naming utf8mb4 avoids that ambiguity.
Does MariaDB system versioning replace regular backups?
MariaDB system versioning does not replace backups. It keeps row history inside the same live database and does not protect against a dropped table, a failed disk, or a corrupted data directory. Treat it as a query feature for auditing, kept alongside a tested backup and restore process.
Sources
-
[1]
MariaDB Basics(mariadb.com)
-
[2]
MariaDB Unicode Character Sets(mariadb.com)
-
[3]
History of MySQL(dev.mysql.com)
-
[4]
Why Is the Software Called MariaDB?(mariadb.com)
-
[5]
Installing MariaDB(mariadb.com)
-
[6]
Configuring MariaDB with Option Files(mariadb.com)
-
[7]
InnoDB Storage Engine(mariadb.com)
-
[8]
System-Versioned Tables(mariadb.com)
-
[9]
mariadb-dump(mariadb.com)
-
[10]
MariaDB Versus MySQL Compatibility(mariadb.com)
Read Next
Learn where MySQL fits, how to create a restricted application user, how InnoDB transactions work, and how to plan backups and upgrades.
Compare MySQL, MariaDB, and SAP MaxDB by lineage, support context, compatibility testing, and migration criteria.
Use PDO prepared statements to connect PHP to MySQL safely and keep SQL input handling clear.
Guides to the common SQL database engines: MySQL, PostgreSQL, SQLite, and MariaDB, and when to reach for each.