1.安裝MongoDB
下載地址 https://www.mongodb.com/download-center/community
下載server和 shell兩個包
以centos6 為示例
wget https://repo.mongodb.org/yum/redhat/6/mongodb-org/4.0/x86_64/RPMS/mongodb-org-server-4.0.10-1.el6.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/6/mongodb-org/4.0/x86_64/RPMS/mongodb-org-shell-4.0.10-1.el6.x86_64.rpm
下載完成使用 npm -ivh 包名
安裝
或者直接用yum install
yum install https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/RPMS/mongodb-org-server-4.2.0-1.el7.x86_64.rpm
yum install https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/RPMS/mongodb-org-shell-4.2.0-1.el7.x86_64.rpm
修改外網(wǎng)配置
vim /etc/mongod.conf
net:
port: 27017
bindIp: 0.0.0.0
初始配置
1.配置用戶名密碼
啟動mongod服務(wù)
/etc/init.d/mongod start
打開客戶端 mongo
mongo --host localhost --port 27017
use admin
db.createUser(
{
user: "myUserAdmin",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)
4.2版本以下需把
passwordPrompt()
函數(shù)換成字符串格式密碼如"password"
創(chuàng)建完成 重啟MongoDB 然后開啟認(rèn)證
mongod --auth --port 27017 --dbpath /var/lib/mongodb
或者修改配置文件
vim /etc/mongd.conf
添加以下項
security:
authorization: "enabled"
shell 登錄方式一
mongo --port 27017 --authenticationDatabase "admin" -u "username" -p
shell 登錄方式二
mongo --port 27017
打開mongo shell
use dbname
選擇數(shù)據(jù)庫
db.auth("username","passsword")
進(jìn)行密碼認(rèn)證