03. MongoDB Replica Set+Sharding
#事前準備(每台4 vcpu,8G ram,300G HD)
192.168.100.13 mongodb-01
192.168.100.14 mongodb-02
192.168.100.15 mongodb-03
#以上每台皆如下安裝與設定
#=============================================================
#設定mongodb-org.repo
vim /etc/yum.repos.d/mongodb-org.repo
#新增下列內容
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
#安裝mongodb-org
yum install mongodb-org -y
#啟用與設定開起啟動mongod
systemctl daemon-reload
systemctl enable mongod.service
systemctl restart mongod.service
#連限到第一台mongodb-01
mongo mongodb-01:27019
#輸入以下內容
use admin
db.createUser( {
user: "myUserAdmin",
pwd: "你的密碼",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
});
db.createUser( {
user: "siteRootAdmin",
pwd: "你的密碼",
roles: [ { role: "root", db: "admin" } ]
});
exit
#於mongodb-01輸入下列內容
openssl rand -base64 741 > /var/lib/mongo/mongodb-keyfile
chmod 600 /var/lib/mongo/mongodb-keyfile
chown mongod.mongod /var/lib/mongo/mongodb-keyfile
#將mongodb-keyfile放至mongodb-02與mongodb-03的/var/lib/mongo/目錄下
#並執行下列語法更改權限
chmod 600 /var/lib/mongo/mongodb-keyfile
chown mongod.mongod /var/lib/mongo/mongodb-keyfile
#編輯vim /etc/mongod.conf
security:
keyFile: /var/lib/mongo/mongodb-keyfile
replication:
replSetName: rs-a