These are the default locations for some important mongodb files.
- config file :
/etc/mongod.conf - log directory :
/var/log/mongodb - data directotry :
/var/lib/mongodb
- config file :
/usr/local/etc/mongod.conf - log directory :
/usr/local/var/log/mongodb - data directotry :
/usr/local/var/mongodb
- config file :
/opt/homebrew/etc/mongod.conf - log directory :
/opt/homebrew/var/log/mongodb - data directory :
/opt/homebrew/var/mongodb
When running as a service, the default config file will be used
$ sudo systemctl start mongod$ sudo systemctl stop mongod$ sudo systemctl restart mongod
$ brew services start mongodb/brew/mongodb-community$ brew services stop mongodb/brew/mongodb-community$ brew services restart mongodb/brew/mongodb-community
$ mongod --config /usr/local/etc/mongod.conf --fork
$ mongod --config /opt/homebrew/etc/mongod.conf --fork- NOTE: This could be useful when needing to run a
testdatabase with separatelogfiles anddatadirectory. Or we could use the samelogfiles and the samedatadirectory and instead just create a new databasecovizu_testwithin the Mongo application.
$ mongosh
- By default MongoDB does not seem to enable any authorization. Meaning anyone can connect to our database on
port:27017and CRUD our data. - However, it might be different for you. When trying to open the
cliif you get an error that looks like this
Current Mongosh Log ID: 63fc56344c05787dbf853d87
Connecting to:mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.7.1
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
then it probably means your MongoDB has locked you out. You need to disable authorization.
- To enable/disable authorization, open the afore-mentioned
mongod.confconfiguraiton file. - Add (remove) the following lines to the bottom of the config file to enable (disable) authorization
security:
authorization: enabled
- After editing those lines, restart the
MongoDB daemon. - To confirm that you've succesfully enabled(disabled) authorization, open
mongoshwithout any auth token.$ mongosh
- Switch to the
admindatabase$ use admin
- View all the databases
$ show databases
- If
MongoDBprints out something like this then you've succesfully disabled (unsuccesfully enabled) authentication.
admin> show databases
admin 180.00 KiB
config 108.00 KiB
local 120.00 KiB
- If instead,
MongoDBprints out something like this then you've succesfully enabled (unsuccesfully disabled) authentication.
admin> show databases
MongoServerError: command listDatabases requires authentication
- On first install, we need to create a
adminaccount, and acovizuacccount (readonly). To do so, first disable authorization in themongod.confconfiguration file - Then ensure that your
.env.devor.env.prodfile contains the environment variables for the following
DATA_FOLDER='data'
ADMIN_USERNAME='admin'
ADMIN_PASSWORD='supersecretpassword'
COVIZU_USERNAME='covizu'
COVIZU_PASSWORD='supersecretpassword'
DB_URL='localhost:27017'
-
Before creating the user accounts, first we check for any existing user accounts of the same name as our
ADMIN_USERNAMEorCOVIZU_USERNAME. Run the followingnodejsscript.$ npm run delete-users
-
To confirm that there are no user accounts with conflicting names open the
mongosh$ mongoshtest> use admintest> show users
-
After deleting any existing user accounts, run the following script
$ npm run create-users
-
Once your user accounts are created, edit the
configfile to enable authorization and restart the service.
- If you haven't already, obtain the following data files from our main server at https://filogeneti.ca/covizu/data/:
timetree.nwkdbstats.jsonclusters.jsonand save your local copies undercovizu/data/.- To import these data files into our
MongoDBdatabase run the following script$ npm run update-db1
- This will create our primary database and import the
JSON/textrecords into it. - Once the import script has finished executing, start the server by executing the following script
$ npm run start-db1
- Navgiate your browser to
localhost:8001to verify that it works. - If that went well, then run the next script to setup our secondary database
$ npm run update-db2
- After executing the import script, start the server with a connection to the secondary database
$ npm run start-db2
- To update the database, first obtain the new data files from
https://filogeneti.ca/covizu/dataand replace the files incovizu/data - If your
nodeserver is currently using the primary (secondary) database then update the secondary (primary) database$ npm run update-db2($ npm run update-db1)
- After updating the secondary (primary) database, close the currently running server which is still serving data from the primary(secondary) database and restart the server with a connection to the secondary (primary) database
$ npm run start-db2($ npm run start-db1)
- On the next batch of new data, update the primary (secondaary) database and restart the node server with a connection to the primary (secondary) database
-
To open
mongoCLI in non-authenticated mode$ mongosh
-
To open
mongoCLI as anadmin$ mongosh --username admin --authenticationDatabase admin
-
To view all users
- Connect as
admin() use adminshow users
- Connect as
-
To delete an existing user
"myusername"- Connect as
admin use adminshow usersdb.dropUser('myusername')
- Connect as
-
To view all collections within a database
"mydatabase"- Connect as
admin use mydatabaseshow collections
- Connect as
-
To drop the database
"mydatabase"- Connect as
admin use mydatabasedb.dropDatabase()
- Connect as
-
To drop the collection
"mycollection"with the database"mydatabase"- Connect as
admin use mydatabasedb.mycollection.drop()
- Connect as