MongoDB Getting Started
MongoDB is a scalable, high-performance, open source NoSQL database written in C++.MongoDB, Inc. is the company behind the database for GIANT ideas, offering the best of traditional databases as well as the flexibility, scale and performance today’s applications require. We build MongoDB and the drivers, offer software and services, run MongoDB University (which has trained over 350,000 engineers in MongoDB), and sponsor the MongoDB community.
If you have installed Websoft9 MongoDB, the following steps is for your quick start
Preparation
- Get the Internet IP of your Server on Cloud
- Check your Inbound of Security Group Rule of Cloud Console to ensure the TCP:27017,9091 is allowed
- Complete Five steps for Domain if you want to use Domain for MongoDB
- Get default username and password of MongoDB
MongoDB Initialization
Steps for you
You should verify the MongoDB when completed deployment:
Check MongoDB
- Use the SSH to connect Server, and run the command below to view the installation information and running status
cd /data/apps/mongodb && sudo docker compose ls
- You can ge the message from SSH " STATUS: running(1) " when MongoDB is running
Connect MongoDB
-
Use the SSH to connect Server, and run MongoDB shell (Don't have password?)
$ docker exec -it mongodb mongo admin -u root -p YOURPASSWORD
MongoDB shell version v5.0.10
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
{"t":{"$date":"2022-08-10T03:05:34.194Z"},"s":"I", "c":"NETWORK", "id":5693100, "ctx":"js","msg":"Asio socket.set_option failed with std::system_error","attr":{"note":"connect (sync) TCP fast open","option":{"level":6,"name":30,"data":"01 00 00 00"},"error":{"what":"set_option: Protocol not available","message":"Protocol not available","category":"asio.system","value":92}}}
Implicit session: session { "id" : UUID("030a4e0b-54cf-4f93-aa90-792b10c478f7") }
MongoDB server version: 5.0.10
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
> -
List all databases and users
# list all databases
show dbs
# use admin, and list all users
use admin
show users
Having trouble?
Below is for you to solve problem, and you can contact Websoft9 Support or refer to Troubleshoot + FAQ to get more.
Does MongoDB enable account authentication by default?
YES, Mongodb authentication is enabled by default.
MongoDB QuickStart
To learn more about the use of mongodb, refer to the official document MongoDB Administration
MongoDB Setup
Enable MongoDB remote connection
-
Use SSH to connect MongoDB server and modify the MongoDB configuration file
#1 set authorization **disabled** to **enabled**
security:
authorization: enabled
#2 set bindIP to 0.0.0.0
net:
port: 27017
bindIp: 0.0.0.00.0.0.0 means any Internet IP can connect your MongoDB
-
Restart MongoDB service
sudo docker restart mongodb
-
Go to the Cloud Console and enable the TCP:27017 port of Security Group
Close MongoDB access authentication
Mongodb authentication is enabled by default, and can be closed according to the following process:
-
Edit MongoDB configuration file, comment out the environment variable user and password.
services:
mongo:
image: mongo:${APP_VERSION}
restart: always
container_name: ${APP_NAME}
ports:
- ${APP_MONGO_PORT}:27017
#environment:
# MONGO_INITDB_ROOT_USERNAME: ${APP_USER}
# MONGO_INITDB_ROOT_PASSWORD: ${APP_PASSWORD} -
Recreate MongoDB container
cd /data/apps/mongodb
sudo docker compose up -d
MongoDB web GUI
Now, we will introduce how to use MongoDB compass
Preparation
- Open the access authentication of MongoDB
- Check your Inbound of Security Group Rule of Cloud Console to ensure the TCP:9091 is allowed
When completed the preparation, you can use the GUI now
- Open Chrome or Firefox on your local PC to visit URL http://Internet IP:9091, follow the prompts to enter the user name and password(Don't have password?)
- Click the MongoDB compass icon on the web desktop to enter MongoDB compass
- Fill in the correct items and connect MongoDB
# example connect string
mongodb://root:1cTFecwTEs@mongodb:27017 - Go go MongoDB Compass console when connect successfully
Planning data model
MongoDB as a kind of database, it is also similar to the traditional RDBMS, that is, planning the data model and establishing the database paradigm.
Only in this way can the performance of the database be better developed.
The main design points of data planning include:
- Use data paradigm
- Using embedded document anti paradigm
- Use fixed sets
- Consider document enlargement
- Planning indexing, sharding, and replication
- Planning data lifecycle
Useful MongoDB Command
show all database, create database, insert data
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
-------------------------
#2 create database test, if there have test, it's means switch to test, example
> use test
switched to db test
# show the current database
> db
test
# show the current database users
> show users
-------------------------
#3 Insert data into the database test, example
> db.test.insert({"name":"company"})
WriteResult({ "nInserted" : 1 })
-------------------------
Delete the database
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
websoft9 0.000GB
> use test
switched to db test
> use test
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
websoft9 0.000GB
Create administrator user
> mongo
> use admin
switched to db admin
> db.createUser( { user: "webs_admin", pwd: "websoft9", roles: ["userAdminAnyDatabase"] } )
Successfully added user: { "user" : "webs_admin", "roles" : [ "userAdminAnyDatabase" ] }
# Show account number
> show users
{
"_id" : "admin.webs_admin",
"user" : "webs_admin",
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
Password management
Modify password
You can modify the password of root user which added on your MongoDB by the following command
$ docker exec -it mongodb mongo admin -u root -p YOURPASSWORD
MongoDB shell version v4.0.18
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
> db = db.getSiblingDB('admin')
admin
> db.changeUserPassword("root", "NEWPASSWORD")
> exit
Reset password
Reset password is the process of resetting a new password through special solutions in case the password has been forgotten.
-
Edit MongoDB configuration file, comment out the environment variable user and password.
services:
mongo:
image: mongo:${APP_VERSION}
restart: always
container_name: ${APP_NAME}
ports:
- ${APP_MONGO_PORT}:27017
#environment:
# MONGO_INITDB_ROOT_USERNAME: ${APP_USER}
# MONGO_INITDB_ROOT_PASSWORD: ${APP_PASSWORD} -
Recreate MongoDB container
cd /data/apps/mongodb
sudo docker compose up -d -
Run the MongoDB command to set new password
$ docker exec -it mongodb nongo
> db = db.getSiblingDB('admin')
admin
> db.changeUserPassword("root", "NEWPASSWORD") -
Edit MongoDB configuration file, make the environment variable user and password effective
services:
mongo:
image: mongo:${APP_VERSION}
restart: always
container_name: ${APP_NAME}
ports:
- ${APP_MONGO_PORT}:27017
environment:
MONGO_INITDB_ROOT_USERNAME: ${APP_USER}
MONGO_INITDB_ROOT_PASSWORD: ${APP_PASSWORD} -
Recreate MongoDB container, and the new password will take effect immediately
cd /data/apps/mongodb
sudo docker compose up -d