cd /usr/local/src/
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.7.1.tgz
tar -zxvf mongodb-linux-x86_64-3.7.1.tgz
mv mongodb-linux-x86_64-3.7.1 /usr/local/mongodb
echo "PATH=/usr/local/mongodb/bin:$PATH" >> /etc/profile
source /etc/profile
groupadd mongo
useradd -rg mongo mongo
mkdir -p /data/db
chown -R mongo mongo /usr/local/mongodb
cat >> /usr/local/mongodb/bin/mongodb.conf <<EOF
fork = true
port = 27017
bind_ip = 0.0.0.0
dbpath = /data/db
logpath = /data/db/mongodb.log
EOF
##启动
mongod --config /usr/local/mongodb/bin/mongodb.conf
> use admin
switched to db admin
> db.createUser({user:"admin",pwd:"password",roles:["root"]})
Successfully added user: { "user" : "admin", "roles" : [ "root" ] }
> db.getCollectionNames()
[ "system.users", "system.version" ]
echo auth = true >> /usr/local/mongodb/bin/mongodb.conf ##重启mongodb
##登录账号密码
> db.auth("admin", "password")
#创建库
> use autoops
> db.createUser({user: "autoops", pwd: "autoops", roles: [{ role: "dbOwner", db: "autoops" }]})
Successfully added user: {
"user" : "autoops",
"roles" : [
{
"role" : "dbOwner",
"db" : "autoops"
}
]
}
mongo -u admin -p password 127.0.0.1:27017/admin
本文转自 295631788 51CTO博客,原文链接:http://blog.51cto.com/hequan/2061444,如需转载请自行联系原作者