1. 环境准备
1.1 环境说明
CentOS7 64-bit
| No | 主机名 | 域名 | 作用 | IP |
|---|---|---|---|---|
| 1 | mongodb | mongodb.local.com | 数据库 版本4.4 | 192.168.8.171 |
2 环境安装
2.1 hostname
# hostnamectl set-hostname mongodb --static
2.2 网络设置
$ vi /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO="static" #dhcp改为static
ONBOOT="yes" #开机启用本配置
IPADDR=192.168.8.171 #静态IP
GATEWAY=192.168.8.2 #默认网关
NETMASK=255.255.255.0 #子网掩码
DNS1=114.114.114.114 #DNS 配置
DNS2=8.8.8.8 #DNS 配置
$ service network restart
2.3 hosts
$ echo "192.168.8.171 mongodb.local.com" >> /etc/hosts
$ reboot
2.4 防火墙端口设置
$ firewall-cmd --zone=public --add-port=27017/tcp --permanent
success
$ firewall-cmd --reload
success
$ firewall-cmd --zone=public --list-ports
27017/tcp
3 安装社区版mongodb
3.1 配置包管理系统(yum)
cat << 'EOF' > /etc/yum.repos.d/mongodb-org-4.4.repo
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
EOF
3.2 安装MongoDB包
$ sudo yum install -y mongodb-org
.....
Installed:
mongodb-org.x86_64 0:4.4.4-1.el7
Dependency Installed:
cyrus-sasl.x86_64 0:2.1.26-23.el7
cyrus-sasl-gssapi.x86_64 0:2.1.26-23.el7
cyrus-sasl-plain.x86_64 0:2.1.26-23.el7
mongodb-database-tools.x86_64 0:100.3.1-1
mongodb-org-database-tools-extra.x86_64 0:4.4.4-1.el7
mongodb-org-mongos.x86_64 0:4.4.4-1.el7
mongodb-org-server.x86_64 0:4.4.4-1.el7
mongodb-org-shell.x86_64 0:4.4.4-1.el7
mongodb-org-tools.x86_64 0:4.4.4-1.el7
Complete!
[root@mongodb ~]#
4 运行MongoDB社区版
4.1 启动MongoDB
通过发出以下命令来启动mongod进程:
$ sudo service mongod start
Redirecting to /bin/systemctl start mongod.service
4.2 验证MongoDB是否已成功启动
通过检查/var/log/mongodb/mongod.log中日志文件的内容来检查mongod进程是否已成功启动以获取行读取
$ cat /var/log/mongodb/mongod.log
"msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}
其中port是/etc/mongod.conf中配置的端口,默认为27017。
可以选择通过发出以下命令来确保MongoDB在系统重新启动后启动:
$ sudo chkconfig mongod on
Note: Forwarding request to 'systemctl enable mongod.service'.
4.3 停止MongoDB
根据需要,还可以通过发出以下命令来停止mongod进程:
$ sudo service mongod stop
Redirecting to /bin/systemctl stop mongod.service
4.4 重启MongoDB
可以通过发出以下命令重新启动mongod进程:
$ sudo service mongod restart
Redirecting to /bin/systemctl restart mongod.service
通过观察/var/log/mongodb/mongod.log文件中的输出来跟踪错误或重要消息的进程状态。
5 开始使用MongoDB
在与mongod相同的主机上启动mongo shell。您可以在没有任何命令行选项的情况下运行mongo shell,以使用默认端口27017连接到localhost上运行的mongod:
$ mongo
mongo
MongoDB shell version v4.4.4
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("534df4ef-9854-4185-8113-5485f36b9fab") }
MongoDB server version: 4.4.4
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
---
The server generated these startup warnings when booting:
2021-03-27T21:04:12.548+09:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2021-03-27T21:04:12.549+09:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
2021-03-27T21:04:12.549+09:00: /sys/kernel/mm/transparent_hugepage/defrag 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()
---
>
5.1 为MongoDB添加admin管理员
接上继续
> use admin
switched to db admin
> db.createUser({
... user: 'admin',
... pwd: 'admin',
... roles:[{role: 'root',db: 'admin'},{role: 'userAdminAnyDatabase',db: 'admin'}]
... })
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
},
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
db.createUser({
... ... user: "order",
... ... pwd: "order",
... ... roles:[{role: 'dbOwner',db: 'order-database'}]
... ... })
Successfully added user: {
"user" : "order",
"roles" : [
{
"role" : "dbOwner",
"db" : "order-database"
}
]
}
注意:上面除了建立超级用户外,还要建立一个order用户
5.2 修改mongod.conf配置文件
权限以及远程访问控制配置
network interfaces
net:
security:
$ vi /etc/mongod.conf
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
security:
authorization: "enabled"
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
<ESC>:wq
$ service mongod restart
Redirecting to /bin/systemctl restart mongod.service
5.3 为order-database数据库添加用户
注意记得顺序:切换到admin库,验证后再往下进行
在5.1 的用户order用来操作order-database数据库的
用户删除用户的话
> db.dropUser("order")
true
权限说明:
数据库用户角色
- read: 只读数据权限
- readWrite:学些数据权限
数据库管理角色
- dbAdmin: 在当前db中执行管理操作的权限
- dbOwner: 在当前db中执行任意操作
- userADmin: 在当前db中管理user的权限
备份和还原角色
- backup
- restore
夸库角色
- readAnyDatabase: 在所有数据库上都有读取数据的权限
- readWriteAnyDatabase: 在所有数据库上都有读写数据的权限
- userAdminAnyDatabase: 在所有数据库上都有管理user的权限
- dbAdminAnyDatabase: 管理所有数据库的权限
集群管理
- clusterAdmin: 管理机器的最高权限
- clusterManager: 管理和监控集群的权限
- clusterMonitor: 监控集群的权限
- hostManager: 管理Server
超级权限
- root: 超级用户
参考:
Install MongoDB Community Edition on Red Hat or CentOS — MongoDB Manual
在Linux上安装MongoDB社区版 - 简书 (jianshu.com)
MongoDB创建数据库和用户 - 简书 (jianshu.com)
[mongodb远程连接配置 - 今孝 - 博客园 (cnblogs.com)](