3.4版本的mongodb配置文件为/etc/mongod.conf,格式是yaml。点击查看官方文档
安装
1.引入公钥
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
2.创建列表文件
Ubuntu 12.04
echo "deb [ arch=amd64 ] http://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
Ubuntu 14.04
echo "deb [ arch=amd64 ] http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
Ubuntu 16.04
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
3.更新包资源
sudo apt-get update
4.安装mongodb
sudo apt-get install -y mongodb-org
设置远程连接
在配置文件中将
bindIp: 127.0.0.1
注释掉(即在句首加#
)
#bindIp: 127.0.0.1
重启数据库
sudo service mongod restart
开启用户验证
1.添加用户
启动mongodb:
mongod
启动mongodb shell
mongo
创建用户
use admin
db.createUser(
{
user: "admin",
pwd: "123456",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]</code>
}
)
2.更改配置文件
在配置文件中将
#security:
中的#去掉,并在下一行,输入两个空格,加上authorization: enabled
,如下
security:
authorization: enabled
3.更改配置文件
sudo service mongod restart
创建其他数据库并且添加用户
use ceadeal
db.createUser({
user: "ceadeal",
pwd: "123456",
roles: [
{ role: "userAdmin", db: "ceadeal" },
{ role: "readWrite", db: "ceadeal" },
{ role: "dbAdmin", db: "ceadeal" }
]
});