該文檔默認(rèn)命名和配置:
- 復(fù)制集名稱為:ulocation
- 三臺(tái)mongo實(shí)例,192.168.50.222(primary,第一臺(tái)),192.168.50.223(secondary,第二胎),192.168.50.223(arbiter,第三臺(tái))
- mongo數(shù)據(jù)目錄:/var/lib/mongo/,日志目錄/var/log/mongodb/,備份目錄:/app/mongo/backup,keyfile文件:/app/mongo/mongo.keyfile
mongo centos安裝卸載
注: 如下操作是在ip、防火墻、selinux都配置好后進(jìn)行的。
1.配置yum源
vim /etc/yum.repos.d/mongodb-enterprise.repo
[mongodb-enterprise]
name=MongoDB Enterprise Repository
baseurl=https://repo.mongodb.com/yum/redhat/$releasever/mongodb-enterprise/4.0/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
2.yum安裝
yum install -y mongodb-enterprise-4.0.6 mongodb-enterprise-server-4.0.6 mongodb-enterprise-shell-4.0.6 mongodb-enterprise-mongos-4.0.6 mongodb-enterprise-tools-4.0.6
3.防止yum update更新
vim /etc/yum.conf
exclude=mongodb-enterprise,mongodb-enterprise-server,mongodb-enterprise-shell,mongodb-enterprise-mongos,mongodb-enterprise-tools
4.常用命令
sudo service mongod start
sudo chkconfig mongod on
sudo service mongod stop
sudo service mongod restart
5.mongo centos移除
sudo service mongod stop
sudo yum erase $(rpm -qa | grep mongodb-enterprise)
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongo
以上5部驟參考:https://docs.mongodb.com/manual/tutorial/install-mongodb-enterprise-on-red-hat/
6.Percona Server for MongoDB 4.0 安裝
yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
percona-release enable psmdb-40 release
yum install percona-server-mongodb
service mongod start
chkconfig mongod on
service mongod stop
mongo開啟身份認(rèn)證(復(fù)制集環(huán)境)
https://docs.mongodb.com/manual/tutorial/enable-authentication/
創(chuàng)建keyfile
mkdir -p /app/mongo
openssl rand -base64 756 > /app/mongo/mongo.keyfile
chmod 400 /app/mongo/mongo.keyfile
chown mongod:mongod /app/mongo/mongo.keyfile?
創(chuàng)建超級(jí)管理員
mongo
use admin
db.createUser(
{
user: "admin",
pwd: "admin@13579*01",
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, { "role" : "clusterAdmin", "db" : "admin" }, "readWriteAnyDatabase","root" ]
}
)
創(chuàng)建ulocation管理員
use ulocation
db.createUser(
{
user: "ulocation",
pwd: "ulocation2019",
roles: [ { role: "readWrite", db: "ulocation" }]
}
)
若為復(fù)制集,需將keyfile拷貝到其他節(jié)點(diǎn)上。
關(guān)閉mongo實(shí)例,修改配置文件:
security:
authorization: enabled
keyFile: /app/mongo/mongo.keyfile?
使用創(chuàng)建的用戶登錄
mongo --port 27017 -u "admin" --authenticationDatabase "admin" -p
mongo --port 27017 -u "ulocation" --authenticationDatabase "ulocation" -p
--authenticationDatabase : 指定用戶在哪個(gè)庫中創(chuàng)建,若不指定,mongo使用連接串中的數(shù)據(jù)庫。
Standalone mongo實(shí)例轉(zhuǎn)為復(fù)制集
1.關(guān)閉第一臺(tái)實(shí)例
mongo
use admin
db.shutdownServer()
2.第一臺(tái)實(shí)例增加復(fù)制集啟動(dòng)配置項(xiàng),并啟動(dòng)
vim /etc/mongod.conf
replication:
replSetName: ulocation
enableMajorityReadConcern: false # PSA架構(gòu)需關(guān)閉此項(xiàng)
service mongod start
3.第一臺(tái)實(shí)例配置復(fù)制集信息
mongo
config = {
"_id":"ulocation",
"members":[
{"_id":0,"host":"192.168.50.222:27017"},
]
}
rs.initiate(config)
該節(jié)點(diǎn)為primary節(jié)點(diǎn)
4.新增secondary mongo
在第二臺(tái)mongo實(shí)例,操作1、2兩部
在primary上執(zhí)行:
rs.add( { host: "192.168.50.223:27017", priority: 0, votes: 0 } )
調(diào)用該命令后,會(huì)自動(dòng)執(zhí)行Initial Sync,進(jìn)行數(shù)據(jù)同步
修改priority和vote(原因:When a newly added secondary has its votes and priority settings greater than zero, during its initial sync, the secondary still counts as a voting member even though it cannot serve reads nor become primary because its data is not yet consistent.)
var cfg = rs.conf();
cfg.members[1].priority = 1
cfg.members[1].votes = 1
rs.reconfig(cfg)
5.新增仲裁成員
在第三臺(tái)mongo實(shí)例,操作1、2兩部
在primary上執(zhí)行:
rs.addArb("192.168.50.224:27017")
參考:https://docs.mongodb.com/manual/tutorial/convert-standalone-to-replica-set/
https://docs.mongodb.com/manual/tutorial/expand-replica-set/
https://docs.mongodb.com/manual/tutorial/add-replica-set-arbiter/
Initial Sync數(shù)據(jù)同步
兩種方式:
方式一:在primary上執(zhí)行:rs.add( { host: "192.168.50.223:27017", priority: 0, votes: 0 } )。注:前提是空的數(shù)據(jù)目錄
方式二:拷貝primary上的數(shù)據(jù)文件,步驟如下:
1.停掉primary,拷貝數(shù)據(jù)文件
2.停掉secondary將拷貝的數(shù)據(jù)文件移到secondary的數(shù)據(jù)目錄
3.rm -rf mongod.lock(不刪除會(huì)報(bào)“Attempted to create a lock file on a read-only directory: /var/lib/mongo”)
chown -R mongod:mongod *
4.啟動(dòng)secondary上的mongo實(shí)例
Percona Server熱備份
mkdir -p /app/mongo/backup
chown -R mongod:mongod /app/mongo/backup
> use admin
switched to db admin
> db.runCommand({createBackup: 1, backupDir: "/app/mongo/backup"})
{ "ok" : 1 }
mongo shell常用語句
庫操作:
show dbs;
use test;
test.dropDatabase();
查詢語句:
日期:ISODate("2012-12-19T06:01:17.171Z")
db.inventory.find( {} )
db.inventory.find( { status: "D" } )
db.inventory.find( { status: { $in: [ "A", "D" ] } } )
db.inventory.find( { status: "A", qty: { $lt: 30 } } )
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
賦角色:
db.grantRolesToUser(
"dayunAdmin",
[
{ role: "root", db: "admin" }
]
)
復(fù)制集
rs.conf()
rs.status()
db.printReplicationInfo()
rs.printSlaveReplicationInfo()
FAQ
-
(PSA) architecture關(guān)閉enableMajorityReadConcern
Read Concern "majority": https://docs.mongodb.com/manual/reference/read-concern-majority/#disable-read-concern-majority
-
主節(jié)點(diǎn)db.shutdownServer(),會(huì)出現(xiàn)失敗
ulocation:PRIMARY> db.shutdownServer() 2019-04-17T12:34:12.606+0800 E QUERY [js] Error: shutdownServer failed: { "operationTime" : Timestamp(1555475638, 1), "ok" : 0, "errmsg" : "No electable secondaries caught up as of 2019-04-17T12:34:12.604+0800Please use the replSetStepDown command with the argument {force: true} to force node to step down.", "code" : 262, "codeName" : "ExceededTimeLimit", "$clusterTime" : { "clusterTime" : Timestamp(1555475638, 1), "signature" : { "hash" : BinData(0,"dHvSJ4hy78YRfqf0hSAotb29wuo="), "keyId" : NumberLong("6680450114256896002") } } } :解決:先降級(jí),db.adminCommand( { replSetStepDown: 120, secondaryCatchUpPeriodSecs: 15, force: true } ),然后,關(guān)閉。
-
先正常運(yùn)行,修改配置后運(yùn)行不成功,還原回去后還不能正常運(yùn)行
刪除 /tmp/mongodb-27017.sock