1. Environmental description
Environment components | Version |
---|---|
hundred | 7.6.1810 |
MongoDB | 4.0.26 |
2. Installation steps
2.1 Download MongoDB binary version
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.26.tgz
2.2 Unzip to opt
tar -zxvf mongodb-linux-x86_64-rhel70-4.0.26.tgz -C /opt
2.3 Renaming a folder
mv mongodb-linux-x86_64-rhel70-4.0.26/ mongodb4.0.26/
2.4 Add to environment variables
A). vim /etc/profile
86 export MONGO_HOME=/opt/mongodb4.0.26
87 export PATH=$PATH:$MONGO_HOME/bin
B). source /etc/profile
2.5 Create the runtime data directory
A). Create "data+log" directory
mkdir -p /data/mongodb40/{
log,data}
B). Create a configuration file directory
mkdir -p /opt/mongodb4.0.26/config
### C). 创建配置文件
```bash
cd /opt/mongodb4.0.26/config/
vim mongo11111.conf
2.6 Start MongoDB
/opt/mongodb4.0.26/bin/mongod -f /opt/mongodb4.0.26/config/mongo11111.conf
2.7 Test whether the startup is successful
curl http://localhost:11111
2.8 Add boot auto-start
echo “/opt/mongodb4.0.26/bin/mongod -f /opt/mongodb4.0.26/config/mongo11111.conf” >> /etc/rc.local
3. Manage users
3.1 Log in to MongoDB for the first time
mongo --port 11111
3.2 Create admin user (root)
# 切换到admin库
use admin
# 创建管理员用户,设置其账户为(root/123456)
db.createUser({
user: "root", pwd: "123456", roles: [{
role: "root", db: "admin"}] })
# 验证是否成功(返回1,即为成功)
db.auth("root", "123456")
3.3 Login with admin user
mongo --port 11111 -uroot -p123456
MongoDB shell version v4.0.26
connecting to: mongodb://127.0.0.1:11111/?gssapiServiceName=mongodb
Implicit session: session {
"id" : UUID("965b8e97-7d57-4beb-9b61-4f53a57d169e") }
MongoDB server version: 4.0.26
Server has startup warnings:
2022-04-19T22:34:01.665+0800 I STORAGE [initandlisten]
2022-04-19T22:34:01.665+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2022-04-19T22:34:01.665+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2022-04-19T22:34:02.212+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2022-04-19T22:34:02.212+0800 I CONTROL [initandlisten]
2022-04-19T22:34:02.212+0800 I CONTROL [initandlisten]
2022-04-19T22:34:02.212+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2022-04-19T22:34:02.212+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2022-04-19T22:34:02.212+0800 I CONTROL [initandlisten]
2022-04-19T22:34:02.212+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2022-04-19T22:34:02.212+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2022-04-19T22:34:02.212+0800 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>
3.4 Create "Database + User"
1). Use the database (create it if you don't have one)
use testDB
2). Create a database management user and authorize it.
Note: On which library is this operation performed, it indicates which library the user belongs to; when logging in, authentication is required on the library
# 创建用户(需要在切换的testDB上做认证)
db.createUser( {
user: "testUser", pwd: "123456", roles: [{
role: "dbOwner", db: "testDB"}]} )
Test login:
mongo --host 127.0.0.1 --port 11111 -u testUser -p 123456 --authenticationDatabase testDB
- Create a normal user for the database and set up authorization
use testDB
db.createUser( {
user: "commonUser", pwd: "123456", roles: [{
role: "readWrite", db: "testDB"}]} )
Fourth, MongoDB basic commands
4.1 Built-in database
There are some built-in databases in MongoDB, and the newly created database name cannot have the same name as these databases.
Environment components | Version |
---|---|
admin | It belongs to the privilege database, if you add a user to this database, the user will inherit all privileges of the database |
config | Configuration class information |
local | Data in the database will not be replicated |
test | Default database, used to do some testing work |
4.2 Common Commands
- login to mongo
# 用户名/密码/认证服务器
mongo --host 127.0.0.1 --port 11111 -u testUser -p 123456 --authenticationDatabase testDB
- switch or use a database
# 如无则创建
use dbName
- View database version
db.version()
- View the current database name
db.getName()
- View current database address
db.getMongo()
- View current database status
db.stats()
{
"db" : "testDB",
"collections" : 0,
"views" : 0,
"objects" : 0,
"avgObjSize" : 0,
"dataSize" : 0,
"storageSize" : 0,
"numExtents" : 0,
"indexes" : 0,
"indexSize" : 0,
"fileSize" : 0,
"fsUsedSize" : 0,
"fsTotalSize" : 0,
"ok" : 1
}