警告
本文最后更新于 2021-02-22 12:01,文中内容可能已过时。
下载地址:
http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel70-v3.6-latest.tgz
安装:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| mkdir /application
tar xf mongodb-linux-x86_64-rhel70-v3.6-latest.tgz -C /application/
cd /application
mv mongodb-linux-x86_64-rhel70-3.6.16-15-g4cd114f/ mongodb
echo 'export PATH=$PATH:/application/mongodb/bin' >>/etc/profile
source /etc/profile
# 创建用户
groupadd mongod
useradd -g mongod mongod
echo 123456 |passwd --stdin mongod
#创建目录
cd /application/mongodb
mkdir conf log data
chown -R mongod.mongod /application/mongodb
|
关闭大页内存
1
2
| echo 'never' >> /sys/kernel/mm/transparent_hugepage/enabled
echo 'never' >> /sys/kernel/mm/transparent_hugepage/defrag
|
编写配置文件
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
| # 系统日志相关
systemLog:
destination: file
logAppend: true
path: /application/mongodb/log/mongod.log
# 数据存储相关
storage:
dbPath: /application/mongodb/data
journal:
enabled: true
# 网络相关
net:
port: 27017
bindIp: 0.0.0.0
# 进程控制相关
processManagement:
fork: true
# pidFilePath: /var/run/mongod.pid
# 安全配置
security:
authorization: enabled
|
启动mongodb
1
2
3
4
5
6
7
| su - mongod
#无配置文件启动
mongod --dbpath=/application/mongodb/data --logpath=/application/mongodb/log/mongodb.log --logappend --fork
#使用配置文件启动
su - mongod
mongod -f /application/mongodb/conf/mongo.conf
|
启动脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| [Unit]
Description=Mongos server
Wants=network.target
After=network.target
[Service]
Type=forking
PIDFile=/data/mongodb/mongod.pid
ExecStart=/application/mongodb/bin/mongod -f /usr/local/mongodb/etc/mongod.conf
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
User=mongod
Group=mongod
LimitNOFILE=65534
LimitNPROC=65534
[Install]
WantedBy=multi-user.target
|
创建mongodb超级管理用户:
1
2
3
4
5
6
7
8
| use admin
db.createUser(
{
user:"root",
pwd:"123",
roles:[ { role: "root", db:"admin" } ]
}
)
|
连接数据库
1
2
3
4
5
| mongo -uroot -p123 10.0.0.40/admin
或者
mongo
use admin
db.auth('root','123')
|
创建普通读写用户:
1
2
3
4
5
6
7
8
| use app
db.createUser(
{
user: "app1",
pwd: "app1",
roles: [ { role: "readWrite", db: "app" } ]
}
)
|
关闭数据库
1
| mongod -f conf/mongo.conf --shutdown
|