Firstly login to your VPS server using ip address and vps password.
Or if you created user then login using your created user. If you don't know how to do click here to know how to login.
And also done the installtion of mongoDB database on VPS using simple steps, Click here to know.
Let's start to create the user and database in MongoDB using simple steps:
Just copy and paste the commands.
Connect to MongoDB shell:
sudo mongosh
Note : If it asks for password, enter the password. it ask only for one time. The password is of root user passsword.
You getting this output:
Show database:
show dbs
Change to admin database:
use admin
the output looks like this:
test> show dbs
admin 40.00 KiB
config 60.00 KiB
local 40.00 KiB
test> use admin
switched to db admin
admin>
Create superuser with all privileges:
db.createUser({user: "username-here" , pwd: passwordPrompt() , roles: ["root"]})
In my case i will do:
db.createUser({user: "eagleye" , pwd: passwordPrompt() , roles: ["root"]})
It asks to create password. Enter a new password you want to set.
Now exit Mongosh shell:
.exit
Let's bindup the MongoDB with the IP address. So that we easily connect database with project.
Open the mongod file:
sudo nano /etc/mongod.conf
Now find this:
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
and change this to:
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
after change this,
Now press Ctrl + x, then press y and hit Enter. This is for saving the changes we did in file.
Restart MongoDB:
sudo service mongod restart
Now Create the database in MongoDB:
Access Mongo Shell as Super User use this command:
sudo mongosh --port 27017 --authenticationDatabase "admin" -u "username-here" -p "password-here"
In my case i will run:
sudo mongosh --port 27017 --authenticationDatabase "admin" -u "eagleye" -p "Learnhosting@123"
Create Database & User for project:
use database_name
I will host Todo project so let's make database name simple, So i use:
use todoProjectdb
Create a simple user with some root permission that is connected with this database.
db.createUser({user:"new_username_here", pwd:passwordPrompt(), roles:[{role:"readWrite", db:"database_name"}]})
In my case:
db.createUser({user:"todomaker", pwd:passwordPrompt(), roles:[{role:"readWrite", db:"todoProjectdb"}]})
It asks for to createing a new password. i set password : todomaker@123
Now Exit mongosh shell:
.exit
MongoDB URI to Connect with projects is:
mongodb://username-here:[email protected]:27017/database_name
If I use my configurations as i set in this complete tutorial. Then MongoDB URI becomes:
Username : todomaker
Password : todomaker%40123 # because @ symbol replace by %40 when we concider this in password.
Database Name : todoProjectdb
mongodb://todomaker:todomaker%[email protected]:27017/todoProjectdb
Now use this in you react project.
Tags:
Leave a comment
You must be logged in to post a comment.