db_name: is the database name. See naming convention note above.
home_path: is the path where the mongodb binaries are installed.
data_path: is the root path where the data files of the database will be stored.
template: is the template for the init file for mongodb database. Severall ini files are proposed in the templates/dbcreate directory.
To create the database execute the 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.
At the end the structure directory for the database, a systemd mongodb_mdb01.service file to copy it to /etc/systemd/system, and the init mdb01.conf file where created, as root user:
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 mongodb user.
The database can be started/stopped by the systemd service or using the tool `dmk_db_ctl.sh``
# start 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 database
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_monogodb_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 mongodb user.
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:
------------------------------------------------
# set the mdb01 database environment
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
****************************
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 mongodb user.
🔴 Keep credentials file safe.
🔴 To create the first user we need to use the "Localhost Exception". See (https://www.mongodb.com/docs/v4.4/core/security-users/#std-label-localhost-exception) documentation which describe this exception.
The database must bind on localhost (bindIp parameter in the init file: /u01/app/mongodb/admin/mdb01/etc/mdb01.conf)
To use credentials they have to be set into the file $MONGODB_BASE/admin/$MONGODB_SERVER/secret/cred.yml
The alias msp allow to connect using credentials reading them from $MONGODB_BASE/admin/$MONGODB_SERVER/secret/cred.yml file
An template is in $MONGODB_BASE/local/dmk/templates/secret/cred.yml.
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 }
Now you can use the alias msp to connect to the database using th credentials file:
# connect using credentials. Note 'crdentials' in 'Connectong 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>