Basic management

Basic management

Table of contents

Create the first database

All commands are executed as the mongodb user.

To create the database directory file structure and configuration file, use the template located at $DMK_HOME/templates/dbcreate/mcreate_sample_tpl.yaml

Although the template can be placed anywhere, it is a good practice to copy it to $DMK_HOME/etc before adapting it.

🟢 The naming convention for the database is defined in the $DMK_HOME/etc/dmk.conf file. By default, it is set to the regular expression ^mdb(\D){0,4}(\d+)$

mongodb@dev-vm:/home/mongodb/ [DUMMY] cp $DMK_HOME/templates/dbcreate/mcreate_sample_tpl.yaml $DMK_HOME/etc/mcreate_mdb01.yaml
mongodb@dev-vm:/home/mongodb/ [DUMMY] cp $DMK_HOME/templates/dbcreate/mongo_ini_sample.yaml $DMK_HOME/etc/mongo_ini_sample.yaml

# edit the file mcreate_mdb01.yaml
mongodb@dev-vm:/home/mongodb/ [DUMMY] cat /u01/app/mongodb/local/dmk/etc/mcreate_mdb01.yaml
# mongod.conf
# Generated by MONGODB - DMK dbi services

mongodb_cfg:
   db_name: mdb01
   home_path: /u01/app/mongodb/product/8.0
   data_path: /u02/mongodbdata
   bindIp: 0.0.0.0
   port: 25630
   template: /u01/app/mongodb/local/dmk/etc/mongo_ini_sample.yaml
  • db_name: Database name (see naming conventions note above).

  • home_path: Path where the MongoDB binaries are installed.

  • data_path: Root path where database data files will be stored.

  • template: Path to the MongoDB configuration file template. Several templates are available in the templates/dbcreate directory.

To create the database, execute the following command:

mongodb@dev-vm:/home/mongodb/ [DUMMY] dmk_dbcreate.sh -c /u01/app/mongodb/local/dmk/etc/mcreate_mdb01.yaml
2025-01-14_10-29-52::dmk_mongodb_create.p::Mainprogram         ::INFO     ==> Configuration File = /u01/app/mongodb/local/dmk/etc/mcreate_mdb01.yaml
2025-01-14_10-29-52::dmk_mongodb_create.p::check_conf_param    ::WARNING  ==> bindIp is set to 0.0.0.0. There might be issues if TLS is used.
2025-01-14_10-29-52::dmk_mongodb_create.p::MainProgram         ::INFO     ==> Create directory layout for mdb01
2025-01-14_10-29-52::dmk_mongodb_create.p::main::mkdir_if_not_e::INFO     ==> Create directory: /u02/mongodblog/mdb01
2025-01-14_10-29-52::dmk_mongodb_create.p::main::mkdir_if_not_e::INFO     ==> Create directory: /u02/mongodbdata/mdb01
2025-01-14_10-29-52::dmk_mongodb_create.p::main::mkdir_if_not_e::INFO     ==> Create directory: /u01/app/mongodb/admin/mdb01
2025-01-14_10-29-52::dmk_mongodb_create.p::main::mkdir_if_not_e::INFO     ==> Create directory: /u01/app/mongodb/admin/mdb01/pid
2025-01-14_10-29-52::dmk_mongodb_create.p::main::mkdir_if_not_e::INFO     ==> Create directory: /u01/app/mongodb/admin/mdb01/etc
2025-01-14_10-29-52::dmk_mongodb_create.p::main::mkdir_if_not_e::INFO     ==> Create directory: /u01/app/mongodb/admin/mdb01/backup
2025-01-14_10-29-52::dmk_mongodb_create.p::main::mkdir_if_not_e::INFO     ==> Create directory: /u01/app/mongodb/admin/mdb01/dump
2025-01-14_10-29-52::dmk_mongodb_create.p::main::mkdir_if_not_e::INFO     ==> Create directory: /u01/app/mongodb/admin/mdb01/secret
2025-01-14_10-29-52::dmk_mongodb_create.p::create_init_mongo_co::INFO     ==> Create MongoDB conf from template: /u01/app/mongodb/local/dmk/templates/dbcreate/mongo_ini_sample.yaml
2025-01-14_10-29-52::dmk_mongodb_create.p::create_init_mongo_co::INFO     ==> Create MongoDB init file: /u01/app/mongodb/admin/mdb01/etc/mdb01.conf
2025-01-14_10-29-52::dmk_mongodb_create.p::create_init_mongo_co::INFO     ==> Create systemd file from template /u01/app/mongodb/local/dmk/templates/systemd/mongod.service
2025-01-14_10-29-52::dmk_mongodb_create.p::create_systemd_file ::INFO     ==> Created service file for systemd /u01/app/mongodb/admin/mdb01/etc/mongod_mdb01.service
2025-01-14_10-29-52::dmk_mongodb_create.p::create_systemd_file ::INFO     ==>      copy it to /etc/systemd/system as root user
2025-01-14_10-29-52::dmk_mongodb_create.p::MainProgram         ::INFO     ==> Update /u01/app/mongodb/etc/mongodb.lst file with mdb01
2025-01-14_10-29-52::dmk_mongodb_create.p::MainProgram         ::INFO     ==> Source the DMK to get settings new instance mdb01
2025-01-14_10-29-52::dmk_mongodb_create.p::MainProgram         ::INFO     ==>       by executting the command 'source /u01/app/mongodb/local/dmk/bin/dmk.sh'
2025-01-14_10-29-52::dmk_mongodb_create.p::MainProgram         ::INFO     ==> Database mdb01 created.

