一:安裝MongoDB 常用的有兩種方法:我采用的是方法2:
讓客戶(hù)滿意是我們工作的目標(biāo),不斷超越客戶(hù)的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶(hù),將通過(guò)不懈努力成為客戶(hù)在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名與空間、網(wǎng)站空間、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、香洲網(wǎng)站維護(hù)、網(wǎng)站推廣。
方法1 安裝包方式,直接下載.tgz文件,然后解壓即可:
https://www.mongodb.com/download-center/enterprise # 下載需要的版本
下載percona for mongodb
https://www.percona.com/downloads/percona-server-mongodb-LATEST/tar -zxvf mongodb-linux-x86_64-3.0.6.tgz # 解壓mv mongodb-linux-x86_64-3.0.6/ /usr/bin/mongodb/bin/mongod # 將解壓包拷貝到指定目錄
這種啟動(dòng)方式如下:
/usr/bin/mongodb/bin/mongod -f /etc/mongod.conf
方法2 yum 安裝,然后默認(rèn)配置文件在/etc/mongod.conf,這樣安裝的mongodb
的相關(guān)命令都在/usr/bin下面的!
安裝yum源(percona自己的yum源)
# yum -y install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
yum list | grep mongo #選擇合適版本
[root@beijing-fuli-hadoop-02 bin]# yum install Percona-Server-MongoDB-34-mongos.x86_64
這種方法的啟動(dòng)方式:
service mongod start
二:生成 keyfile
[root@beijing-fuli-hadoop-02 mongodb]# openssl rand -base64 756 >/data/mongodb/config/mongodb.key
三:編寫(xiě)配置文件
1.注意文件的空格和大小寫(xiě)都會(huì)導(dǎo)致啟動(dòng)失敗
2.如果配置了配置了clusterAuthMode,那么一定要提前生成keyfile
3.關(guān)于節(jié)點(diǎn)的優(yōu)先級(jí)的設(shè)置,具有較高Priority的Secondary相對(duì)于較低Priority的Secondary
會(huì)更早的發(fā)起選舉,也更容易成為新的Primary節(jié)點(diǎn),但是即使優(yōu)先級(jí)低的Secondary
也可以在段時(shí)間內(nèi)被選舉為Primary節(jié)點(diǎn),重點(diǎn)來(lái)了,這種情況發(fā)生的時(shí)候副本集就會(huì)繼續(xù)
進(jìn)行選舉,直到可用的最高優(yōu)先級(jí)Secondary被選舉為Primary,所以當(dāng)你想讓特定的
Secondary節(jié)點(diǎn)成為主的時(shí)候,你才有必要配置不同的Primary,否則你只需要配置成一樣的
Priority(例如都是1即可)
4.關(guān)于vote的設(shè)置:從mongodb 3.2開(kāi)始,無(wú)投票權(quán)的成員必須配置Priority為0,priority大于
0的成員其vote必須配置為1
四:如果你想用非root來(lái)啟動(dòng)mongodb,那么就創(chuàng)建 mongodb的賬號(hào)和屬組
[root@beijing-fuli-hadoop-02 init.d]# groupadd -g 666 mongod
[root@beijing-fuli-hadoop-02 init.d]# useradd -m -s /bin/bash -g mongod -u 666 mongod
五:?jiǎn)?dòng)數(shù)據(jù)庫(kù):三個(gè)節(jié)點(diǎn)都啟動(dòng)
[mongod@beijing-fuli-hadoop-02 db]$ /usr/bin/mongodb/bin/mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 89234
child process started successfully, parent exiting
六:轉(zhuǎn)為復(fù)制集架構(gòu)
> use admin # 切換到 admin 數(shù)據(jù)庫(kù)switched to db admin # 初始化副本集,副本集名稱(chēng)為 liuhe_rs ,第一個(gè)成員為自己本身,然后他會(huì)被選舉為主> rs.initiate({_id:'liuhe_rs',members: [{ _id: 0 , host: "10.9.21.178:27017"},{ _id: 1 , host: "10.9.21.179:27017"},{ _id: 2 , host: "10.9.21.114:27017"}]}) { "ok" : 1 }
或者如下:
>config={_id:'liuhe_rs',members: [{ _id: 0 , host: "10.9.21.178:27017"},{ _id: 1 , host: "10.9.21.179:27017"},{ _id: 2 , host: "10.9.21.114:27017"}]}
>rs.initiate(config)
七:?jiǎn)?dòng)報(bào)錯(cuò):
7.1,參數(shù)indexBuildRetry不能在副本集中配置
[root@beijing-fuli-hadoop-02 mongodb]# /usr/bin/mongodb/bin/mongod -f /etc/mongod.conf
2019-11-11T22:36:15.551+0800 F CONTROL [main] Failed global initialization: BadValue: replication.replSetName is not allowed when storage.indexBuildRetry is specified
報(bào)錯(cuò)內(nèi)容:
Failed global initialization: BadValue: replication.replSetName is not allowed when storage.indexBuildRetry is specified
storage 參數(shù) indexBuildRetry 不能與 副本集共存,當(dāng)開(kāi)啟 副本集的時(shí)候,就需要將indexBuildRetry 參數(shù)注釋。官網(wǎng)鏈接
Changed in version 4.0: The setting storage.indexBuildRetry cannot be used in conjunction withreplication.replSetName.
7.2.啟動(dòng)報(bào)錯(cuò) error number 100
啟動(dòng)的時(shí)候會(huì)在數(shù)據(jù)目錄下生成相應(yīng)的初始化庫(kù)以及特定的問(wèn)題,所以
要保證這個(gè)目錄是空的
[mongod@beijing-fuli-hadoop-02 ~]$ /usr/bin/mongodb/bin/mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 89082
ERROR: child process failed, exited with error number 100
To see additional information in this output, start without the "--fork" option.
可能是因?yàn)?你的 數(shù)據(jù)庫(kù)目錄下有別的文件,清空即可!
7.3 啟動(dòng)報(bào)錯(cuò):error number 14
[mongod@beijing-fuli-hadoop-04 log]$ /usr/bin/mongodb/bin/mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 100524
ERROR: child process failed, exited with error number 14
To see additional information in this output, start without the "--fork" option.
查看日志發(fā)現(xiàn)如下:
2019-11-16T16:51:37.938+0800 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock Operation not permitted
原來(lái)是之前用root賬號(hào)啟動(dòng)的,生成了一個(gè)root用戶(hù)的mongodb-27017.sock文件,然后又用mongod
用戶(hù)啟動(dòng),結(jié)果沒(méi)有權(quán)限操作,刪除這個(gè)文件即可,然后用mongod用戶(hù)啟動(dòng)會(huì)自動(dòng)生成一個(gè)
屬于mongod的文件,如下:
[root@beijing-fuli-hadoop-02 tmp]# ll mongodb-27017.sock
srwx------ 1 mongod mongod 0 Nov 16 17:12 mongodb-27017.sock
那么mongodb-27017.sock這個(gè)文件是做什么的?
服務(wù)器與本地客戶(hù)端進(jìn)行通信的Unix套接字文件。
啟動(dòng)時(shí)會(huì)打開(kāi)。
默認(rèn)的存儲(chǔ)路徑是/tmp/mongodb-
八:mongodb的基本維護(hù)操作命令:
8.1:查看mongodb 副本集的狀態(tài)
MongoDB Enterprise liuhe_rs:PRIMARY> rs.status()
8.2:允許secondary節(jié)點(diǎn)可以寫(xiě)
對(duì)于replica set 中的secondary 節(jié)點(diǎn)默認(rèn)是不可讀的,在寫(xiě)多讀少的應(yīng)用中,使用Replica Sets來(lái)實(shí)現(xiàn)讀寫(xiě)分離。通過(guò)在連接時(shí)指定或者在主庫(kù)指定slaveOk,由Secondary來(lái)分擔(dān)讀的壓力,Primary只承擔(dān)寫(xiě)操作
第一種方法:db.getMongo().setSlaveOk();#從庫(kù)執(zhí)行,并且是臨時(shí)啟作用,
第二種方法:rs.slaveOk(); #從庫(kù)執(zhí)行,并且是臨時(shí)啟作用,
上面的兩種方法都是臨時(shí)起作用的,下次再通過(guò)mongo進(jìn)入實(shí)例的時(shí)候,查詢(xún)?nèi)匀粫?huì)報(bào)錯(cuò),為此可以通過(guò)下列方式
[mongod@beijing-fuli-hadoop-04 ~]$ vi .mongorc.js
rs.slaveOk();
這樣的話以后每次通過(guò)mongo命令進(jìn)入都可以查詢(xún)了
8.3.查看當(dāng)前mongodb連接的那個(gè)庫(kù)
> use liuwenhe
switched to db liuwenhe
> db.stats()
{
"db" : "liuwenhe",
"collections" : 1,
"views" : 0,
"objects" : 1,
"avgObjSize" : 48,
"dataSize" : 48,
"storageSize" : 16384,
"numExtents" : 0,
"indexes" : 1,
"indexSize" : 16384,
"fsUsedSize" : 18297913344,
"fsTotalSize" : 75949613056,
"ok" : 1
}
8.4查看副本集從庫(kù)狀態(tài)信息(延遲、成員等)
MongoDB Enterprise liuhe_rs:PRIMARY> rs.printSlaveReplicationInfo()
source: 10.9.21.179:27017
syncedTo: Sat Nov 16 2019 21:56:29 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
source: 10.9.21.114:27017
syncedTo: Sat Nov 16 2019 21:56:29 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
如上所示,有兩個(gè)從庫(kù),分別是10.9.21.179、10.9.21.114,然后同步到了主庫(kù)的oplogs
Sat Nov 16 2019 21:56:29 的時(shí)間點(diǎn)了,沒(méi)有延遲;
8.5:查看副本集的oplog信息:
MongoDB Enterprise liuhe_rs:PRIMARY> rs.printReplicationInfo()
configured oplog size: 51200MB
log length start to end: 424168secs (117.82hrs)
oplog first event time: Tue Nov 12 2019 00:10:41 GMT+0800 (CST)
oplog last event time: Sat Nov 16 2019 22:00:09 GMT+0800 (CST)
now: Sat Nov 16 2019 22:00:16 GMT+0800 (CST)
8.6查看某個(gè)庫(kù)下的用戶(hù):
MongoDB Enterprise liuhe_rs:PRIMARY> use admin
switched to db admin
MongoDB Enterprise liuhe_rs:PRIMARY> show users
九:創(chuàng)建管理員用戶(hù)
use admin
db.createUser(
{
user: "liuwenhe",
pwd: "liuwenhe",
roles: [ { role: "root", db: "admin" } ]
}
)
MongoDB Enterprise liuhe_rs:PRIMARY> db.auth('liuwenhe','liuwenhe')
登錄數(shù)據(jù)庫(kù):
mongo -u liuwenhe -p liuwenhe --authenticationDatabase admin
創(chuàng)建普通賬號(hào):
db.createUser({user:'user', pwd:'123456', roles:[ {role:'readWrite', db:'liuwenhe'}]})