visit
Frequently, I have to deploy MariaDB for development on my working laptop, a
MariaDB publishes
It’s always recommended to use the
It’s worth mentioning that there also is a
Spinning up a MariaDB container for development doesn’t require major configurations. Typically you’ll need to expose a port and set a root password. This can be done using environment variables. Check each image documentation to learn about the available parameters. Additionally, you might want to set up a custom
docker run --name mariadb-database \
--detach \
--volume mariadb-database-volume:/var/lib/mysql \
--publish 3333:3306 \
--env MARIADB_ROOT_PASSWORD='the_root_password' \
--env MARIADB_DATABASE=the_database \
--env MARIADB_USER=the_app_user \
--env MARIADB_PASSWORD='the_user_password' \
mariadb:latest
This configures a detached (running in the background) container with a MariaDB database using a Docker volume named mariadb-database-volume
. The database is, accessible on port 3333 (3333:3306
means “forward requests on port 3333 to 3306 in the container”). The root user has the_root_password
. the_database
is automatically created and the_app_user
can access this database using the_user_password
. If you want the database to start automatically when Docker starts, add the --restart unless-stopped
option somewhere before the image name (mariadb:latest
).
MariaDB can be configured using
You can pass system variables in the command line when you run a container. For example, to enable the
docker run --name mariadb-database \
--detach \
--env MARIADB_ROOT_PASSWORD='the_root_password' \
mariadb:latest \
--log_bin=primary_log_bin --server_id=1 --port=3333
docker exec -it mariadb-database \
mariadb --port 3333 --password='the_root_password' \
--execute='select @@log_bin, @@server_id'
To pass custom configuration files, use Docker volumes. Custom .cnf files should be placed in the /etc/mysql/conf.d/ directory inside the container. For example, you can enable the binary log and set a server ID using the following my-server.cnf file:
[mariadb]
log_bin = primary_log_bin
server_id = 1
docker run --name mariadb-database \
--detach \
--volume $PWD:/etc/mysql/conf.d \
--env MARIADB_ROOT_PASSWORD='the_root_password' \
mariadb:latest
docker exec -it mariadb-database \
mariadb --password='the_root_password' \
--execute='select @@log_bin, @@server_id'
Any .sql, .sh (as well as .sql.gz, .sql.xz and .sql.zst) files in the /docker-entrypoint-initdb.d/ directory inside the container, are run after the container is created and initialized. Let’s combine this feature with option files to configure a primary server with a user for
CREATE USER 'replication_user'@'%' IDENTIFIED BY 'password';
GRANT REPLICATION REPLICA ON *.* TO 'replication_user'@'%';
RESET MASTER;
CREATE USER 'maxscale_user'@'%' IDENTIFIED BY 'password';
GRANT SELECT ON mysql.* TO 'maxscale_user'@'%';
GRANT SHOW DATABASES, SLAVE MONITOR ON *.* to 'maxscale'@'%';
To keep things tidy, we can create two directories in the host machine. One to store .cnf files and one to store .sql files:
A directory structure with separate subdirectories for config and SQL files
To pass these files to the container, run it as follows:
docker run --name mariadb-database \
--detach \
--volume mariadb-database-volume:/var/lib/mysql \
--volume $PWD/sql:/docker-entrypoint-initdb.d \
--volume $PWD/cnf:/etc/mysql/conf.d \
--env MARIADB_ROOT_PASSWORD='the_root_password' \
mariadb:latest
All the SQL scripts in your local ./sql directory and all the config files in ./cnf are passed to the container. For examples of how to use .sh files see
MariaDB Docker images, like any Docker image, can be extended using
A Dockerfile alongside a “copy” directory that follows the structure of the container’s filesystem
Instead of using Docker volumes, we can simply copy the files to the image filesystem using the COPY instruction. This instruction is placed in a custom Dockerfile:
FROM mariadb
ENV MARIADB_ROOT_PASSWORD="password"
ENV MARIADB_DATABASE="demo"
ENV MARIADB_USER="user"
ENV MARIADB_PASSWORD="password"
COPY primary/copy/ /
EXPOSE 3306
Since the .sql and .cnf files are placed in the expected subdirectories inside the copy/ directory, they will work as described in the previous sections.
After getting a MariaDB database running in a Docker container, you might want to connect to it externally. Remember to publish the port on which MariaDB Server is listening (the default is 3306) using something like --publish 3333:3306
when you run the container. The following examples use 3333 but you can also use any free port in the host, even 3306 (--publish 3306:3306
).
You can connect to the database using any SQL client that directly supports MariaDB, ODBC, or JDBC. For example 127.0.0.1
, the port as 3333
, the user as the_app_user
(or root
), and the password as the_user_password
(or the_root_password
).
You can also use the mariadb
CLI client to connect to the database as follows:
mariadb --host 127.0.0.1 --port 3333 --user the_app_user -p
You can connect to the database from Java, Python, Node.js, and others using the
jdbc:mariadb://localhost:3333/the_database
import mariadb
connection = mariadb.connect(
host="127.0.0.1",
port=3333,
database="the_database"
user="the_app_user",
password="the_user_password")
const mariadb = require('mariadb');
connection = await mariadb.createConnection({
host: 127.0.0.1',
port: '3333',
database: 'the_database',
user: 'the_app_user',
password: 'the_user_password'
});
You can find more examples on the