$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 68818C72E52529D4
# Get Key$ sudo echo "deb http://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
# Create needed files$ sudo apt-get update
# Reload packages$ sudo apt-get install -y mongodb-org
# install MongoDB$ sudo mkdir -p /data/db
# Create the db files$ sudo chown -R `id -un` /data/db
# Give appropriate permissions to directories$ sudo systemctl start mongod
# Start mongod process$ sudo systemctl enable mongod
# Enable the process$ sudo netstat -plntu
# Have mongod start on boot$ sudo systemctl stop mongod
# Stop process$ mongod --port 27017
# Start process to ensure it's running on port 27017- Open a new tab, ssh into remote server to access mongo shell -->
$ mongo --port 27017
- (Now in the mongo shell)
> use admin
these commands come from authentication documentation # Access admin database > db.createUser({ user: "username", pwd: passwordPrompt(), roles: [{ role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]})
# Create superuser for all databases>db.adminCommand( { shutdown: 1 } )
# Stop mongod to change config>exit
# Leave mongo shell 17.$ sudo nano /etc/mongod.conf
# Open config file to enable auth- Uncomment the security config, and type
authorization: enabled
on line below. Match format to other config options. - Save (Ctl+X), Accept (y) and exit (Enter)
$ mongod --auth --port 27017
# Start mongod process with auth enabled (this would be in done in the tab that mongod was logging to in previous steps)$ mongo --port 27017 --authenticationDatabase "admin" -u "myUserAdmin" -p
# Access database as authenticated user>use [new database name]
# create a new database> db.createUser({ user: "username", pwd: passwordPrompt(), roles: [{ role: "readWrite", db:"dbName"}]})
# This is just an example from the documentation showing how one could create a user, for a new database, that didn't have as many permisssions as previous admin user.
The database needs a document (I believe) in order to be saved, so make sure a holder document is inserted until connection with the app can be confirmed. (>db.[collection name].insert({})
)