环境:
- cento8.0
- MongoDB5.0
- 创建一个repo文件
/etc/yum.repos.d/mongodb-org-5.0.repo
- 文件内容为:
[mongodb-org-5.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/5.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc
- 安装MongoDB
sudo yum install -y mongodb-org
或者,要安装特定版本的 MongoDB,请单独指定每个组件包并将版本号附加到包名称,如下例所示:
sudo yum install -y mongodb-org-5.0.2 mongodb-org-database-5.0.2 mongodb-org-server-5.0.2 mongodb-org-shell-5.0.2 mongodb-org-mongos-5.0.2 mongodb-org-tools-5.0.2
您可以指定任何可用的 MongoDB 版本。但是yum ,当新版本可用时升级包。为防止意外升级,请固定封装。要固定包,请将以下exclude指令添加到您的/etc/yum.conf文件中:
exclude=mongodb-org,mongodb-org-database,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools
MongoDB数据和日志目录:
- 以下是两个目录,对MongoDB数据库管理员来说非常重要。
/var/lib/mongo-数据目录(默认)
/var/log/mongodb-日志目录(默认)
- 我们可以通过在/etc/mongodb.conf文件中设置以下参数来自定义以上目录。
storage.dbPath-指定新的数据目录路径
systemLog.path-指定新的日志文件路径
启动 关闭 重启 开机自启动
- 启动MongoDB
systemctl start mongod
- 关闭MongoDB
systemctl stope mongod
- 默认开机启动
systemctl enable mongod
如果您在启动时收到类似于以下内容的错误
Failed to start mongod.service: Unit mongod.service not found.
首先运行以下命令:
sudo systemctl daemon-reload
然后再次运行上面的启动命令。
- 查看运行状态
#查看状态
systemctl status mongod
#查看端口信息
netstat -anplt| grep mongod
ss -anplt | grep mongod
[root@mongodb ~]# systemctl status mongod.service
● mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2020-12-18 23:17:49 CST; 16s ago
Docs: https://docs.mongodb.org/manual
Process: 930807 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
Process: 930804 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 930802 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 930800 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
Main PID: 930809 (mongod)
Memory: 57.7M
CGroup: /system.slice/mongod.service
└─930809 /usr/bin/mongod -f /etc/mongod.conf
Dec 18 23:17:48 mongodb.lianglab.cn systemd[1]: Starting MongoDB Database Server...
Dec 18 23:17:48 mongodb.lianglab.cn mongod[930807]: about to fork child process, waiting until server is ready for connections.
Dec 18 23:17:48 mongodb.lianglab.cn mongod[930807]: forked process: 930809
Dec 18 23:17:49 mongodb.lianglab.cn mongod[930807]: child process started successfully, parent exiting
Dec 18 23:17:49 mongodb.lianglab.cn systemd[1]: Started MongoDB Database Server
[root@mongodb ~]# netstat -anplt| grep mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 930809/mongod
[root@mongodb ~]# ss -anplt | grep mongod
LISTEN 0 128 127.0.0.1:27017 0.0.0.0:* users:(("mongod",pid=930809,fd=12))
创建超级用户
[root@localhost ~]# mongo
MongoDB shell version v4.4.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("2df728ce-2a97-4635-b9c5-81e0f00923c6") }
MongoDB server version: 4.4.6
---
The server generated these startup warnings when booting:
2021-06-29T20:42:30.610+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2021-06-29T20:42:30.611+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
---
---
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()
---
> use admin;
switched to db admin
> db.createUser(
... {
... user: "root",
... pwd: "你需要设置的密码",
... roles: [ { role: "root",db: "admin" } ]
... }
... )
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
> show users
{
"_id" : "admin.admin",
"userId" : UUID("91c06ceb-0dc4-4687-a467-679a9a8c75fa"),
"user" : "admin",
"db" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
> exit
bye
[root@localhost ~]#
开放外网访问 启用控制访问 自定义端口
MongoDB的默认配置文件在 /etc/mongod.conf,
安装完成后,默认是不需要用户认证即可连接
- 启用访问控制
设置security的authorization属性为enabled即可
security:
authorization: enabled
- 开启外网访问
修改net下的bindIpAll属性和ipv6属性 关闭bindIp属性
port属性可以自定义端口
net:
port: 4444
#bindIp: 127.0.0.1
bindIpAll: true
ipv6: true
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: D:\MongoDB\Server\5.0\data
journal:
enabled: true
# engine:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: D:\MongoDB\Server\5.0\log\mongod.log
# network interfaces
net:
port: 4444
#bindIp: 127.0.0.1
bindIpAll: true
ipv6: true
#processManagement:
#security: 启用访问控制
security:
authorization: enabled
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp: