Install MongoDB on ubuntu server and setup remote connection
Step-01 Installing MongoDB: To obtain the most recent version of this software you must include MongoDB dedicated package repository to your API sources, the you will be able to install mongodb-org
curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
This command will return ok if the key was added successfully.
Then run the following command, which creates a file in the sources.list.d named mongodb-org-4.4.list
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
After running this command update servers's local package
sudo apt update
Step-02 starting MongoDB service:
sudo systemctl start mongod.service
then check service status by flowing command
sudo systemctl status mongod
The command will return output like the flowing code
sudo systemctl enable mongo
now you can see connection status by flowing command
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
The command will return the status of the database connection
Step-03 secure MongoDB:
First of all, adding an administrative user.to add an administrative user you must first connect to the mongo shell by flowing command
mongo
then you will see the mongo shell prompt
than run
show dbs
then you will return the below output
now you have to add an administrative user that's why you must first connect to the admin database by flowing command
use admin
then you will return the below output
switched to db admin
than creating administrative user by flowing command
db.createUser({user:"shohedul",pwd:"*******",roles:[{role:"userAdminAnyDatabase", db:"admin"}]})
than you will return
Enabling authentication: to enable authentication you must edit mongo.conf
open the configuration file by the following command
sudo nano /etc/mongod.conf
scroll down and comment-out security section line and then add the authorization parameter and set it to enable
After adding save and closing the file by flowing command CTRL + X then Y and then press Enter.
Then restart the daemon by flowing command
sudo systemctl restart mongod
now check the status of the services to make sure that it restarted correctly
sudo systemctl status mongod
Testing authenticate
mongo
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("04bf1b65-1568-4104-bb24-b628961a9da8") }
MongoDB server version: 4.4.10
config 0.000GB
local 0.000GB
net:
port: 27017
bindIp: 0.0.0.0
Then restart the daemon by flowing command
sudo systemctl restart mongod
if everything is ok then return the output below
Connection to server_ip 27017 port [tcp/*] succeeded!
Now your connection string URL, like this
mongodb://username:password@server_ip:27017
Now you can manage your database remotely.
No comments