Ubuntu14.0.4 安裝mongodb數(shù)據(jù)庫

運行版本:v3.4.3

導(dǎo)入包管理系統(tǒng)所需的公鑰
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
為MongoDB創(chuàng)建一個列表文件
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
更新apt-get包
sudo apt-get update
安裝最新穩(wěn)定版本
sudo apt-get install -y mongodb-org

提示:如果安裝時Unable to locate package ,這時需要更換apt-get源,如果更換源還是出現(xiàn)錯誤,建議尋求多個源數(shù)據(jù),然后執(zhí)行:sudo apt-get update

更換源: $ vim /etc/apt/sources.list

mongodb 配置目錄/etc/mongod.conf ,正常情況內(nèi)容如下 :

# mongod.conf
# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1

#processManagement:
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
啟動服務(wù)
sudo service mongod start
停止服務(wù)
sudo service mongod stop
重啟服務(wù)
sudo service mongod restart
執(zhí)行命令,出現(xiàn)如下圖表示安裝mongodb成功并可以使用
mongo
  1. 限制訪問IP和端口
    編輯/etc/mongod.conf【ubuntu】
#(內(nèi)容的#表示注釋,去掉則啟用)
net:
     port: 27017
     bindIp: 127.0.0.1
  • 設(shè)置用戶名和密碼
    編輯/etc/mongod.conf,如果沒有 authorization: enabled 則添加一行【ubuntu】
#(內(nèi)容的#表示注釋,去掉則啟用)
security:
   authorization: enabled
創(chuàng)建用戶權(quán)限及作用角色

rules可選角色列表

  1. 數(shù)據(jù)庫用戶角色:read、readWrite;
  2. 數(shù)據(jù)庫管理角色:dbAdmin、dbOwner、userAdmin;
  3. 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
  4. 備份恢復(fù)角色:backup、restore;
  5. 所有數(shù)據(jù)庫角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
  6. 超級用戶角色:root // 這里還有幾個角色間接或直接提供了系統(tǒng)超級用戶的訪問(dbOwner 、userAdmin、userAdminAnyDatabase)
  7. 內(nèi)部角色:__system

rules角色列表對應(yīng)釋義

read:允許用戶讀取指定數(shù)據(jù)庫
readWrite:允許用戶讀寫指定數(shù)據(jù)庫
dbAdmin:允許用戶在指定數(shù)據(jù)庫中執(zhí)行管理函數(shù),如索引創(chuàng)建、刪除,查看統(tǒng)計或訪問system.profile
userAdmin:允許用戶向system.users集合寫入,可以找指定數(shù)據(jù)庫里創(chuàng)建、刪除和管理用戶
clusterAdmin:只在admin數(shù)據(jù)庫中可用,賦予用戶所有分片和復(fù)制集相關(guān)函數(shù)的管理權(quán)限。
readAnyDatabase:只在admin數(shù)據(jù)庫中可用,賦予用戶所有數(shù)據(jù)庫的讀權(quán)限
readWriteAnyDatabase:只在admin數(shù)據(jù)庫中可用,賦予用戶所有數(shù)據(jù)庫的讀寫權(quán)限
userAdminAnyDatabase:只在admin數(shù)據(jù)庫中可用,賦予用戶所有數(shù)據(jù)庫的userAdmin權(quán)限
dbAdminAnyDatabase:只在admin數(shù)據(jù)庫中可用,賦予用戶所有數(shù)據(jù)庫的dbAdmin權(quán)限。
root:只在admin數(shù)據(jù)庫中可用。超級賬號,超級權(quán)限

創(chuàng)建用戶,創(chuàng)建成功后如果不執(zhí)行db.auth()登陸用戶則無法使用mongo操作數(shù)據(jù)庫

$ use admin
//在admin下創(chuàng)建超級權(quán)限的管理員
$ db.createUser({user:"admin",pwd:"password",roles:["root"]});
//創(chuàng)建成功驗證超管
$ db.auth("admin","password");
//切換到另一個為數(shù)據(jù)庫
$ use test
//為test數(shù)據(jù)創(chuàng)建一個可讀寫的用戶
$ db.createUser({user:"rw",pwd:"rw2017",roles:[{role:"readWrite",db:"test"}]})
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容