第一部分:MongoDB備忘錄

來源:互聯網
上載者:User

標籤:users   monit   roles   查看   tree   TE   Owner   pass   常用   

一、NoSQL 簡介

  Nosql的全稱是Not Only Sql,這個概念早起就有人提出,在09年的時候比較火。Nosql指的是非關係型資料庫,而我們常用的都是關係型資料庫。就像我們常用的mysql,sqlserver,oracle一樣,這些資料庫一般用來儲存重要訊息,應對普通的業務是沒有問題的。但是,隨著互連網的高速發展,傳統的關係型資料庫在應付超大規模,超大流量以及高並發的時候力不從心。而就在這個時候,Nosql得到的告訴的發展。

 

二、MongoDB備忘錄

1、首先說一下自己在使用MongoDB過程中踩過的坑,安裝的時候很簡單,解壓安裝完成。我安裝的版本是v3.6.5,使用中也非常簡單

最簡單的啟動指令:mongod帶認證的啟動指令:mongod -dbpath /data/db -auth指定設定檔啟動的指令:mongod -config /etc/mongod.conf

 

 

2、啟動後使用mongo進入shell介面,就可以操作資料庫了,下面列一些我還記住的指令,不知道的指令區runoob上查一下

查看資料庫:show dbs使用資料庫:use <db_name>查看集合(表):show collections查看一條內容:db.school.findOne()

 

 

3、上面說到了使用認證方式啟動mongodb,那麼認證的前提當然是需要有使用者名稱和密碼了,下面記錄一下如何增加認證使用者。

> use adminswitched to db admin> db.createUser(...   {...     user: "dba",...     pwd: "dba",...     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]...   }... )Successfully added user: {    "user" : "dba",    "roles" : [        {            "role" : "userAdminAnyDatabase",            "db" : "admin"        }    ]}
  • user:使用者名稱
  • pwd:密碼
  • db : 資料庫名字
  • roles:指定使用者的角色,可以用一個空數組給新使用者設定空角色;在roles欄位,可以指定內建角色和使用者定義的角色。

role裡的角色可以選:

Built-In Roles(內建角色):1. 資料庫使用者角色:read、readWrite;2. 資料庫管理角色:dbAdmin、dbOwner、userAdmin;3. 叢集管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;4. 備份恢複角色:backup、restore;5. 所有資料庫角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase6. 超級使用者角色:root  // 這裡還有幾個角色間接或直接提供了系統超級使用者的訪問(dbOwner 、userAdmin、userAdminAnyDatabase)7. 內部角色:__system具體角色:read:允許使用者讀取指定資料庫readWrite:允許使用者讀寫指定資料庫dbAdmin:允許使用者在指定資料庫中執行管理函數,如索引建立、刪除,查看統計或訪問system.profileuserAdmin:允許使用者向system.users集合寫入,可以找指定資料庫裡建立、刪除和系統管理使用者clusterAdmin:只在admin資料庫中可用,賦予使用者所有分區和複製集相關函數的系統管理權限。readAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的讀許可權readWriteAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的讀寫權限userAdminAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的userAdmin許可權dbAdminAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的dbAdmin許可權。root:只在admin資料庫中可用。超級帳號,超級許可權
開啟使用者權限

建立mongodb.conf檔案,並加入“auth=true”

從設定檔啟動mongodb服務

./bin/mongod -f mongodb.conf

 

使用者驗證

[[email protected] mongodb3.2.10]$ ./bin/mongo MongoDB shell version: 3.6.1connecting to: test> use adminswitched to db caideyang> db.auth(‘admin‘,‘admin‘)1>

mongodb.conf設定檔

#資料庫資料存放目錄dbpath=/usr/local/mongodb304/data#資料庫日誌存放目錄logpath=/usr/local/mongodb304/logs/mongodb.log #以追加的方式記錄日誌logappend = true#連接埠號碼 預設為27017port=27017 #以後台方式運行進程fork=true  #開啟使用者認證auth=true#關閉http介面,預設關閉http連接埠訪問nohttpinterface=true#mongodb所綁定的ip地址bind_ip = 127.0.0.1,192.168.1.140#啟用記錄檔,預設啟用journal=true #這個選項可以過濾掉一些無用的日誌資訊,若需要調試使用請設定為falsequiet=true

其他配置參數含義

