0. 서버 구성
- 서버2대
- 서버 1 : mongos,config,shard1~3
- 서버 2 : shard1~3
1. mongodb 설치
# mongodb repository 등록
vi /etc/yum.repos.d/mongodb-org-3.4.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
# 설치
yum install -y mongodb-org
2. 디렉토리 생성
# 디렉토리 생성 1번서버
mkdir /home/mongodb/config
mkdir /home/mongodb/data
mkdir /home/mongodb/data/conf
mkdir /home/mongodb/data/rs1
mkdir /home/mongodb/data/rs2
mkdir /home/mongodb/data/rs3
mkdir /home/mongodb/log
# 디렉토리 생성 2번서버
mkdir /home/mongodb/config
mkdir /home/mongodb/data
mkdir /home/mongodb/data/rs1
mkdir /home/mongodb/data/rs2
mkdir /home/mongodb/data/rs3
mkdir /home/mongodb/log
3. 키파일 생성
# 서버 1에서 키파일 생성
openssl rand -base64 755 > /home/mongodb/config/mongodb-keyfile
# 파일 권한 변경
chmod 600 /home/mongodb/config/mongodb-keyfile
# 서버 2로 파일 복사
4. conf 파일 생성
- security 는 주석처리
- 계정 생성후 주석 제거
# 1번서버
vi /home/mongodb/config/mongod.config.conf
vi /home/mongodb/config/mongod.shard.rs1.conf
vi /home/mongodb/config/mongod.shard.rs2.conf
vi /home/mongodb/config/mongod.shard.rs3.conf
vi /home/mongodb/config/mongos.config.conf
# 2번서버
vi /home/mongodb/config/mongod.shard.rs1.conf
vi /home/mongodb/config/mongod.shard.rs2.conf
vi /home/mongodb/config/mongod.shard.rs3.conf
#mongos
sharding:
configDB: configRepl/172.16.1.13:20000
net:
port: 20001
processManagement:
fork: true
systemLog:
destination: file
path: /home/mongodb/log/mongos.log
logAppend: true
#security:
# keyFile: /home/mongodb/config/mongodb-keyfile
#config
sharding:
clusterRole: configsvr
replication:
replSetName: "configRepl"
net:
port: 20000
storage:
dbPath: /home/mongodb/data/conf
journal:
enabled: true
engine: "wiredTiger"
processManagement:
fork: true
systemLog:
destination: file
path: /home/mongodb/log/mongod-config.log
logAppend: true
#security:
# keyFile: /home/mongodb/config/mongodb-keyfile
# authorization: 'enabled'
#shard1~3
sharding:
clusterRole: shardsvr
net:
port: 30001
storage:
dbPath: /home/mongodb/data/rs1~rs3
journal:
enabled: true
engine: "wiredTiger"
wiredTiger:
engineConfig:
cacheSizeGB: 8
processManagement:
fork: true
systemLog:
destination: file
path: /home/mongodb/log/mongod-shard-rs1~rs3.log
logAppend: true
#security:
# keyFile: /home/mongodb/config/mongodb-keyfile
# authorization: 'enabled'
5. 인스턴스 실행
# 1번 서버
# config 인스턴스 실행
mongod --config /home/mongodb/config/mongod.config.conf
# config 서버 설정
mongo 1번서버IP:20000
rs.initiate( {
_id : "configRepl",
members: [
{ _id: 0, host: "1번서버IP:20000" }
]
})
# 샤드 인스턴스 실행
mongod --config /home/mongodb/config/mongod.shard.rs1.conf
mongod --config /home/mongodb/config/mongod.shard.rs2.conf
mongod --config /home/mongodb/config/mongod.shard.rs3.conf
# 라우터 인스턴스 실행
mongos --config /home/mongodb/config/mongos.config.conf
6. 계정 생성
# 계정 생성
mongo 1번서버IP:20000
mongo 1번서버IP:30001
mongo 1번서버IP:30002
mongo 1번서버IP:30003
mongo 1번서버IP:20001
mongo 2번서버IP:30001
mongo 2번서버IP:30002
mongo 2번서버IP:30003
use admin
db.createUser({user: "root", pwd: "!!tltmxpa8674", roles:["root"]})
use testdb
db.createUser( { user: "testuser", pwd: "!!test123", roles: [ { role: "readWrite", db: "testdb" }] } )
7. 샤드 설정
# 라우터 서버 접속
mongo 1번서버IP:20001
# 샤드 추가
sh.addShard("1번서버IP:30001")
sh.addShard("1번서버IP:30002")
sh.addShard("1번서버IP:30003")
sh.addShard("2번서버IP:30001")
sh.addShard("2번서버IP:30002")
sh.addShard("2번서버IP:30003")
# 상태 확인
sh.status()
sh.enableSharding("testdb")
use testdb
# 컬렉션 인덱스 생성
db.컬렉션명.createIndex({"_id": "hashed"})
sh.shardCollection("db명.컬렉션명", {"_id": "hashed"})
ex)
db.userInfoDocument.createIndex({"_id": "hashed"})
sh.shardCollection("testdb.userInfoDocument", {"_id": "hashed"})
8. security 설정
- security 주석 제거 후 인스턴스 재시작
9. logrotate 설정
vi /etc/logrotate.d/mongo
/home/mongodb/log/*.log {
su root root
daily
missingok
copytruncate
rotate 30
dateext
}
광고 : https://play.google.com/store/apps/details?id=com.ljo.blocktube
'서버설정 및 설치 > DB' 카테고리의 다른 글
Redis 설치 (0) | 2018.07.11 |
---|---|
MongoDB 설치 (0) | 2018.07.11 |
MariaDB 설치 (0) | 2018.07.11 |