mongodb 简单部署方案及实例
第一節(jié)?準(zhǔn)備工作
?一?安裝mongodb
??我這里選用rehl 5.6作為其運行載體。
??1.?下載mongodb的linux版本(注意32位和64位的區(qū)別),下載地址:http://www.mongodb.org/downloads
???# wget?http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.4.tgz
??2.?將下載的mongodb安裝文件 mongodb-linux-x86_64-2.0.4.tgz 放到/usr/local/下
??3.?解壓?
???# tar -zxvf mongodb-linux-x86_64-2.0.4.tgz
???重命名?
???# mv mongodb-linux-x86_64-2.0.4 /usr/local/mongodb
??4.?創(chuàng)建數(shù)據(jù)庫文件目錄, # mkdir -p /data/mongodb,我把數(shù)據(jù)庫文件目錄放在當(dāng)前文件夾下,# mkdir -p data/db,創(chuàng)建日志目錄 #mkdir -p log
??
?二?啟動
??首先用命令進入到bin目錄下:cd /usr/local/mongodb/bin
??
??方式一:直接運行命令啟動
??./mongod –port 10000 –fork –logpath= logpath=/data/mongodb/log/mongodb.log -- logappend -- dbpath=/data/mongodb/data/db
??
??方式二:通過配置文件方式啟動(推薦)。
??1.在服務(wù)器上新建mongodb.conf配置文件(建議用命令生成,放在mongodb同一個目錄下)
??內(nèi)容為:
??port=10000 #端口號
??fork=true #以守護進程的方式運行,創(chuàng)建服務(wù)器進程
??#master=true #單主從配置時設(shè)為主服務(wù)器
??#salve=true ##單主從配置時設(shè)為從服務(wù)器
??logpath=/data/mongodb/log/mongodb.log #日志輸出文件路徑
??logappend=true #日志輸出方式
??dbpath=/data/mongodb/data/db #數(shù)據(jù)庫路徑
??replSet=blort #設(shè)置富本集的名字
??shardsvr=true #設(shè)置是否分片
??2.運行./mongod –config ~/.mongodb.conf 命令(--config可以直接寫成-f)。
??輸出:
??all output going to: /data/mongodb/log/mongodb.log
??forked process: 5315
??
??mongodb 啟動參數(shù)
??? --quiet????????????????????????????? # 安靜輸出?
??? --port arg??????????????????????? # 指定服務(wù)端口號,默認(rèn)端口27017?
??? --bind_ip arg????????????????? # 綁定服務(wù)IP,若綁定127.0.0.1,則只能本機訪問,不指定默認(rèn)本地所有IP?
??? --logpath arg????????????????? # 指定MongoDB日志文件,注意是指定文件不是目錄?
??? --logappend?????????????????? # 使用追加的方式寫日志?
??? --pidfilepath arg???????????? # PID File 的完整路徑,如果沒有設(shè)置,則沒有PID文件?
??? --keyFile arg?????????????????? # 集群的私鑰的完整路徑,只對于Replica Set 架構(gòu)有效?
??? --unixSocketPrefix arg? # UNIX域套接字替代目錄,(默認(rèn)為 /tmp)?
??? --fork???????????????????????????????? # 以守護進程的方式運行MongoDB,創(chuàng)建服務(wù)器進程?
??? --auth??????????????????????????????? # 啟用驗證?
??? --cpu???????????????????????????????? # 定期顯示CPU的CPU利用率和iowait?
??? --dbpath arg???????????????????? # 指定數(shù)據(jù)庫路徑?
??? --diaglog arg??????????????????? # diaglog選項 0=off 1=W 2=R 3=both 7=W+some reads?
??? --directoryperdb????????????? # 設(shè)置每個數(shù)據(jù)庫將被保存在一個單獨的目錄?
??? --journal??????????????????????????? # 啟用日志選項,MongoDB的數(shù)據(jù)操作將會寫入到j(luò)ournal文件夾的文件里?
??? --journalOptions arg?????? # 啟用日志診斷選項?
??? --ipv6???????????????????????????????? # 啟用IPv6選項?
??? --jsonp????????????????????????????? # 允許JSONP形式通過HTTP訪問(有安全影響)?
??? --maxConns arg???????????? # 最大同時連接數(shù) 默認(rèn)2000?
??? --noauth?????????????????????????? # 不啟用驗證?
??? --nohttpinterface???????????? # 關(guān)閉http接口,默認(rèn)關(guān)閉27018端口訪問?
??? --noprealloc???????????????????? # 禁用數(shù)據(jù)文件預(yù)分配(往往影響性能)?
??? --noscripting??????????????????? # 禁用腳本引擎?
??? --notablescan????????????????? # 不允許表掃描?
??? --nounixsocket??????????????? # 禁用Unix套接字監(jiān)聽?
??? --nssize arg (=16)?????????? # 設(shè)置信數(shù)據(jù)庫.ns文件大小(MB)?
??? --objcheck??????????????????????? # 在收到客戶數(shù)據(jù),檢查的有效性,?
??? --profile arg????????????????????? # 檔案參數(shù) 0=off 1=slow, 2=all?
??? --quota????????????????????????????? # 限制每個數(shù)據(jù)庫的文件數(shù),設(shè)置默認(rèn)為8?
??? --quotaFiles arg?????????????? #? number of files allower per db, requires --quota?
??? --rest????????????????????????????????? # 開啟簡單的rest API?
??? --repair????????????????????????????? # 修復(fù)所有數(shù)據(jù)庫run repair on all dbs?
??? --repairpath arg?????????????? # 修復(fù)庫生成的文件的目錄,默認(rèn)為目錄名稱dbpath?
??? --slowms arg (=100)?????? # value of slow for profile and console log?
??? --smallfiles?????????????????????? # 使用較小的默認(rèn)文件?
??? --syncdelay arg (=60)??? # 數(shù)據(jù)寫入磁盤的時間秒數(shù)(0=never,不推薦)?
??? --sysinfo?????????????????????????? # 打印一些診斷系統(tǒng)信息?
??? --upgrade??????????????????????? # 如果需要升級數(shù)據(jù)庫
????
??? * Replicaton 參數(shù)
???--------------------------------------------------------------------------------
??? --fastsync????????????????????? # 從一個dbpath里啟用從庫復(fù)制服務(wù),該dbpath的數(shù)據(jù)庫是主庫的快照,可用于快速啟用同步?
??? --autoresync???????????????? # 如果從庫與主庫同步數(shù)據(jù)差得多,自動重新同步,?
??? --oplogSize arg??????????? # 設(shè)置oplog的大小(MB)?
????
??? * 主/從參數(shù)
???--------------------------------------------------------------------------------
??? --master??????????????????????? # 主庫模式?
??? --slave?????????????????????????? # 從庫模式?
??? --source arg???????????????? # 從庫 端口號?
??? --only arg????????????????????? # 指定單一的數(shù)據(jù)庫復(fù)制?
??? --slavedelay arg????????? # 設(shè)置從庫同步主庫的延遲時間?
?????
??? * Replica set(副本集)選項:
???--------------------------------------------------------------------------------
??? --replSet arg?????????????????? # 設(shè)置副本集名稱?
????
??? * Sharding(分片)選項
???--------------------------------------------------------------------------------
??? --configsvr?????????????????????? # 聲明這是一個集群的config服務(wù),默認(rèn)端口27019,默認(rèn)目錄/data/configdb?
??? --shardsvr??????????????????????? # 聲明這是一個集群的分片,默認(rèn)端口27018?
??? --noMoveParanoia??????? # 關(guān)閉偏執(zhí)為moveChunk數(shù)據(jù)保存??
?
?三?查詢進程和關(guān)閉數(shù)據(jù)庫
??1.?運行 ps -ef|grep mongo 命令查詢服務(wù)器進程
??2.?運行 kill -2 5315命令kill掉5315的進程
??
?四?進入數(shù)據(jù)庫并簡單操作數(shù)據(jù)庫
??1.運行./mongo –port 1000進入剛剛啟動的端口號為10000的數(shù)據(jù)庫
??2.使用use test可以切換集合(相當(dāng)于數(shù)據(jù)庫表),這里切換到了test集合
??3.使用db.test.insert("a","b");向集合插入數(shù)據(jù)
??4.使用db.test.find();查詢集合里的數(shù)據(jù)
??5.更多shell命令請查閱相關(guān)文檔
?五?注意事項
??1. 防火墻設(shè)置(測試時可直接關(guān)閉linux關(guān)閉防火墻:立即但不永久生效:service iptables stop重啟永久生效:chkconfig iptables off)
第二節(jié)?副本集
?簡單的說,副本集就是有自動故障恢復(fù)功能的主從集群,副本集沒有固定的"主節(jié)點",集群會通過投票選舉一個"主節(jié)點"。當(dāng)主節(jié)點巖機時,會變更到其他節(jié)點。副本集布在不同機器上時,至少要啟動三個(單數(shù))數(shù)據(jù)庫服務(wù)器進程,否則啟動時投票不成功會一直初始化不了。
?
?一?以配置文件方式啟動三臺機器上的數(shù)據(jù)庫服務(wù)器
??1.?三臺服務(wù)器的配置文件:
???port=10000 #端口號
???fork=true #以守護進程的方式運行,創(chuàng)建服務(wù)器進程
???logpath=/data/mongodb/log/mongodb.log #日志輸出文件路徑
???logappend=true #日志輸出方式
???dbpath=/data/mongodb/data/db #數(shù)據(jù)庫路徑
???replSet=blort #設(shè)置富本集的名字為blort,replSet是讓服務(wù)器知道在這個"blort"副本集中還有別的機器
???# replSet=blort /10.10.112.181:10000, 10.10.112.191:10000#設(shè)置富本集的名字,這種方式也可以,意思就是"blort"副本集其他機器,位置在10.10.112.181:10000和10.10.112.191:10000,但不推薦
??
??2.?通命令啟動三臺數(shù)據(jù)庫服務(wù)器
???/usr/local/mongodb/bin/mongod -f /data/mongodb/blort/config.conf
???/usr/local/mongodb/bin/mongod -f /data/mongodb/blort2/config.conf
???/usr/local/mongodb/bin/mongod -f /data/mongodb/blort3/config.conf
??
?二?初始化副本集(只能初始化一次)
??/usr/local/mongodb/bin/mongo --port 30001
??use admin
??方法一:
??db.runCommand({"replSetInitiate":{"_id":"blort","members":[{"_id":0,"host":"10.10.113.122:30001"},{"_id":1,"host":"10.10.113.122:30002"},{"_id":2,"host":"10.10.113.122:30003","arbiterOnly":true}]}})
??
??方法二:
??config={_id:'blort',members:[{"_id":0,host:'10.10.113.122:30001},{"_id":1,host:'10.10.113.122:30002'},{"_id":2,"host":"10.10.113.122:30003","arbiterOnly":true}]};
??rs.initiate(config);
?三?增加和設(shè)置副本集
??有幾種不同類型的節(jié)點可以存在于副本集:
??Standard :存在于副本,參與投票,有可能成為活躍節(jié)點(即主節(jié)點)
??Passive :存儲了完整的數(shù)據(jù)副本,參與投票,不能成為活躍節(jié)點
??Arbiter:仲裁者,只參與投票,不能成為活躍節(jié)點
??Priority:優(yōu)先級,(投票會投優(yōu)先級高的,如果相同,則投數(shù)據(jù)最新的)
??以上類型可以通過以下命令對副本集合進行修改或者增加服務(wù)器
??db.runCommand({"replSetReconfig":{"_id":"test3","version":2,"members":[{"_id":0,"host":"10.10.113.122:30001"},{"_id":1,"host":"10.10.113.122:30002"},{"_id":2,"host":"10.10.113.122:30003","arbiterOnly":true}]}})
??
??增加副本服務(wù)器
??在增加一個從服務(wù)器節(jié)點
??a.以相同副本集名字啟動一臺服務(wù)器
??b.通過rs.add命令往system.replset添加新的從服務(wù)器成員
??rs.add("10.10.113.122:30005"); 或者rs.add({"_id":4,"host":"10.10.113.122:30005"})?
??添加仲裁
??rs.addArb("10.10.113.122:30004");
?四?讀擴展
??待研究
??
?五?用戶校驗
??未成功
??use test;
??db.addUser("test","123456");
??db.auth("test","123456");
??db.system.users.find();
??db.system.users.remove("test","123456");
??
第二節(jié)?replica sets + shard (雙機方案,單機模擬)
?
?一?準(zhǔn)備
??mkdir -p /data/mongodb/shard1-1/db/
??mkdir -p /data/mongodb/shard1-1/log/
??
??/data/mongodb/shard1-1/config.conf
???port=30001 ??#端口號
???fork=true ??#以守護進程的方式運行,創(chuàng)建服務(wù)器進程
???#nohttpinterface=true???# 關(guān)閉http接口,默認(rèn)關(guān)閉27018端口訪問 port + 1000
???rest = true??#http接口 啟動rest 使能訪問 。與nohttpinterface 對應(yīng) 只開一個
???shardsvr=true??#啟動分片
???#oplogSize=100??#復(fù)制日志大小 MB
???logpath=/data/mongodb/shard1-1/log/shard1-1.log ??#日志輸出文件路徑
???logappend=true ??#日志輸出方式
???dbpath=/data/mongodb/shard1-1/db ??#數(shù)據(jù)庫路徑
???directoryperdb=true??# 設(shè)置每個數(shù)據(jù)庫將被保存在一個單獨的目錄?
???replSet=shard1 ??#設(shè)置富本集的名字為shard1,replSet是讓服務(wù)器知道在這個"shard1"副本集中還有別的機器
???#auth=true?# 啟用驗證
??
??mkdir -p /data/mongodb/shard1-1a/db/
??mkdir -p /data/mongodb/shard1-1a/log/
??/data/mongodb/shard1-1a/config.conf
???port=30002 ??#端口號
???fork=true ??#以守護進程的方式運行,創(chuàng)建服務(wù)器進程
???#nohttpinterface=true???# 關(guān)閉http接口,默認(rèn)關(guān)閉27018端口訪問 port + 1000
???rest = true??#http接口 啟動rest 使能訪問 。與nohttpinterface 對應(yīng) 只開一個
???shardsvr=true??#啟動分片
???#oplogSize=100??#復(fù)制日志大小 MB
???logpath=/data/mongodb/shard1-1a/log/shard1-1a.log ??#日志輸出文件路徑
???logappend=true ??#日志輸出方式
???dbpath=/data/mongodb/shard1-1a/db ??#數(shù)據(jù)庫路徑
???directoryperdb=true??# 設(shè)置每個數(shù)據(jù)庫將被保存在一個單獨的目錄?
???replSet=shard1 ??#設(shè)置富本集的名字為shard1,replSet是讓服務(wù)器知道在這個"shard1"副本集中還有別的機器
???#auth=true?# 啟用驗證
??
??
??mkdir -p /data/mongodb/shard1-2/db/
??mkdir -p /data/mongodb/shard1-2/log/
??/data/mongodb/shard1-2/config.conf
???port=30003 ??#端口號
???fork=true ??#以守護進程的方式運行,創(chuàng)建服務(wù)器進程
???#nohttpinterface=true???# 關(guān)閉http接口,默認(rèn)關(guān)閉27018端口訪問 port + 1000
???rest = true??#http接口 啟動rest 使能訪問 。與nohttpinterface 對應(yīng) 只開一個
???shardsvr=true??#啟動分片
???#oplogSize=100??#復(fù)制日志大小 MB
???logpath=/data/mongodb/shard1-2/log/shard1-2.log ??#日志輸出文件路徑
???logappend=true ??#日志輸出方式
???dbpath=/data/mongodb/shard1-2/db ??#數(shù)據(jù)庫路徑
???directoryperdb=true??# 設(shè)置每個數(shù)據(jù)庫將被保存在一個單獨的目錄?
???replSet=shard1 ??#設(shè)置富本集的名字為shard1,replSet是讓服務(wù)器知道在這個"shard1"副本集中還有別的機器
???#auth=true?# 啟用驗證
??
??mkdir -p /data/mongodb/shard2-1/db/
??mkdir -p /data/mongodb/shard2-1/log/
??/data/mongodb/shard2-1/config.conf
???port=32001 ??#端口號
???fork=true ??#以守護進程的方式運行,創(chuàng)建服務(wù)器進程
???#nohttpinterface=true???# 關(guān)閉http接口,默認(rèn)關(guān)閉27018端口訪問 port + 1000
???rest = true??#http接口 啟動rest 使能訪問 。與nohttpinterface 對應(yīng) 只開一個
???shardsvr=true??#啟動分片
???#oplogSize=100??#復(fù)制日志大小 MB
???logpath=/data/mongodb/shard2-1/log/shard2-1.log ??#日志輸出文件路徑
???logappend=true ??#日志輸出方式
???dbpath=/data/mongodb/shard2-1/db ??#數(shù)據(jù)庫路徑
???directoryperdb=true??# 設(shè)置每個數(shù)據(jù)庫將被保存在一個單獨的目錄?
???replSet=shard2 ??#設(shè)置富本集的名字為shard2,replSet是讓服務(wù)器知道在這個"shard2"副本集中還有別的機器
???#auth=true?# 啟用驗證
??
??mkdir -p /data/mongodb/shard2-1a/db/
??mkdir -p /data/mongodb/shard2-1a/log/
??/data/mongodb/shard2-1a/config.conf
???port=32002 ??#端口號
???fork=true ??#以守護進程的方式運行,創(chuàng)建服務(wù)器進程
???#nohttpinterface=true???# 關(guān)閉http接口,默認(rèn)關(guān)閉27018端口訪問 port + 1000
???rest = true??#http接口 啟動rest 使能訪問 。與nohttpinterface 對應(yīng) 只開一個
???shardsvr=true??#啟動分片
???#oplogSize=100??#復(fù)制日志大小 MB
???logpath=/data/mongodb/shard2-1a/log/shard2-1a.log ??#日志輸出文件路徑
???logappend=true ??#日志輸出方式
???dbpath=/data/mongodb/shard2-1a/db ??#數(shù)據(jù)庫路徑
???directoryperdb=true??# 設(shè)置每個數(shù)據(jù)庫將被保存在一個單獨的目錄?
???replSet=shard2 ??#設(shè)置富本集的名字為shard2,replSet是讓服務(wù)器知道在這個"shard2"副本集中還有別的機器
???#auth=true?# 啟用驗證
??
??mkdir -p /data/mongodb/shard2-2/db/
??mkdir -p /data/mongodb/shard2-2/log/
??/data/mongodb/shard2-2/config.conf
???port=32003 ??#端口號
???fork=true ??#以守護進程的方式運行,創(chuàng)建服務(wù)器進程
???#nohttpinterface=true???# 關(guān)閉http接口,默認(rèn)關(guān)閉27018端口訪問 port + 1000
???rest = true??#http接口 啟動rest 使能訪問 。與nohttpinterface 對應(yīng) 只開一個
???shardsvr=true??#啟動分片
???#oplogSize=100??#復(fù)制日志大小 MB
???logpath=/data/mongodb/shard2-2/log/shard2-2.log ??#日志輸出文件路徑
???logappend=true ??#日志輸出方式
???dbpath=/data/mongodb/shard2-2/db ??#數(shù)據(jù)庫路徑
???directoryperdb=true??# 設(shè)置每個數(shù)據(jù)庫將被保存在一個單獨的目錄?
???replSet=shard2 ??#設(shè)置富本集的名字為shard2,replSet是讓服務(wù)器知道在這個"shard2"副本集中還有別的機器
???#auth=true?# 啟用驗證
??
??mkdir -p /data/mongodb/config1/db/
??mkdir -p /data/mongodb/config1/log/
??/data/mongodb/config1/config.conf
???dbpath = /data/mongodb/config1/db
???configsvr = true
???port = 40001
???logpath =/data/mongodb/config1/log/config1.log
???logappend = true
???fork = true
???
???#auth=true?# 啟用驗證
??
??mkdir -p /data/mongodb/config2/db/
??mkdir -p /data/mongodb/config2/log/
??/data/mongodb/config2/config.conf
???dbpath = /data/mongodb/config2/db
???configsvr = true
???port = 40002
???logpath =/data/mongodb/config2/log/config2.log
???logappend = true
???fork = true
???
???#auth=true?# 啟用驗證
??
??mkdir -p /data/mongodb/config3/db/
??mkdir -p /data/mongodb/config3/log/
??/data/mongodb/config3/config.conf
???dbpath = /data/mongodb/config3/db
???configsvr = true
???port = 40003
???logpath =/data/mongodb/config3/log/config3.log
???logappend = true
???fork = true
???
???#auth=true?# 啟用驗證
??
??mkdir -p /data/mongodb/mongos1/log/
??/data/mongodb/mongos1/config.conf
???configdb =10.10.113.122:40001,10.10.113.122:40002,10.10.113.122:40003
???port = 50001
???chunkSize = 1
???logpath =/data/mongodb/mongos1/log/mongos1.log
???logappend = true
???fork = true
???#auth=true?# 啟用驗證
??
??mkdir -p /data/mongodb/mongos2/log/
??/data/mongodb/mongos2/config.conf
???configdb =10.10.113.122:40001,10.10.113.122:40002,10.10.113.122:40003
???port = 50002
???chunkSize = 1
???logpath =/data/mongodb/mongos2/log/mongos2.log
???logappend = true
???fork = true
???#auth=true?# 啟用驗證
?二?啟動
?
??shard1 副本 啟動
??/usr/local/mongodb/bin/mongod -f /data/mongodb/shard1-1/config.conf
??/usr/local/mongodb/bin/mongod -f /data/mongodb/shard1-1a/config.conf
??/usr/local/mongodb/bin/mongod -f /data/mongodb/shard1-2/config.conf
??
??/usr/local/mongodb/bin/mongo --port 30001
??
??use admin
??db.runCommand({"replSetInitiate":{"_id":"shard1","members":[{"_id":0,"host":"10.10.113.122:30001"},{"_id":1,"host":"10.10.113.122:30002","arbiterOnly":true},{"_id":2,"host":"10.10.113.122:30003"}]}})
??
??shard2 副本 啟動
??/usr/local/mongodb/bin/mongod -f /data/mongodb/shard2-1/config.conf
??/usr/local/mongodb/bin/mongod -f /data/mongodb/shard2-1a/config.conf
??/usr/local/mongodb/bin/mongod -f /data/mongodb/shard2-2/config.conf
??
??/usr/local/mongodb/bin/mongo --port 31001
??
??use admin
??db.runCommand({"replSetInitiate":{"_id":"shard2","members":[{"_id":0,"host":"10.10.113.122:32001"},{"_id":1,"host":"10.10.113.122:32002","arbiterOnly":true},{"_id":2,"host":"10.10.113.122:32003"}]}})
?
??config1 啟動
??/usr/local/mongodb/bin/mongod -f /data/mongodb/config1/config.conf
??config2 啟動
??/usr/local/mongodb/bin/mongod -f /data/mongodb/config2/config.conf
??config3 啟動
??/usr/local/mongodb/bin/mongod -f /data/mongodb/config3/config.conf
??
??mongos1 啟動
??/usr/local/mongodb/bin/mongos -f /data/mongodb/mongos1/config.conf
??mongos2 啟動
??/usr/local/mongodb/bin/mongos -f /data/mongodb/mongos2/config.conf
??
??
??/usr/local/mongodb/bin/mongo --port 50001
??use admin
??db.runCommand({addshard:"shard1/10.10.113.122:30001,10.10.113.122:30003",name:"shard1", maxsize:100})
??db.runCommand({addshard:"shard2/10.10.113.122:32001,10.10.113.122:32003",name:"shard2", maxsize:100})
??
??db.runCommand({listshards:1})
??
??use test
??db.test.insert({"aaa":"bbb"});
第三節(jié)?replica sets + shard (3機高可用 , 一主雙備份)
?一?方案
??每個分片3服務(wù)器,前期采用三臺,日后服務(wù)器的增加考慮災(zāi)備,服務(wù)增加的基數(shù)最少為三臺(或才有雙機方案)。
??Server1 10.10.113.122?Server1 10.10.113.132?Server3 10.10.113.133
??
??類型????服務(wù)器???用途??? ???系統(tǒng)???說明
???
??存儲/數(shù)據(jù)???Server1?? ?Shard1/Shard2/Shard3??Linux 64位??Shard1:10001;Shard2:10002,Shard3:10003;
???????Server2???Shard1/Shard2/Shard3??Linux 64位??Shard1:10001;Shard2:10002,Shard3:10003;
???????Server3???Shard1/Shard2/Shard3??Linux 64位??Shard1:10001;Shard2:10002,Shard3:10003;
???
??配置????Server1???Config1??????Linux 64位??Config1:20000;
???????Server2???Config2??????Linux 64位??Config2:20000;?
???????Server3???Config3??????Linux 64位??Config3:20000;
???
??路由????Server1???Mongos1??????Linux 64位??Mongos:30000;
???????Server2???Mongos2??????Linux 64位??Mongos:30000;
???????Server3???Mongos3??????Linux 64位??Mongos:30000;
?
?二?準(zhǔn)備
??安裝mongodb 參照第一節(jié)
??
??
??linux 傳文件
??# scp mongodb-linux-x86_64-2.0.4.tgz?root@10.10.113.132:/root
??# ssh 10.10.113.132
??
??刪除目錄
??# rm -r -f /data/mongodb
?
?
??創(chuàng)建配置、日志、分片、key文件存儲目錄及驗證文件
??
??mkdir /data/mongodb/configsvr/ -p
??
??mkdir /data/mongodb/log/ -p
??
??mkdir /data/mongodb/shard1/ -p
??
??mkdir /data/mongodb/shard2/ -p
??
??mkdir /data/mongodb/shard3/ -p
??
??mkdir /data/mongodb/key/ -p
??
??mkdir /data/mongodb/conf/security/ -p
??mkdir /data/mongodb/conf/nosecurity/ -p
??
??創(chuàng)建配置文件
???1、 創(chuàng)建驗證文件security于/data/mongodb/key/目錄,關(guān)賦予可讀權(quán)限,命令如下:
???cd /data/mongodb/key/
???echo 'hycloudmongodbkey' > security
???chmod 600 security
???
???2、 創(chuàng)建shard1.conf、shard2.conf、shard3.conf、configsvr.conf、mongos.conf于/data/mongodb/conf/security/ 和 /data/mongodb/conf/nosecurity/的目錄,內(nèi)容分別如下:
???shard1.conf
???
dbpath = /data/mongodb/shard1
directoryperdb = true
shardsvr = true
replSet = shard1
#bind_ip = 10.10.113.122,localhost
port = 10001
oplogSize = 100
logpath =/data/mongodb/log/shard1.log
logappend = true
profile = 1
slowms = 5
rest = true
fork = true
keyFile = /data/mongodb/key/security? #nosecurity目錄將該行刪除
????
???shard2.conf
dbpath = /data/mongodb/shard2
directoryperdb = true
shardsvr = true
replSet = shard2
#bind_ip = 10.10.113.122,localhost
port = 10002
oplogSize = 100
logpath = /data/mongodb/log/shard2.log
logappend = true
profile = 1
slowms = 5
rest = true
fork = true
keyFile = /data/mongodb/key/security? #nosecurity目錄將該行刪除
????
???shard3.conf
dbpath = /data/mongodb/shard3
directoryperdb = true
shardsvr = true
replSet = shard3
#bind_ip = 10.10.113.122,localhost
port = 10003
oplogSize = 100
logpath = /data/mongodb/log/shard3.log
logappend = true
profile = 1
slowms = 5
rest = true
fork = true
keyFile = /data/mongodb/key/security? #nosecurity目錄將該行刪除
????
???configsvr.conf
dbpath = /data/mongodb/configsvr
directoryperdb = true
configsvr = true
port = 20000
logpath =/data/mongodb/log/configsvr.log
logappend = true
fork = true
keyFile = /data/mongodb/key/security? #nosecurity目錄將該行刪除
????
???mongos.conf
configdb =10.10.113.122:20000, 10.10.113.132:20000, 10.10.113.133:20000
port = 30000
chunkSize = 1??#單位 mb 生成環(huán)境請使用 200 或刪除
logpath =/data/mongodb/log/mongos.log
logappend = true
fork = true
keyFile = /data/mongodb/key/security? #nosecurity目錄將該行刪除
???
??分片配置
???說明:分片要在無驗證環(huán)境中配置,否則會出現(xiàn)無權(quán)限等異常。采用以下命令啟動Server1\Server2\Server3上的shard1\shard2\shard3:
???# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/nosecurity/shard1.conf
???# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/nosecurity/shard2.conf
???# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/nosecurity/shard3.conf
???以下命令查看是否正常啟動:
???# netstat -lnpt # 或 ps -ef | grep mongo
???啟動后連接到shard1\shard2\shard3分別進行配置,在任意一臺服務(wù)器即可,以下是具體配置過程:
???# /usr/local/mongodb/bin/mongo --port 10001
???
???>use admin
???
???>config = {_id:"shard1", members: [
?????????? {_id: 0, host:"10.10.113.122:10001"},
?????????? {_id: 1, host:"10.10.113.132:10001"},
?????????? {_id: 2, host:"10.10.113.133:10001"}]
??????? };
???>rs.initiate(config)
???>exit
???# /usr/local/mongodb/bin/mongo --port 10002
???>use admin
???
???>config = {_id:"shard2", members: [
?????????? {_id: 0, host:"10.10.113.132:10002"},
?????????? {_id: 1, host:"10.10.113.133:10002"},
?????????? {_id: 2, host:"10.10.113.122:10002"}]
?????};
???>rs.initiate(config)
???>exit
???# /usr/local/mongodb/bin/mongo --port 10003
???
???>use admin
???>config = {_id:"shard3", members: [
?????????? {_id: 0, host:"10.10.113.133:10003"},
?????????? {_id: 1, host:"10.10.113.122:10003"},
?????????? {_id: 2, host:"10.10.113.132:10003"}]
?????};
???>rs.initiate(config)
???至此,已完成分片配置
???
??路由設(shè)置
???路由是能過config來連接分片服務(wù)器,在啟動路由進程時,先啟動配置進程,路由配置過程如下:
???# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/nosecurity/configsvr.conf
???# /usr/local/mongodb/bin/mongos -f /data/mongodb/conf/nosecurity/mongos.conf
???啟動后,連接路由進行分片添加,只需配置一臺路由。注:分片操作需在admin庫下進行,另外必需在無驗證要求下進行,即采用前面創(chuàng)建于nosecurity文件夾下的配置。
???# /usr/local/mongodb/bin/mongo --port 30000
???mongos> use admin
???mongos> db.runCommand({addshard:"shard1/10.10.113.122:10001,10.10.113.132:10001,10.10.113.133:10001",name:"shard1", maxsize:20480} )
???mongos> db.runCommand({addshard:"shard2/10.10.113.122:10002,10.10.113.132:10002,10.10.113.133:10002",name:"shard2", maxsize:20480} )
???mongos> db.runCommand({addshard:"shard3/10.10.113.122:10003,10.10.113.132:10003,10.10.113.133:10003",name:"shard3", maxsize:20480} )
???命令檢查分片添加情況,如出現(xiàn)以下結(jié)果則表示配置成功:
???mongos> db.runCommand( {listshards : 1 } )
??
??
??權(quán)限控制
???MongoDB默認(rèn)為驗證模式。如需對數(shù)據(jù)庫進行權(quán)限控制,需先采用無驗證模式登錄,進入admin庫創(chuàng)建管理員用戶后,再采用驗證模式登錄。通過前面創(chuàng)建的管理員帳號進行數(shù)據(jù)庫與用戶的創(chuàng)建。MongoDB集群的權(quán)限與單臺的權(quán)限控制的不同之處在于,單臺是通過-auth屬性,集群是通過keyFile來進行服務(wù)器間的驗證。以下介紹配置全過程。
???前面的所有步驟,都是在nosecurity模式下進行。如果沒有采用非驗證模式的需要將所有進程(分片、配置、mongos)停止,將切換到無驗證模式。
???步驟一:先進行登錄,并切換進admin庫創(chuàng)建管理員帳號
???# /usr/local/mongodb/bin/mongo --port 30000
???mongos>use admin
???mongos>db.addUser('admin','123456')
???{
?????"singleShard" :"192.168.2.88:20000,192.168.2.89:20000,192.168.2.90:20000",
?????"n" : 0,
?????"connectionId" : 211,
?????"err" : null,
?????"ok" : 1
???}
???{
?????"_id" :ObjectId("4f6c78ddad912a3ac6833ece"),
?????"user" : "admin",
?????"readOnly" : false,
?????"pwd" :"95ec4261124ba5951720b199908d892b"
???}
???驗證用戶名與密碼
???mongos> db.auth('admin','123456')
???1
???mongos>exit
???步驟二:退出后,將Server1\Server2\Server3服務(wù)器上MongoDB的所有進程(分片、配置、mongos)停止,將切換到驗證模式。具體命令如下:
???#killall mongod mongos
???#netstat -lnpt
???# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/security/shard1.conf
???# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/security/shard2.conf
???# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/security/shard3.conf
???# netstat -lnpt
???# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/security/configsvr.conf
???# /usr/local/mongodb/bin/mongos -f /data/mongodb/conf/security/mongos.conf
???啟動后,如對庫進行查看,則會報以下異常:
???# /usr/local/mongodb/bin/mongo 10.10.113.122:30000/admin
???MongoDB shell version: 2.0.4
???connecting to:10.10.113.122:30000/admin
???> show dbs
???Fri Mar 23 22:28:28 uncaughtexception: listDatabases failed:{ "ok" : 0, "errmsg" :"unauthorized" }
???以下是正常登錄后顯示的信息:
???# /usr/local/mongodb/bin/mongo 10.10.113.122:30000/admin
???MongoDB shell version: 2.0.4
???connecting to:10.10.113.122:30000/admin
???>db.auth('admin','123456')
???1
???mongos>
???步驟三:以下是數(shù)據(jù)庫及數(shù)據(jù)庫用戶創(chuàng)建的過程:
???mongos> use hello
???switched to db hello
???mongos>db.addUser('sa','sa')
???{
?????"singleShard" :"shard2/10.10.113.122:10002,10.10.113.132:10002,10.10.113.133:10002",
?????"n" : 0,
?????"lastOp" :NumberLong("5723101431532093441"),
?????"connectionId" : 38,
?????"err" : null,
?????"ok" : 1
???}
???{
?????"user" : "sa",
?????"readOnly" : false,
?????"pwd" :"75692b1d11c072c6c79332e248c4f699",
?????"_id" :ObjectId("4f6c8a6e9f67b049a20a00de")
???}
???mongos> exit
???bye
???# /usr/local/mongodb/bin/mongo 10.10.113.122:30000/hello -u sa -p
???MongoDB shell version: 2.0.4
???Enter password:
???connecting to:10.10.113.122:30000/hello
???> show collections
???system.indexes
???system.users
???> db.system.users.find()
???{ "_id" :ObjectId("4f6c8a6e9f67b049a20a00de"), "user" :"sa", "readOnly" : false, "pwd" :"75692b1d11c072c6c79332e248c4f699" }
??分片
???# /usr/local/mongodb/bin/mongo -port 30000
???
???> use test
???
???導(dǎo)入數(shù)據(jù)
???> use admin
???> db.runCommand({"enablesharding":"test"})
???> db.runCommand({"shardcollection":"test.doc","key":{"_id":1}})
???
第四節(jié)?replica sets + shard (3機高可用 , 一主一備份一仲裁)
?一?方案
??每個分片3服務(wù)器,前期采用三臺,日后服務(wù)器的增加考慮災(zāi)備,服務(wù)增加的基數(shù)最少為三臺(或用有雙機方案)。
??Server1 10.10.113.122?Server1 10.10.113.132?Server3 10.10.113.133
??
??類型????服務(wù)器???用途??? ???系統(tǒng)???說明
???
??存儲/數(shù)據(jù)???Server1?? ?Shard1/Shard2/Shard3??Linux 64位??Shard1:10001;Shard2:10002,Shard3:10003;
???????Server2???Shard1/Shard2/Shard3??Linux 64位??Shard1:10001;Shard2:10002,Shard3:10003;
???????Server3???Shard1/Shard2/Shard3??Linux 64位??Shard1:10001;Shard2:10002,Shard3:10003;
???
??配置????Server1???Config1??????Linux 64位??Config1:20000;
???????Server2???Config2??????Linux 64位??Config2:20000;?
???????Server3???Config3??????Linux 64位??Config3:20000;
???
??路由????Server1???Mongos1??????Linux 64位??Mongos:30000;
???????Server2???Mongos2??????Linux 64位??Mongos:30000;
???????Server3???Mongos3??????Linux 64位??Mongos:30000;
?
?二?準(zhǔn)備
??安裝mongodb 參照第一節(jié)
??
??
??linux 傳文件
??# scp mongodb-linux-x86_64-2.0.4.tgz?root@10.10.113.132:/root
??# ssh 10.10.113.132
?
?
??創(chuàng)建配置、日志、分片、key文件存儲目錄及驗證文件
??
??mkdir /data/mongodb/configsvr/ -p
??
??mkdir /data/mongodb/log/ -p
??
??mkdir /data/mongodb/shard1/ -p
??
??mkdir /data/mongodb/shard2/ -p
??
??mkdir /data/mongodb/shard3/ -p
??
??mkdir /data/mongodb/key/ -p
??
??mkdir /data/mongodb/conf/security/ -p
??mkdir /data/mongodb/conf/nosecurity/ -p
??
??創(chuàng)建配置文件
???1、 創(chuàng)建驗證文件security于/data/mongodb/key/目錄,關(guān)賦予可讀權(quán)限,命令如下:
???cd /data/mongodb/key/
???echo 'hycloudmongodbkey' > security
???chmod 600 /data/mongodb/key/security
???
???2、 創(chuàng)建shard1.conf、shard2.conf、shard3.conf、configsvr.conf、mongos.conf于/data/mongodb/conf/security/ 和 /data/mongodb/conf/nosecurity/的目錄,內(nèi)容分別如下:
???shard1.conf
???
dbpath = /data/mongodb/shard1
directoryperdb = true
shardsvr = true
replSet = shard1
#bind_ip = 10.10.113.122,localhost
port = 10001
oplogSize = 100
logpath =/data/mongodb/log/shard1.log
logappend = true
profile = 1
slowms = 5
rest = true
fork = true
keyFile = /data/mongodb/key/security? #nosecurity目錄將該行刪除
????
???shard2.conf
dbpath = /data/mongodb/shard2
directoryperdb = true
shardsvr = true
replSet = shard2
#bind_ip = 10.10.113.122,localhost
port = 10002
oplogSize = 100
logpath = /data/mongodb/log/shard2.log
logappend = true
profile = 1
slowms = 5
rest = true
fork = true
keyFile = /data/mongodb/key/security? #nosecurity目錄將該行刪除
????
???shard3.conf
dbpath = /data/mongodb/shard3
directoryperdb = true
shardsvr = true
replSet = shard3
#bind_ip = 10.10.113.122,localhost
port = 10003
oplogSize = 100
logpath = /data/mongodb/log/shard3.log
logappend = true
profile = 1
slowms = 5
rest = true
fork = true
keyFile = /data/mongodb/key/security? #nosecurity目錄將該行刪除
????
???configsvr.conf
dbpath = /data/mongodb/configsvr
directoryperdb = true
configsvr = true
port = 20000
logpath =/data/mongodb/log/configsvr.log
logappend = true
fork = true
keyFile = /data/mongodb/key/security? #nosecurity目錄將該行刪除
????
???mongos.conf
configdb =10.10.113.122:20000, 10.10.113.132:20000, 10.10.113.133:20000
port = 30000
chunkSize = 1??#單位 mb 生成環(huán)境請使用 200 或刪除
logpath =/data/mongodb/log/mongos.log
logappend = true
fork = true
keyFile = /data/mongodb/key/security? #nosecurity目錄將該行刪除
???
??分片配置
???說明:分片要在無驗證環(huán)境中配置,否則會出現(xiàn)無權(quán)限等異常。采用以下命令啟動Server1\Server2\Server3上的shard1\shard2\shard3:
???# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/nosecurity/shard1.conf
???# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/nosecurity/shard2.conf
???# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/nosecurity/shard3.conf
???以下命令查看是否正常啟動:
???# netstat -lnpt # 或 ps -ef | grep mongo
???啟動后連接到shard1\shard2\shard3分別進行配置,在任意一臺服務(wù)器即可,以下是具體配置過程:(注意不能在仲裁機器初始化)
???# /usr/local/mongodb/bin/mongo --port 10001
???
???>use admin
???
???>config = {_id:"shard1", members: [
?????????? {_id: 0, host:"10.10.113.122:10001"},
?????????? {_id: 1, host:"10.10.113.132:10001"},
?????????? {_id: 2, host:"10.10.113.133:10001","arbiterOnly":true}]
??????? };
???>rs.initiate(config)
???>exit
???# /usr/local/mongodb/bin/mongo --port 10002
???>use admin
???
???>config = {_id:"shard2", members: [
?????????? {_id: 0, host:"10.10.113.132:10002"},
?????????? {_id: 1, host:"10.10.113.133:10002"},
?????????? {_id: 2, host:"10.10.113.122:10002","arbiterOnly":true}]
?????};
???>rs.initiate(config)
???>exit
???
???# /usr/local/mongodb/bin/mongo --port 10003
???
???>use admin
???>config = {_id:"shard3", members: [
?????????? {_id: 0, host:"10.10.113.133:10003"},
?????????? {_id: 1, host:"10.10.113.122:10003"},
?????????? {_id: 2, host:"10.10.113.132:10003","arbiterOnly":true}]
?????};
???>rs.initiate(config)
???至此,已完成分片配置
???
??路由設(shè)置
???路由是能過config來連接分片服務(wù)器,在啟動路由進程時,先啟動配置進程,路由配置過程如下:
???# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/nosecurity/configsvr.conf
???# /usr/local/mongodb/bin/mongos -f /data/mongodb/conf/nosecurity/mongos.conf
???啟動后,連接路由進行分片添加,只需配置一臺路由。注:分片操作需在admin庫下進行,另外必需在無驗證要求下進行,即采用前面創(chuàng)建于nosecurity文件夾下的配置。
???# /usr/local/mongodb/bin/mongo --port 30000
???mongos> use admin
???# maxsize:20480 #單位 mb 分片限制大小 根據(jù)實際服務(wù)器來定
???mongos> db.runCommand({addshard:"shard1/10.10.113.122:10001,10.10.113.132:10001,10.10.113.133:10001",name:"shard1", maxsize:20480} )
???mongos> db.runCommand({addshard:"shard2/10.10.113.132:10002,10.10.113.133:10002,10.10.113.122:10002",name:"shard2", maxsize:20480} )
???mongos> db.runCommand({addshard:"shard3/10.10.113.133:10003,10.10.113.122:10003,10.10.113.132:10003",name:"shard3", maxsize:20480} )
???命令檢查分片添加情況,如出現(xiàn)以下結(jié)果則表示配置成功:
???mongos> db.runCommand( {listshards : 1 } )
??
??
??權(quán)限控制
???MongoDB默認(rèn)為驗證模式。如需對數(shù)據(jù)庫進行權(quán)限控制,需先采用無驗證模式登錄,進入admin庫創(chuàng)建管理員用戶后,再采用驗證模式登錄。通過前面創(chuàng)建的管理員帳號進行數(shù)據(jù)庫與用戶的創(chuàng)建。MongoDB集群的權(quán)限與單臺的權(quán)限控制的不同之處在于,單臺是通過-auth屬性,集群是通過keyFile來進行服務(wù)器間的驗證。以下介紹配置全過程。
???前面的所有步驟,都是在nosecurity模式下進行。如果沒有采用非驗證模式的需要將所有進程(分片、配置、mongos)停止,將切換到無驗證模式。
???步驟一:先進行登錄,并切換進admin庫創(chuàng)建管理員帳號
???# /usr/local/mongodb/bin/mongo --port 30000
???mongos>use admin
???mongos>db.addUser('admin','123456')
???{
?????"singleShard" :"192.168.2.88:20000,192.168.2.89:20000,192.168.2.90:20000",
?????"n" : 0,
?????"connectionId" : 211,
?????"err" : null,
?????"ok" : 1
???}
???{
?????"_id" :ObjectId("4f6c78ddad912a3ac6833ece"),
?????"user" : "admin",
?????"readOnly" : false,
?????"pwd" :"95ec4261124ba5951720b199908d892b"
???}
???驗證用戶名與密碼
???mongos> db.auth('admin','123456')
???1
???mongos>exit
???步驟二:退出后,將Server1\Server2\Server3服務(wù)器上MongoDB的所有進程(分片、配置、mongos)停止,將切換到驗證模式。具體命令如下:
???# killall mongod mongos
???# netstat -lnpt
???# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/security/shard1.conf
???# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/security/shard2.conf
???# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/security/shard3.conf
???# netstat -lnpt # ps -ef | grep mongo
???# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/security/configsvr.conf
???# /usr/local/mongodb/bin/mongos -f /data/mongodb/conf/security/mongos.conf
???啟動后,如對庫進行查看,則會報以下異常:
???# /usr/local/mongodb/bin/mongo 10.10.113.122:30000/admin
???MongoDB shell version: 2.0.4
???connecting to:10.10.113.122:30000/admin
???> show dbs
???Fri Mar 23 22:28:28 uncaughtexception: listDatabases failed:{ "ok" : 0, "errmsg" :"unauthorized" }
???以下是正常登錄后顯示的信息:
???# /usr/local/mongodb/bin/mongo 10.10.113.122:30000/admin
???MongoDB shell version: 2.0.4
???connecting to:10.10.113.122:30000/admin
???>db.auth('admin','123456')
???1
???mongos>
???步驟三:以下是數(shù)據(jù)庫及數(shù)據(jù)庫用戶創(chuàng)建的過程:
???mongos> use hello
???switched to db hello
???mongos>db.addUser('sa','sa')
???{
?????"singleShard" :"shard2/10.10.113.122:10002,10.10.113.132:10002,10.10.113.133:10002",
?????"n" : 0,
?????"lastOp" :NumberLong("5723101431532093441"),
?????"connectionId" : 38,
?????"err" : null,
?????"ok" : 1
???}
???{
?????"user" : "sa",
?????"readOnly" : false,
?????"pwd" :"75692b1d11c072c6c79332e248c4f699",
?????"_id" :ObjectId("4f6c8a6e9f67b049a20a00de")
???}
???mongos> exit
???bye
???# /usr/local/mongodb/bin/mongo 10.10.113.122:30000/hello -u sa -p
???MongoDB shell version: 2.0.4
???Enter password:
???connecting to:10.10.113.122:30000/hello
???> show collections
???system.indexes
???system.users
???> db.system.users.find()
???{ "_id" :ObjectId("4f6c8a6e9f67b049a20a00de"), "user" :"sa", "readOnly" : false, "pwd" :"75692b1d11c072c6c79332e248c4f699" }
??分片
???# /usr/local/mongodb/bin/mongo -port 30000
???
???> use admin
???> db.runCommand({"enablesharding":"test"})
???> db.runCommand({"shardcollection":"test.doc","key":{"_id":1}})
???> db.runCommand({"shardcollection":"test.txt","key":{"_id":1}})
???索引
???> use test
???> db.fs.chunks.ensureIndex({files_id: 1});??
???> use admin
???> db.runCommand({ shardcollection : "test.fs.chunks", key : { files_id : 1 }})?
???
???索引
???> use test
???
???> db.doc.ensureIndex({"letter":1})
???
???導(dǎo)入數(shù)據(jù)
???
???查詢數(shù)據(jù)
???
???分片管理
???> use config
???> show collections
???> db.shards.find()
???> db.databases.find()
???> db.chunks.find()
???
???> db.printShardingStatus()
???
? ?
轉(zhuǎn)載于:https://blog.51cto.com/feihan21/1592719
總結(jié)
以上是生活随笔為你收集整理的mongodb 简单部署方案及实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# winform打印总结 z
- 下一篇: Entity FrameWork 365