--quiet # 安靜輸出--port arg  # 指定服務連接埠號碼,預設連接埠27017--bind_ip arg   # 綁定服務IP,若綁定127.0.0.1,則只能本機訪問,不指定預設本地所有IP--logpath arg   # 指定MongoDB記錄檔,注意是指定檔案不是目錄--logappend # 使用追加的方式寫日誌--pidfilepath arg   # PID File 的完整路徑,如果沒有設定,則沒有PID檔案--keyFile arg   # 叢集的私密金鑰的完整路徑,只對於Replica Set 架構有效--unixSocketPrefix arg  # UNIX域通訊端替代目錄,(預設為 /tmp)--fork  # 以守護進程的方式運行MongoDB,建立伺服器處理序--auth  # 啟用驗證--cpu   # 定期顯示CPU的CPU利用率和iowait--dbpath arg    # 指定資料庫路徑--diaglog arg   # diaglog選項 0=off 1=W 2=R 3=both 7=W+some reads--directoryperdb    # 設定每個資料庫將被儲存在一個單獨的目錄--journal   # 啟用日誌選項,MongoDB的資料操作將會寫入到journal檔案夾的檔案裡--journalOptions arg    # 啟用日誌診斷選項--ipv6  # 啟用IPv6選項--jsonp # 允許JSONP形式通過HTTP訪問(有安全影響)--maxConns arg  # 最大同時串連數 預設2000--noauth    # 不啟用驗證--nohttpinterface   # 關閉http介面,預設關閉27018連接埠訪問--noprealloc    # 禁用資料檔案預分配(往往影響效能)--noscripting   # 禁用指令碼引擎--notablescan   # 不允許表掃描--nounixsocket  # 禁用Unix通訊端監聽--nssize arg (=16)  # 設定信資料庫.ns檔案大小(MB)--objcheck  # 在收到客戶資料,檢查的有效性,--profile arg   # 檔案參數 0=off 1=slow, 2=all--quota # 限制每個資料庫的檔案數,設定預設為8--quotaFiles arg    # number of files allower per db, requires --quota--rest  # 開啟簡單的rest API--repair    # 修複所有資料庫run repair on all dbs--repairpath arg    # 修複庫產生的檔案的目錄,預設為目錄名稱dbpath--slowms arg (=100) # value of slow for profile and console log--smallfiles    # 使用較小的預設檔案--syncdelay arg (=60)   # 資料寫入磁碟的時間秒數(0=never,不推薦)--sysinfo   # 列印一些診斷系統資訊--upgrade   # 如果需要升級資料庫 * Replicaton 參數----------------------------------------------------------------------------------fastsync  # 從一個dbpath裡啟用從庫複製服務,該dbpath的資料庫是主庫的快照,可用於快速啟用同步--autoresync    # 如果從庫與主庫同步資料差得多,自動重新同步,--oplogSize arg # 設定oplog的大小(MB) * 主/從參數----------------------------------------------------------------------------------master    # 主庫模式--slave # 從庫模式--source arg    # 從庫 連接埠號碼--only arg  # 指定單一的資料庫複寫--slavedelay arg    # 設定從庫同步主庫的延遲時間 * Replica set(複本集)選項:----------------------------------------------------------------------------------replSet arg   # 設定複本集名稱 * Sharding(分區)選項----------------------------------------------------------------------------------configsvr # 聲明這是一個叢集的config服務,預設連接埠27019,預設目錄/data/configdb--shardsvr  # 聲明這是一個叢集的分區,預設連接埠27018--noMoveParanoia    # 關閉偏執為moveChunk資料儲存
備份還原資料庫 備份
[[email protected] mongodb3.2.10]$ ./bin/mongodump --port=27017 -ucdy -ppasswd --db=caideyang -o /home/caideyang/backup/2016-10-30T15:58:58.082+0800    writing caideyang.score to 2016-10-30T15:58:58.083+0800    writing caideyang.user to 2016-10-30T15:58:58.084+0800    done dumping caideyang.score (4 documents)2016-10-30T15:58:58.085+0800    done dumping caideyang.user (2 documents)[[email protected] ~]$ tree backup/backup/└── caideyang    ├── score.bson    ├── score.metadata.json    ├── user.bson    └── user.metadata.json1 directory, 4 files

還原

[[email protected] mongodb3.2.10]$ ./bin/mongorestore --port=27017 -ucdy -ppasswd --db=caideyang ../backup/caideyang/2016-10-30T16:04:43.914+0800    building a list of collections to restore from ../backup/caideyang dir2016-10-30T16:04:43.918+0800    reading metadata for caideyang.score from ../backup/caideyang/score.metadata.json2016-10-30T16:04:43.918+0800    reading metadata for caideyang.user from ../backup/caideyang/user.metadata.json2016-10-30T16:04:43.918+0800    restoring caideyang.score from ../backup/caideyang/score.bson2016-10-30T16:04:43.918+0800    restoring caideyang.user from ../backup/caideyang/user.bson2016-10-30T16:04:43.930+0800    error: multiple errors in bulk operation:  - E11000 duplicate key error collection: caideyang.user index: _id_ dup key: { : ObjectId(‘5815a5fa5fee222be4c321c9‘) }  - E11000 duplicate key error collection: caideyang.user index: _id_ dup key: { : ObjectId(‘5815a63a5fee222be4c321ca‘) }2016-10-30T16:04:43.930+0800    restoring indexes for collection caideyang.user from metadata2016-10-30T16:04:43.936+0800    finished restoring caideyang.user (2 documents)2016-10-30T16:04:43.988+0800    restoring indexes for collection caideyang.score from metadata2016-10-30T16:04:43.989+0800    finished restoring caideyang.score (4 documents)2016-10-30T16:04:43.989+0800    done[[email protected] mongodb3.2.10]$

註:部分內容引用自 https://www.jianshu.com/p/5844fdfa9511

第一部分:MongoDB備忘錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.