Create a MariaDB container and manage databases
Creating the MariaDB container
docker create \
--name=mariadb \
-e PUID=1027 \
-e PGID=100 \
-e MYSQL_ROOT_PASSWORD=rootpassword \
-e TZ=Europe/Stockholm \
-e MYSQL_DATABASE=db_name \
-e MYSQL_USER=db_username \
-e MYSQL_PASSWORD=db_password \
-p 3306:3306 \
-v /volume1/docker/mariadb:/config \
--restart unless-stopped \
linuxserver/mariadb
--name: Name of the Docker container
-e PUID: User ID
-e PGID: User Group ID
-e MYSQL_ROOT_PASSWORD: Password needed to access the container
-e TZ: Timezone. Consult this list to find yours
-e MYSQL_DATABASE: In case you want to immediately create a database, this is the name you want to give to the database
-e MYSQL_USER: Every database needs to be assigned to a user. You can create one here
-e MYSQL_PASSWORD: Password of the database
-p 3306:3306: The port you want to map to the database. Remember to open the port from the router admin panel
Accessing MariaDB
- Connect via SSH as root
- Type
docker exec -it mariadb bash - Access as root user using the command
mysql -u root -pand inputting the password
Creating a new database and user
CREATE DATABASE database_name;CREATE USER 'user_name'@'localhost' IDENTIFIED BY 'password';
Giving permission to the new user
GRANT USAGE ON database_name.* TO 'user_name'@localhost IDENTIFIED BY 'password';SHOW GRANTS FOR 'user_name'@localhost;FLUSH PRIVILEGES;
Showing list of users
SELECT user FROM mysql.user;
Showing user's permissions
SHOW GRANTS FOR 'username'@'localhost';
Deleting user and database
DROP USER 'user'@'localhost';DROP DATABASE database_name;