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
test
database with separatelog
files anddata
directory. Or we could use the samelog
files and the samedata
directory and instead just create a new databasecovizu_test
within the Mongo application.
$ mongosh
- By default MongoDB does not seem to enable any authorization. Meaning anyone can connect to our database on
port:27017
and CRUD our data. - However, it might be different for you. When trying to open the
cli
if 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.conf
configuraiton 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
mongosh
without any auth token.$ mongosh
- Switch to the
admin
database$ use admin
- View all the databases
$ show databases
- If
MongoDB
prints 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,
MongoDB
prints 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
admin
account, and acovizu
acccount (readonly). To do so, first disable authorization in themongod.conf
configuration file - Then ensure that your
.env.dev
or.env.prod
file 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_USERNAME
orCOVIZU_USERNAME
. Run the followingnodejs
script.$ npm run delete-users
-
To confirm that there are no user accounts with conflicting names open the
mongosh
$ mongosh
test> use admin
test> show users
-
After deleting any existing user accounts, run the following script
$ npm run create-users
-
Once your user accounts are created, edit the
config
file 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.nwk
dbstats.json
clusters.json
and save your local copies undercovizu/data/
.- To import these data files into our
MongoDB
database run the following script$ npm run update-db1
- This will create our primary database and import the
JSON
/text
records 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:8001
to 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/data
and replace the files incovizu/data
- If your
node
server 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
mongo
CLI in non-authenticated mode$ mongosh
-
To open
mongo
CLI as anadmin
$ mongosh --username admin --authenticationDatabase admin
-
To view all users
- Connect as
admin
() use admin
show users
- Connect as
-
To delete an existing user
"myusername"
- Connect as
admin
use admin
show users
db.dropUser('myusername')
- Connect as
-
To view all collections within a database
"mydatabase"
- Connect as
admin
use mydatabase
show collections
- Connect as
-
To drop the database
"mydatabase"
- Connect as
admin
use mydatabase
db.dropDatabase()
- Connect as
-
To drop the collection
"mycollection"
with the database"mydatabase"
- Connect as
admin
use mydatabase
db.mycollection.drop()
- Connect as