Linux下搭建MongoDB
smartpotato 超膜裁形
本文距离上次更新已过去 0 天,部分内容可能已经过时,请注意甄别。

官网下载

https://www.mongodb.com/try/download/community

配置环境变量

1
export MONGODB_HOME=/usr/local/MongoDB/mongodbServer export PATH=$PATH:$MONGODB_HOME/bin

创建conf文件

1
cd /usr/local/MongoDB/mongodbServer/bin vim mongod.conf

写入配置

1
2
3
4
5
6
7
8
9
10
11
storage:
dbPath: "/usr/local/MongoDB/data"
systemLog:
destination: file
path: "/usr/local/MongoDB/log/mongod.log"
logAppend: true
net:
port: 27017
bindIpAll: true
processManagement:
fork: true

启动

1
mongod --config /usr/local/MongoDB/mongodbServer/bin/mongod.conf

添加管理用户

1
2
3
4
5
6
mongo	//进入数据库
use admin // 进入 admin 数据库

db.createUser({ user: 'name', pwd: 'password', roles: [{ role: 'dbOwner', db: 'simple-design' }] })
// 为名为 simple-design 的数据库添加数据库管理员,账号为 name,密码为 password, 角色权限为 dbOwner

常用角色

1
2
3
4
5
6
7
8
9
常用 mongoDB 角色

数据库用户角色: read、 readWrite
数据库管理角色:dbAdmin、dbOwner、userAdmin
集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager
备份恢复角色:backup、restore
所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
超级用户角色:root
内部角色: __system

设置MongoDB服务为系统服务

1
vim /etc/init.d/mongod

//写入配置

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
!/bin/bash

start() {
/usr/local/MongoDB/mongodbServer/bin/mongod --config /usr/local/MongoDB/mongodbServer/bin/mongod.conf
}

stop() {
/usr/local/MongoDB/mongodbServer/bin/mongod --config /usr/local/MongoDB/mongodbServer/bin/mongod.conf --shutdown
}
case "$1" in
start)
start
;;

stop)
stop
;;

restart)
stop
start
;;
*)
echo
$"Usage: $0 {start|stop|restart}"
exit 1
esac

添加权限

1
2
3
4
5
chmod +x /etc/init.d/mongod
systemctl unmask mongod.service

启动
service mongod start

查看当前所有启动的服务 service –status-all

 请作者喝咖啡