The script creates several files and directories :

  • The database directory structure.

  • A systemd file named mongodb_mdb01.service, which has to be copied to /etc/systemd/system.

  • The configuration file mdb01.conf.

As the root user, copy the service file and start the database :

root $ cp /u01/app/mongodb/admin/mdb01/etc/mongod_mdb01.service /etc/systemd/system
root $ systemctl daemon-reload
root $ systemctl start mongod_mdb01.service
root $ systemctl status mongod_mdb01.service
● mongodb_mdb01.service - Mongod Database Service
     Loaded: loaded (/etc/systemd/system/mongod_mdb01.service; disabled; vendor preset: enabled)
     Active: active (running) since Wed 2024-03-06 10:24:17 UTC; 7s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 9170 (mongod)
      Tasks: 33 (limit: 6987)
     Memory: 136.3M
        CPU: 2.540s
     CGroup: /system.slice/mongodb_mdb01.service
             └─9170 /u01/app/mongodb/product/8.0/bin/mongod -f /u01/app/mongodb/admin/mdb01/etc/mdb01.conf

Mar 06 10:24:20 dmk-mongo-dev bash[9129]:   connectionId: 5,
Mar 06 10:24:20 dmk-mongo-dev bash[9129]:   minWireVersion: 0,
Mar 06 10:24:20 dmk-mongo-dev bash[9129]:   maxWireVersion: 21,
Mar 06 10:24:20 dmk-mongo-dev bash[9129]:   readOnly: false,
Mar 06 10:24:20 dmk-mongo-dev bash[9129]:   ok: 1
Mar 06 10:24:20 dmk-mongo-dev bash[9129]: }
Mar 06 10:24:20 dmk-mongo-dev bash[9129]: 2024-03-06_10-24-20::MDB.pm              ::MDB::get_db_state   ::DEBUG    ==> Database mdb01 state OPEN
Mar 06 10:24:20 dmk-mongo-dev bash[9129]: 2024-03-06_10-24-20::dmk_mongodb_ctl.pl  ::control_database    ::INFO     ==> Database mdb01 is now OPEN. SUCCESS
Mar 06 10:24:20 dmk-mongo-dev bash[9129]: 2024-03-06_10-24-20::dmk_mongodb_ctl.pl  ::control_database    ::INFO     ==> Database mdb01 is now OPEN. SUCCESS
Mar 06 10:24:20 dmk-mongo-dev bash[9229]: 2024-03-06_10-24-20::NONE                ::EXIT                ::DEBUG    ==> Program exited with ExitCode : 0

Start/Stop the database

All commands are executed as the mongodb user. You can start/stop the database using the systemd service or the dmk_db_ctl.sh tool.

# Start command (if already started)
mongodb@dev-vm:/home/mongodb/ [DUMMY] dmk_db_ctl.sh -a start -d mdb01
2025-01-14_14-17-51::dmk_mongodb_ctl.pl  ::control_database    ::INFO     ==> Database mdb01 is already OPEN state. SUCCESS

# Restart command
mongodb@dev-vm:/home/mongodb/ [DUMMY] dmk_db_ctl.sh -a restart -d mdb01
2025-01-14_14-18-14::dmk_mongodb_ctl.pl  ::dmk_mongodb_ctl.pl  ::INFO     ==> RESTART database mdb01 ...
2025-01-14_14-18-15::dmk_mongodb_ctl.pl  ::control_database    ::INFO     ==> Put database mdb01 in state CLOSED ...
2025-01-14_14-18-21::dmk_mongodb_ctl.pl  ::control_database    ::INFO     ==> Database mdb01 is now CLOSED. SUCCESS
2025-01-14_14-18-21::dmk_mongodb_ctl.pl  ::control_database    ::INFO     ==> Put database mdb01 in state OPEN ...
2025-01-14_14-18-27::dmk_mongodb_ctl.pl  ::control_database    ::INFO     ==> Database mdb01 is now OPEN. SUCCESS

