注: Standalone 版仅适用于快速搭建、学习和测试,不能用于生产环境。
环境组件 | 版本 |
---|---|
Centos | 7.6.1810 |
MongoDB | 4.0.26 |
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.26.tgz
tar -zxvf mongodb-linux-x86_64-rhel70-4.0.26.tgz -C /opt
mv mongodb-linux-x86_64-rhel70-4.0.26/ mongodb4.0.26/
86 export MONGO_HOME=/opt/mongodb4.0.26
87 export PATH=$PATH:$MONGO_HOME/bin
mkdir -p /data/mongodb40/{log,data}
mkdir -p /opt/mongodb4.0.26/config
cd /opt/mongodb4.0.26/config/
vim mongo11111.conf
systemLog:
# 日志存储方式:file或syslog.指定为file时务必配置systemLog.path选项.不显示指定则为标准输出
destination: file
# 日志目录
path: /data/mongodb40/log/mongo40001.log
# true: 静默输出. 为方便日志跟踪,为false时将记录连接与关闭mongodb的日志.
quiet: true
# 日志是否追加,否则为覆盖
logAppend: true
# 日志使用时间戳重新命名老的日志文件(滚动输出到新的文件上,以防日志过大)
logRotate: rename
storage:
# 数据目录
dbPath: /data/mongodb40/data
# 此为存储引擎辅助机制,每次写入会记录一条操作日志(通过journal可以重放写入数据,如果出现宕机,重启时Wiredtriger会先将数据恢复至最近的检查点-checkpoint,然后重放后续的journal日志来一条条恢复数据)
journal:
enabled: true
# 以文件形式存储数据
directoryPerDB: true
# 设置存储引擎为 wiredTriger
wiredTiger:
engineConfig:
# mongo内存大小
configString: cache_size=1G
processManagement:
# 后台方式运行
fork: true
net:
# ip限制
bindIp: 0.0.0.0
# 端口
port: 40001
# 最大连接数
maxIncomingConnections: 1500
security:
# 开启安全认证
authorization: enabled
operationProfiling:
# 慢查询时间(毫秒)
slowOpThresholdMs: 1000
# 慢查询跟踪级别,数据字典:off:0,slowOp:1,all:2,全部可选性为:[0:关闭;1:记录慢查询(默认为>100毫秒);2:记录所有命令]
mode: slowOp
/opt/mongodb4.0.26/bin/mongod -f /opt/mongodb4.0.26/config/mongo11111.conf
curl http://localhost:11111
echo "/opt/mongodb4.0.26/bin/mongod -f /opt/mongodb4.0.26/config/mongo11111.conf" >> /etc/rc.local
mongo --port 11111
# 切换到admin库
use admin
# 创建管理员用户,设置其账户为(root/123456)
db.createUser({ user: "root", pwd: "123456", roles: [{role: "root", db: "admin"}] })
# 验证是否成功(返回1,即为成功)
db.auth("root", "123456")
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()
---
>
1). 使用数据库(没有则创建)
use testDB
2). 创建数据库管理用户并授权
注意: 在哪个库上执行这个操作,则表示该用户隶属于哪个库; 在登录时,需要在该库上做认证
# 创建用户(需要在切换的testDB上做认证)
db.createUser( {user: "testUser", pwd: "123456", roles: [{role: "dbOwner", db: "testDB"}]} )
测试登录:
mongo --host 127.0.0.1 --port 11111 -u testUser -p 123456 --authenticationDatabase testDB
use testDB
db.createUser( {user: "commonUser", pwd: "123456", roles: [{role: "readWrite", db: "testDB"}]} )
MongoDB中有一些内置的数据库, 新创建的数据库名称不能与这些数据库重名.
环境组件 | 版本 |
---|---|
admin | 属于权限数据库,添加用户到此数据库中,则用户将继承数据库的所有权限 |
config | 配置类信息 |
local | 数据库中的数据不会被复制 |
test | 默认数据库, 用来做一些测试工作 |
# 用户名/密码/认证服务器
mongo --host 127.0.0.1 --port 11111 -u testUser -p 123456 --authenticationDatabase testDB
# 如无则创建
use dbName
db.version()
db.getName()
db.getMongo()
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
}