Local MariaDB Database, using Docker June 3, 2020
Installation
Run the following docker command:
docker run --name mariaDb -e MYSQL_ROOT_PASSWORD=root -d -p 3306:3306 -v mariaDbData:/var/lib/mysql mariadb
Where:
--namewill name the containermariaDb-pwill expose port3306to the host machine-vwill create a volume namedmariaDbData, so the database data will persist between uses
For more information, read docker run --help
Usage
The Docker container running the database can be used the same way as any other instance of a MySQL/MariaDB database. This means you can connect to it using PHPMyAdmin or any other database access tool, such as DBeaver.
MariaDB connection strings are formatted like so: jdbc:mariadb://<host>:<port>/<database>. The driver's class name is org.mariadb.jdbc.Driver. The table below contains the default connection information.
| Parameter | Value |
|---|---|
| Connection string | jdbc:mariadb://localhost:3306/<database> |
| Host | localhost |
| Port | 3306 |
| Database | You must create one |
| User | root |
| Password | root |
Maintenance
Connecting using mysql
To connect using mysql, simply run the following docker command:
> docker exec -ti mariaDb mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 10.4.13-MariaDB-1:10.4.13+maria~bionic mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>...
MariaDB [(none)]>exit
Where:
-tiallows to type in an interactive shell-uprovides the user using psql-pindicates that you wish to provide a password (enterrootwhen prompted)
For more information, read docker exec --help
Adding a new user and database
To add a new user and database, start by connecting to the container using mysql, then simply run the following commands:
MariaDB [(none)]>CREATE DATABASE <database>;
MariaDB [(none)]>GRANT ALL PRIVILEGES ON <database>.* TO '<user>'@'%' IDENTIFIED BY '<password>';
Tailing the logs
To tail the logs, simply run the following docker command:
docker logs -f mariaDb --tail 500
Where:
-fwill keep following the logs in real time--tailwill show the last 500 lines of log
For more information, read docker logs --help
Docker Cheatsheet
Last updated on June 3, 2020
— Etienne Lamoureux