6.2. Database - Mark-van-B/IT-Landscape-UCLL- GitHub Wiki

Introduction to MongoDB

Most databases are built around a format that accomodates the use of SQL scripts for creating, reading, writing, and updating database entries. While this has many benefits, sometimes it's worth looking beyond SQL-based databases.

In this case, we're going to make a MongoDB database through Docker Compose.

Installation

  1. create a docker-compose.yaml file in a new folder, somewhere on your PC.
  2. In it, paste the following code:
services:
  mongo:
    image: mongo
    container_name: mongo
    restart: always
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    volumes:
      - mongo-data:/data/db
  mongo-express: # Spacing is important here: this needs to be 2 spaces indented, just like 'mongo' on line 2.
    image: mongo-express
    container_name: mongo-express
    restart: always
    ports:
    - "8081:8081"
    environment: # The 5 lines underneath need a press with the Tab-key in front of each, in order to indent them properly.
        ME_CONFIG_MONGODB_ADMINUSERNAME: root
        ME_CONFIG_MONGODB_ADMINPASSWORD: example
        ME_CONFIG_MONGODB_SERVER: mongo
        ME_CONFIG_BASICAUTH_USERNAME: admin
        ME_CONFIG_BASICAUTH_PASSWORD: admin123
volumes:
  mongo-data:

image

  1. Then, use the docker compose up -d command in any Terminal.
  2. Once that's done, the mongodb container should appear in Docker Desktop.

image

  1. Click the port link, or manually go to localhost:8081 in any browser.
  2. While you can work through the browser, one of the big reasons we use a NoSQL database is to use scripts that aren't SQL.
  • In any terminal, preferably Docker Desktop for convenience, use the following command: docker exec -it mongo mongosh -u root -p example -u = username, -p = password, they're both specified in the compose.yaml file.

image

  1. This will land you in the container, from which you can run queries to manipulate the database.
  • For example, show dbs will return a list of all the databases.

image

image

Maintenance

Creating backup

Very important with software, even more so with databases! Type the following command in the Terminal, outside of the scope of the container (Ctrl+C to back out if you're still in there):

docker exec -it mongo mongodump -u root -p example --out /backup

image

Now, this will only create a backup inside the container, so if you also want one on your local device, use the following command: docker cp mongo:/backup ./local_backup

image

The backup has been placed in a new folder named "local_backup", inside Users/MARK0, because that's where the Terminal was positioned when I threw the command. By either giving it a custom file path, or using the "cd" command to first navigate to that environment, you can adjust where the backup will appear.

MongoDB commands

For this, I'm just going to reference the full list on the official website. It will be more useful to have the complete list at hand.

MongoDB command list

To enter the commands, go to any Terminal and enter the container environment through:

docker exec -it mongo mongosh -u root -p example, with "root" and "example" the same as Username and Password in the compose.yaml file.