包下载:https://www.mongodb.com/download-center/community

配置参数:https://docs.mongodb.com/manual/reference/configuration-options/

本文使用的配置来源:https://github.com/mongodb/mongo/blob/master/rpm/mongod.conf

安装

1
2
3
4
5
6
7
8
9
10
11
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.10.tgz
tar zxf mongodb-linux-x86_64-4.0.10.tgz
mv mongodb-linux-x86_64-4.0.10 /opt
mkdir /var/log/mongodb
mkdir /var/lib/mongo

groupadd mongod
useradd -M mongod -s /sbin/nologin -g mongod

chown -R mongod:mongod /var/lib/mongo
chown -R mongod:mongod /var/log/mongodb

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
mkdir /opt/mongodb-linux-x86_64-4.0.10/conf
cat /opt/mongodb-linux-x86_64-4.0.10/conf/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:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

启动 关闭

1
2
3
4
5
添加变量
echo 'export PATH=/opt/mongodb-linux-x86_64-4.0.10/bin:$PATH' >> /etc/profile
source /etc/profile

mongod -f /opt/mongodb-linux-x86_64-4.0.10/conf/mongod.conf
1
2
3
4
关闭
mongo --host 127.0.0.1 --port 27017
use admin
db.shutdownServer()

警告处理

1
2
3
4
5
6
7
8
9
10
11
Q:WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
A:echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled

Q:You are running this process as the root user, which is not recommended
A:su - mongod -c "/opt/mongodb-linux-x86_64-4.0.10/bin/mongod -f /opt/mongodb-linux-x86_64-4.0.10/conf/mongod.conf"

Q:Access control is not enabled for the database Read and write access to data and configuration is unrestricted
A:需要创建用户,根据下方创建用户解决

Q:soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files
A:根据下方修改limits限制
1
2
3
4
5
cat /etc/security/limits.conf
mongod hard nofile 64000
mongod soft nofile 64000
mongod soft nproc 64000
mongod hard nproc 64000

需要注意

启动报错时将 –fork 关闭 可查看输出内容进行排错

注意文件权限,以及目录权限

用户权限

mongodb 中 角色role 解释

官方文档

简书 kimoCHG

创建用户

1
2
3
mongo --port 27017
use admin;
db.createUser({user: "jianyugong",pwd: "jianyu9808",roles: [{role: "userAdminAnyDatabase", db: "admin"}]})

翻译过来为 “创建一个对admin这个数据库有userAdminAnyDatabase权限,用户名 jianyugong,密码 jianyu980”

更改配置文件

1
2
3
4
vim /opt/mongodb-linux-x86_64-4.0.10/conf/mongod.conf
添加如下,并重启
security:
authorization: enabled

登陆验证

1
2
3
4
5
6
7
8
9
10
mongo --port 27017
use admin
db.auth("jianyugong","jianyu9808")

出现1即为成功

show dbs 进行验证
use jianyugong 创建数据库
db 可以查看刚刚创建数据库
db.jianyugong.insert({"name":"jianyugong"}) mongodb新创建数据库需要插入数据才会显示