Mongodb的安裝、主從配置、replicaSet配置

一.Mongodb的安裝

 

環境:linux 64bit

版本:2.0.1

位置:/opt/mongodb

安裝步驟:

tar –zxvf mongodb-linux-x86_64-2.0.1.tar –C /opt/mongodb

mkdir /opt/mongodb/data

touch /opt/mongodb/logs

 

安裝是很簡單的,關鍵是下面的啓動的配置。啓動配置有三種情況,分別是正常的單主機啓動,主從啓動,副本集啓動三種方式。

1.       單主機啓動方式

啓動

cd /opt/mongodb/bin

/opt/mongodb/bin/mongod –f mongodb.conf

#mongodb.conf是自定義的啓動配置文件,其中參數auth表明需要認證才能登錄,內容如下:

#start as daemon and need authentication

port=27017

fork=true

dbpath=/opt/mongodb/data/

logpath=/opt/mongodb/logs

auth=true

 

設置開機自動啓動(可選)

echo "/opt/mongodb/bin/mongod –f /opt/mongodb/bin/mongodb.conf" >> /etc/rc.local

 

設置密碼

#連接mongodb

cd /opt/mongodb/bin

./mongo

#默認進入到mongodb默認的test庫

#爲數據庫設置用戶名密碼

#首先切換到admin庫,先建立一個管理員賬號

> use admin

> db.addUser("root","root");

#切換到test庫

use test

#爲test庫添加用戶名和密碼

> db.addUser("testUser","123456");

#注:若此時顯示

Tue May 22 16:16:29 uncaught exception: error {

        "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",

        "code" : 10057

}

說明設置密碼已生效,此時要先轉到admin庫,輸入認證再轉回到test庫設置用戶名密碼,如下:

use admin

db.auth(“root”,”root”);

use test

#下次再連接mongodb時需要使用如下方式:

./mongo –u testUser –p 123456

#刪除用戶

Db.system.users.remove({“user”:”testUser”});

#注:mongodb的用戶名和密碼都是針對每一個庫的,所以不同的庫可以設置不同的用戶名和密碼。

2.       主從方式啓動

實際上現在這種方式已經不推薦使用了,推薦使用的是replicaset,下面會講到

假設已經將mongodb安裝在了兩臺主機上ip分別爲192.168.0.1和192.168.0.2上,下面是啓動配置

啓動master(192.168.0.1)
cd /opt/mongodb/
./bin/mongod --dbpath=/opt/mongodb/data --logpath=/opt/mongodb/logs
--master --oplogSize 64 --logappend  --port=27017 –fork

 

啓動slave(192.168.0.2)
cd /opt/mongodb/
./bin/mongod --dbpath=/opt/mongodb/data --logpath=/opt/mongodb/logs
--slave --source 192.168.0.1 --only test --slavedelay 10
--logappend  --port=27017 –fork

 

這樣配置就完成了,當然也可以將這些啓動參數像前面一樣寫在配置文件中,啓動時使用配置文件啓動。

配置完成後可以測試一下,如下:

在主機器上添加數據
cd /opt/mongodb/
./bin/mongo
db.foo.save({"id":123,"name":gongyong});
成功的話可以在從服務器看到數據:
cd /opt/mongodb/
./bin/mongo
db.foo.find({"id":123})

 

 

3.       replcaSet的配置

我們假設有三臺服務器,分別已經安裝好了mongodb,有如下三段配置

#10.127.65.92上的配置

logpath=/opt/mongodb/logs

dbpath=/opt/mongodb/data/

port=27017

fork=true

logappend=true

replSet=rs1/10.127.65.95:27017

 

#10.127.65.95上的配置

logpath=/opt/mongodb/logs

dbpath=/opt/mongodb/data/

port=27017

fork=true

logappend=true

replSet=rs1/10.127.65.92:27017

 

#10.11.154.91上的配置

logpath=/opt/mongodb/logs

dbpath=/opt/mongodb/data/

port=27017

fork=true

logappend=true

replSet=rs1/10.127.65.92:27017,10.127.65.95:27017

分別將上面三段配置複製後放到每臺mongodb的某個位置(推薦bin目錄),三臺服務器分別使用上面的配置啓動後,要進行初始化

首先連接上65.92這臺mongo

./mongo -port 27017

這樣就連接上了mongodb的默認庫test,然後輸入如下指令:

> config_rs1={_id:'rs1',members:[

...{_id:0,host:'10.127.65.92:27017',priority:10},

...{_id:1,host:'10.127.65.95:27017',priority:9},

...{_id:2,host:'10.11.154.91:27017',priority:9}]

...}

> rs.initiate(config_rs1);

回車後出現下面的提示:

