Skip to content

Instantly share code, notes, and snippets.

@Descent098
Created January 24, 2023 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Descent098/f6eee8b87ad1efe2e103b43c209c487c to your computer and use it in GitHub Desktop.
Save Descent098/f6eee8b87ad1efe2e103b43c209c487c to your computer and use it in GitHub Desktop.
Creating a MariaDB import docker compose

This file will teach you how to setup a mariaDB docker container, import a .sql dump and visualize it.

Step 1 Docker containers

First create a compose.yml file with the following info:

# Use root/example as user/password credentials
version: '3.1'

services:

  db:
    image: mariadb
    restart: always
    environment:
      - MARIADB_USER=example-user
      - MARIADB_PASSWORD=my_cool_secret
      - MARIADB_ROOT_PASSWORD=my-secret-pw
    volumes:
      - ./mariadb:/var/lib/mysql


  adminer: # Visualizer
    image: adminer
    restart: always
    ports:
      - 8080:8080

THIS FILE IS NOT SECURE, DO NOT OPEN ANY PORTS TO THE OUTSIDE WORLD

Now run the compose file using docker-compose up -d or docker compose up -d depending on which utility you have installed. This will build the files and create a folder called /mariadb in your CWD with all the files for the install.

Step 2. Accessing your container & finding your file

Once the setup is done running copy your .sql file into /mariadb. There are then 2 options to choose from to open a terminal in the container (pick either one):

  • Docker desktop; If you have it installed head to docker desktop, go to containers and find the mariadb container. Hit the terminal icon to open a terminal in the container. image

  • Docker exec; Run docker ps, and find the mariabd image in the table. Copy the container ID and run docker exec -it <CONTAINER ID> bash. image

Regardless of which you picked run cd /var/lib/mysql to get to the correct folder for step 3!

Step 3. Creating a DB to import to

Your .sql file will be in /var/lib/mysql which we just navigated to. First we need to create a database, then we can import it.

Create a database by running mysql -u root -p, it will ask for a password, which is the MARIADB_ROOT_PASSWORD we set earlier (my-secret-pw if you never changed it).

Once you have entered the password run CREATE DATABASE <DATABASE NAME>; where <DATABASE NAME> is the name you want for the imported DB.

Step 4. Importing the .sql file to the new DB

so you can now import it with mysql -u root -p <DATABASE NAME> < <FILENAME>.sql again it will prompt you for the root password which will be MARIADB_ROOT_PASSWORD (my-secret-pw if you never changed it).

Visualization (optional)

Now you can go to localhost:8080 and login with:

System: MySQL
Server: db
Username: root
Password: <MARIADB_ROOT_PASSWORD> (`my-secret-pw` if you never changed it)
Database: <DATABASE NAME>

This will let you visualize all your tables in one place!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment