准备
groupadd mongodb useradd -g mongodb mongodb echo password |passwd --stdin mongodb mkdir -pv /data/apps/mongodb cd /data/apps/mongodb mkdir -p bin conf log data
下载
wget https://fastdl.mongodb.org/linux/{{version}}.tgz # {{version}} 指版本
安装
tar xf {{version}}.tgz cd {{version}}/bin cp * /data/apps/mongodb/bin chown -R mongodb:mongodb /data/apps/mongodb/ su - mongodb touch .bash_profile cat >> .bash_profile <<'EOF' export PATH=/data/apps/mongodb/bin:$PATH EOF source .bash_profile
配置
vim mongodb.yaml
vim mongodb.yaml systemLog: destination: file path: "/data/apps/mongodb/log/mongodb.log" logAppend: true storage: journal: enabled: true dbPath: "/data/apps/mongodb/data" net: port: 27017 bindIp: "0.0.0.0" #security: # authorization: "enabled"
后台启动
mongod -f mongodb.yaml &
登陆数据库
mongo #设置账户和权限 > use admin ###########使用admin权限 # 添加超级用户root 密码password roles权限 > db.createUser({user:'root',pwd:password,roles:['root']}); > db.auth("root","mongodb") # 测试账户合规性 1 > # 添加库 use dbName #添加账户 db.createUser({user:'user',pwd:'password',roles:[{role:'readWrite', db:'dbname'}]}); db.auth("user","password") #关闭: mongod -f mongodb.yaml --shutdown
配置
vim mongofb.yaml
vim mongodb.yaml systemLog: destination: file path: "/data/apps/mongodb/log/mongodb.log" logAppend: true storage: journal: enabled: true dbPath: "/data/apps/mongodb/data" net: port: 27017 bindIp: "0.0.0.0" security: authorization: "enabled"
# 后台启动
mongod -f mongodb.yaml &
roles的可选项有:
1. 数据库用户角色:read、readWrite; 2. 数据库管理角色:dbAdmin、dbOwner、userAdmin; 3. 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager; 4. 备份恢复角色:backup、restore; 5. 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase 6. 超级用户角色:root