{

   “info”:"Config now saved locallly. should come online in about a minute.",

   " ok":1

}

說明初始化成功,注意只需要在其中任何一臺上初始化就可以

 

運行如下指令查看是否成功

rs.status()

 

然後退出再重新連接,發現光標變成下面這種就對了:

rs1:PRIMARY

 

配置完成後可以進行祕密和用戶名的設置,設置方式前面已經提到,需要注意的是,每臺mongodb都需要用同樣的方式設置用戶名和祕密。

 

到這裏,mongodb的安裝,主從,副本集的配置就基本完成,再往後還有mongodb的集羣分片的搭建,在另一篇文章裏會詳細說明 http://gong1208.iteye.com/blog/1622078

 

附:各啓動參數的說明

參數解釋: --dbpath 數據庫路徑(數據文件)
--logpath 日誌文件路徑
--master 指定爲主機器
--slave 指定爲從機器
--source 指定主機器的IP地址
--pologSize 命令行參數(與--master一同使用)配置用於存儲給從節點可用的更新信息佔用的磁盤空間(M爲單位),如果不指定這個參數,默認大小爲當前可用磁盤空間的5%(64位機器最小值爲1G,32位機器爲50M)。
--logappend 日誌文件末尾添加
--port 啓用端口號
--fork 在後臺運行
--only 指定只複製哪一個數據庫
--slavedelay 指從複製檢測的時間間隔
--auth 是否需要驗證權限登錄(用戶名和密碼)
-h [ --help ]             show this usage information
--version                 show version information
-f [ --config ] arg       configuration file specifying additional options
--port arg                specify port number
--bind_ip arg             local ip address to bind listener - all local ips
                           bound by default
-v [ --verbose ]          be more verbose (include multiple times for more
                           verbosity e.g. -vvvvv)
--dbpath arg (=/data/db/) directory for datafiles    指定數據存放目錄
--quiet                   quieter output   靜默模式
--logpath arg             file to send all output to instead of stdout   指定日誌存放目錄
--logappend               appnd to logpath instead of over-writing 指定日誌是以追加還是以覆蓋的方式寫入日誌文件
--fork                    fork server process   以創建子進程的方式運行
--cpu                     periodically show cpu and iowait utilization 週期性的顯示cpu和io的使用情況
--noauth                  run without security 無認證模式運行
--auth                    run with security 認證模式運行
--objcheck                inspect client data for validity on receipt 檢查客戶端輸入數據的有效性檢查
--quota                   enable db quota management   開始數據庫配額的管理
--quotaFiles arg          number of files allower per db, requires --quota 規定每個數據庫允許的文件數
--appsrvpath arg          root directory for the babble app server 
--nocursors               diagnostic/debugging option 調試診斷選項
--nohints                 ignore query hints 忽略查詢命中率
--nohttpinterface         disable http interface 關閉http接口,默認是28017
--noscripting             disable scripting engine 關閉腳本引擎
--noprealloc              disable data file preallocation 關閉數據庫文件大小預分配
--smallfiles              use a smaller default file size 使用較小的默認文件大小
--nssize arg (=16)        .ns file size (in MB) for new databases 新數據庫ns文件的默認大小
--diaglog arg             0=off 1=W 2=R 3=both 7=W+some reads 提供的方式,是隻讀,只寫,還是讀寫都行,還是主要寫+部分的讀模式
--sysinfo                 print some diagnostic system information 打印系統診斷信息
--upgrade                 upgrade db if needed 如果需要就更新數據庫
--repair                  run repair on all dbs 修復所有的數據庫
--notablescan             do not allow table scans 不運行表掃描
--syncdelay arg (=60)     seconds between disk syncs (0 for never) 系統同步刷新磁盤的時間,默認是60s
Replication options:
--master              master mode 主複製模式
--slave               slave mode 從複製模式
--source arg          when slave: specify master as <server:port> 當爲從時,指定主的地址和端口
--only arg            when slave: specify a single database to replicate 當爲從時,指定需要從主複製的單一庫
--pairwith arg        address of server to pair with
--arbiter arg         address of arbiter server 仲裁服務器,在主主中和pair中用到
--autoresync          automatically resync if slave data is stale 自動同步從的數據
--oplogSize arg       size limit (in MB) for op log 指定操作日誌的大小
--opIdMem arg         size limit (in bytes) for in memory storage of op ids指定存儲操作日誌的內存大小
Sharding options:
--configsvr           declare this is a config db of a cluster 指定shard中的配置服務器
--shardsvr            declare this is a shard db of a cluster 指定shard服務器

 

注:版權所有,轉載請註明來源:

http://gong1208.iteye.com/blog/1558355

發佈了22 篇原創文章 · 獲贊 2 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章