Connect to the database

All commands are executed as the mongodb user. We'll first list the available databases, then load the environment of a specific database, and connect to it.

To list all the MongoDB databases running on the system, just run the u command :

mongodb@dmk-mongo-dev:/home/mongodb/ [DUMMY] u


 MongoDB Instances OPEN and running on this host:
 ------------------------------------------------
    mdb01 ==> OPEN


 MongoDB Instances CLOSED and NOT running on this host:
 ------------------------------------------------

Load the environment related to a specific database named mdb01 by running the mdb01 command :

mongodb@dmk-mongo-dev:/home/mongodb/ [DUMMY] mdb01

********* dbi services Ltd. *********
  STATUS         : OPEN
  BIND           : 0.0.0.0
  PORT           : 25630
  REPL           : STANDALONE
  CONF FILE      : /u01/app/mongodb/admin/mdb01/etc/mdb01.conf
  DATA PATH      : /u02/mongodbdata/mdb01
  LOG FILE       : /u02/mongodblog/mdb01/mdb01.log
****************************

Finally, run the ms command to connect to the database :

mongodb@dev-vm:/home/mongodb/ [DUMMY] ms

Current Mongosh Log ID:	65e84545d1f4e3ca944d1b9d
Connecting to:		mongodb://128.0.0.1:25610/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.1.1
Using MongoDB:		8.0.5
Using Mongosh:		2.1.1
mongosh 2.1.5 is available for download: https://www.mongodb.com/try/download/shell

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

test>

Create the admin user

All commands are executed as the mongodb user.

🔴 Keep credentials file safe.

🔴 To create the first user we need to use the "Localhost Exception". See the documenation which describes this exception.

The database must bind on localhost (through the bindIp parameter in the configuration file: /u01/app/mongodb/admin/mdb01/etc/mdb01.conf).

Credentials must be set in the $MONGODB_BASE/admin/$MONGODB_SERVER/secret/cred.yml file. A template is available at $MONGODB_BASE/local/dmk/templates/secret/cred.yml. The alias msp allows the connection using credentials set in the $MONGODB_BASE/admin/$MONGODB_SERVER/secret/cred.yml file.

mongodb@dmk-mongo-dev:/home/mongodb/ [mdb01] cp $MONGODB_BASE/local/dmk/templates/secret/cred.yml /u01/app/mongodb/admin/mdb01/secret/cred.yml
# Add your username and password to the cred.yml file.
mongodb@dmk-mongo-dev:/home/mongodb/ [mdb01] chmod 400 /u01/app/mongodb/admin/mdb01/secret/cred.yml
mongodb@dmk-mongo-dev:/home/mongodb/ [mdb01] cat $MONGODB_BASE/admin/$MONGODB_SERVER/secret/cred.yml
....
mdb_admin_user: "root"
mdb_admin_pwd: "root123"

To create the user defined in the $MONGODB_BASE/local/dmk/templates/secret/cred.yml file, use the following command:

# Create the user. Note that the roles can be adapted regarding your needs.
mongodb@dmk-mongo-dev:/home/mongodb/ [mdb01]  ms --eval 'db.getSiblingDB("admin").createUser( { user: "root", pwd: "root123" ,roles: [ "root" ]})'
{ ok: 1 }

Once the user is created, you can modify your configuration file to enable authorization. You should set the security.authorization parameter to enabled, by adding this section in the configuration file.

security:
    authorization: enabled

Now restart the database with the dmk_db_ctl.sh -a restart -d mdb01 command, and reload the environment with the mdb01 command.

You can now use the alias msp to connect to the database using the credentials file:

# Connect using credentials. Note 'credentials' in 'Connecting to:' part
mongodb@dmk-mongo-dev:/home/mongodb/ [mdb01] msp
Current Mongosh Log ID:	6638dd6b0485919ee2f2535f
Connecting to:		mongodb://<credentials>@0.0.0.0:25119/?directConnection=true&appName=mongosh+2.1.1
Using MongoDB:		8.0.5
Using Mongosh:		2.1.1
mongosh 2.2.5 is available for download: https://www.mongodb.com/try/download/shell

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

test>

Last updated