# Using DMK

Usually, DMK configures the environment automatically. In some rare cases it must be adapted. DMK also allows you to define your own aliases and environment variables.

#### Alias and variable management

Here is an example of a DMK configuration file:

```yaml
[GLOBAL]
var::INSTANCE_NAMING_CONVENTION::=::nowarn::"^mdb([a-zA-Z]{0,4})(\d+)$"::
# ...
[INSTANCE]
alias::cda::novar_noforce::'cd ${MONGO_BASE}/admin/${MONGO_INSTANCE}'::
# ...
[mdb01]
var::MONGO_URI::=::nowarn::"mongodb://root:mypassword@host1:27001,host2:27001,host3:27001/?replicaset=rs1&readPreference=secondaryPreferred"::
```

DMK sets the environment using two different configuration files:

* `$DMK_HOME/etc/dmk.conf.default`: Default configuration file that defines common MongoDB environment variables and aliases.
* `~/.dmk/dmk.conf.local`: Your local configuration file. It is initially empty and can be freely customized. Every setting made here will overwrite the default ones.

Configuration files are divided into three sections:

* `[GLOBAL]`: Global settings, loaded at login.
* `[INSTANCE]`: MongoDB instance settings, loaded for each MongoDB instance when switching to it.
* `[<instance_name>]`: Specific settings for a MongoDB instance, loaded when switching to it.

#### Declare an alias

Aliases are declared using the following syntax:

`alias::<name>::[var|novar]_[force|noforce]::<your code>::# comment`

`[var|novar]`

* `var`: Automatically creates a variable with the name of the alias with the path specified in the alias. For instance, the alias `cdh='cd $MONGO_HOME'` creates a variable `cdh=$MONGO_HOME`.
* `novar`: Does not create the above variable.

`[force|noforce]`

* `force`: If a command or an alias already exists, it is overwritten.
* `noforce`: A warning is printed instead of overwriting the existing alias or command.

If you specify an alias for `PS1` and it is enclosed in brackets (...) it will be used as-is as a function definition (aliases are implemented as functions). Otherwise, DMK tries to reformat it as a function.

#### Declare a variable

Variables are declared using the following syntax:

`var::<name>::[ =|+|-]::[ begin|end|nooption|warn|nowarn]::<your value>::`

`[=|+|-]`

* `=` : Equal operator
* `+` : expand an existing path variable
* `-` : contract, remove element from a path variable

`[begin|end|nooption|warn|nowarn]`

* `begin/end` : works only with the operators `[+|-]`
* `nooption|warn` : default behavior is to print a warning in case the variable exists
* `nowarn` : disable warning and overwrites the variable

#### Environment loading

When you log into the `mongodb` user after a complete DMK installation, the DMK environment will be loaded automatically, and the current state of the MongoDB instances will be displayed:

```bash
[root@vm00 ~]# su - mongodb
Last login: Mon Jun 30 05:20:36 EDT 2025 on pts/2

Instance             State           Version    Port   Bind IPs
----------------------------------------------------------------------
mdb01                STOPPED         8.0.16     27017  0.0.0.0
mdb02                STOPPED         8.0.16     27102  0.0.0.0
mongodb@vm00:/home/mongodb/ [DUMMY]
```

To know which instances are present, DMK uses the `$MONGO_BASE/etc/mongodb.lst` file:

```bash
mongodb@vm00:/home/mongodb/ [DUMMY] cat ${MONGO_BASE}/etc/mongodb.lst
mdb01:/u01/app/mongodb/product/8.0.16:Y # Generated by DMK - dmk_dbcreate.py
mdb02:/u01/app/mongodb/product/8.0.16:Y # Generated by DMK - dmk_dbcreate.py
mdb03:/u01/app/mongodb/product/8.0.16:N # Generated by DMK - dmk_dbcreate.py
```

For each line:

* The first element is the instance name.
* The second element is the binary folder associated with the instance. This is useful for upgrading specific instances individually.
* The last element is whether the instance is considered active (`Y`) or inactive (`N`). If it's considered inactive, it will not be considered when analyzing the instances. Here, for instance, `mdb03` is considered inactive, so it's not shown when displaying the status of all instances.

#### Switching between environments

At login, no specific instance environment is loaded. To load a specific environment, use the corresponding `<instance_name>` or `<INSTANCE_NAME>` alias.

```bash
mongodb@vm00:/home/mongodb/ [DUMMY] mdb01

----------  dbi services  ----------

INSTANCE                 : mdb01
STATUS                   : STOPPED
VERSION                  : 8.0.16
BIND                     : 0.0.0.0
PORT                     : 27017
REPLICA SET STATUS       : N/A (instance STOPPED)
CONFIGURATION FILE       : /u01/app/mongodb/admin/mdb01/etc/mdb01.conf
DATA PATH                : /u02/mongodb/data/mdb01
LOG FILE                 : /u04/mongodb/log/mdb01/mdb01.log

------------------------------------
mongodb@vm00:/home/mongodb/ [mdb01]
```

This command loads the `[INSTANCE]` and `[mdb01]` sections of your configuration files.
