1. 서버 설정/정보 확인하기

/* 서버 빌드 정보 (버전 포함) */
db.serverBuildInfo();

/* 서버 런타임 정보 (시스템 정보) */
db.serverStatus();

/* 실행 옵션 (MongoDB 설정) */
db.serverCmdLineOpts();

/* 해당 DB 의 Storage 정보 (간략 정보) */
db.stats();


2. Replica Set 정보 확인하기

/* Replica Set 멤버 설정 */

rs.conf();


/* Replica Set 멤버 상태 */

rs.status();



Posted by jungtae17
,

1. 설정 파일에 ReplicaSet 옵션을 추가한다.

replication:
  replSetName: {ReplicaSet이름}

2. MongoDB 서버 실행 후 Primary 가 될 MongoDB 서버로 접속한다.

   a. ReplicaSet 초기화한다.
rs.initiate();

   b. Primary 이름을 변경한다.
conf = rs.conf();

conf.members[0].host = "IP주소:포트";

rs.reconfig(conf);
   참고> MongoDB 는 hostname 을 기본 값으로 사용하기 때문에 문제가 생기는 경우가 있음.

3. ReplicaSet 멤버를 추가한다.

/* 일반 멤버 추가 */
rs.add("IP주소:포트");

/* Arbiter 멤버 추가 */
rs.addArb("IP주소:포트");


4. Delayed 멤버를 설정해야 될 경우

conf = rs.conf();

conf.members[{멤버번호}].hidden = true;
conf.members[{멤버번호}].priority = NumberInt(0);
conf.members[{멤버번호}].slaveDelay = NumberLong({지연시간(초)});

rs.reconfig(conf);



Posted by jungtae17
,

1. Collection Export

mongoexport \
  --host {ReplicaSet이름}/{호스트:포트,...} \
  --readPreference primary \
  --username {사용자계정} \
  --password {비밀번호} \
  --authenticationDatabase admin \
  --db {DB이름} \
  --collection {Collection이름} \
  --out {출력파일}



2. Collection Import

mongoimport \
  --host {ReplicaSet이름}/{호스트:포트,...} \
  --writeConcern 1 \
  --username {사용자계정} \
  --password {비밀번호} \
  --authenticationDatabase admin \
  --db {DB이름} \
  --collection {Collection이름} \
  --drop \
  --file {입력파일}



Posted by jungtae17
,

1. DB 또는 Collection 백업하기

mongodump \

  --host={호스트:포트} \

  --readPreference=secondary \

  --username={관리자계정} \

  --password={비밀번호} \

  --authenticationDatabase=admin \

  --db={DB이름} \

  --collection={Collection이름} \

  --query='{DB 쿼리 조건}' \

  --gzip \

  --out={결과가 저장될 경로명}

  --archive={결과가 저장될 파일명}


옵션> --out--archive 옵션은 중복 사용할 수 없음


참고> https://docs.mongodb.com/manual/reference/program/mongodump/


2. 복원하기

mongorestore ^

  --host={호스트:포트} ^

  --db={DB이름} ^

  --collection={Collection이름} ^

  --drop ^

  --gzip ^

  {복원될 데이터가 있는 경로명}

  --archive={복원될 데이터가 있는 파일명}


옵션> 복원할 경로명--archive 옵션은 중복 사용할 수 없음


참고> https://docs.mongodb.com/manual/reference/program/mongorestore/


Posted by jungtae17
,

MongoDB 접속을 위한 URI 문자열을 이용하여 Replica Set 옵션(Write Concern, Read Concern, Read Preference)을 설정할 수 있다.


1. Replica Set 옵션

2. Write Concern 옵션


3. Read Concern 옵션


4. Read Preference 옵션



Posted by jungtae17
,

aggregation 의 $project 스테이지에서 필드값이 아닌 상수값을 지정할 수 있다.


아래와 같이 $literal 오퍼레이터를 이용한다.


db.statistics.aggregate([
  { $match: { _id: { $lte: 1000 } } },
  { $project: { _id: 0, "Tags": { $literal: "User" } } }
]);


Tags 필드 값은 "User"(문자열)으로 반환된다.



Posted by jungtae17
,

1. munin-node 패키지 설치하기


sudo apt-get -y install munin-node


sudo /etc/init.d/munin-node stop


2. iostat 관련 플러그인 설정하기


sudo ln -s /usr/share/munin/plugins/iostat /etc/munin/plugins/iostat

sudo ln -s /usr/share/munin/plugins/iostat_ios /etc/munin/plugins/iostat_ios


sudo vi /etc/munin/plugin-conf.d/munin-node


[iostat]
env.SHOW_NUMBERED 1

<or>

sudo echo -e "\n[iostat]\nenv.SHOW_NUMBERED 1\n" \
    >> /etc/munin/plugin-conf.d/munin-node


sudo touch /var/lib/munin-node/plugin-state/iostat-ios.state

sudo chown -R root:root /var/lib/munin-node/plugin-state/

sudo chmod -R 766 /var/lib/munin-node/plugin-state/


3. 접속 허용 IP 설정하기


sudo vi /etc/munin/munin-node.conf


allow ^192\.168\.0\.[0-9]+$

<or>

sudo echo -e "\n# Allow Local Network\nallow ^192\.168\.0\.[0-9]+$\n" \
    >> /etc/munin/munin-node.conf


4. munin-node 실행하기


sudo /etc/init.d/munin-node start


5. 접속 테스트


telnet {호스트주소} 4949



Posted by jungtae17
,

1. Node.js 설치


1-1. 빌드된 패키지로 설치(apt-get 패키지)

#!/bin/sh

# install node.js v6.x -------------------------------------
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs
echo

read -p "Press [Enter] key to next step..." key
echo

# check node.js version ------------------------------------
node -v
npm -v
npm list -g -depth 0
echo

read -p "Press [Enter] key to next step..." key
echo

# update npms ----------------------------------------------
sudo npm update -g
echo
 

1-2. edge 패키지를 사용하기 위하여 소스로 빌드하여 설치

#!/bin/sh

# install node.js ------------------------------------------
sudo apt-get -y install python

wget https://raw.githubusercontent.com/tjanczuk/edge/master/tools/debian_ubuntu_clean_install.sh

mv debian_ubuntu_clean_install.sh debian_ubuntu_clean_install.sh.orig
sed -e "s/4.2.3/6.1.0/g" debian_ubuntu_clean_install.sh.orig > debian_ubuntu_clean_install.sh
rm debian_ubuntu_clean_install.sh.orig

export USERNAME=$USER
sudo bash debian_ubuntu_clean_install.sh
echo

read -p "Press [Enter] key to next step..." key
echo

# update npms ----------------------------------------------
sudo npm update -g
echo
 


2. MongoDB 설치

#!/bin/sh

# install mongodb ------------------------------------------
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
sudo apt-get update
sudo apt-get install -y mongodb-org
echo

read -p "Press [Enter] key to next step..." key
echo

# setting mongodb ------------------------------------------
wget https://github.com/mongodb/mongo/raw/master/debian/mongod.service

sudo mv mongod.service /lib/systemd/system
sudo chmod g-w /lib/systemd/system/mongod.service
sudo chown root:root /lib/systemd/system/mongod.service

# service enable -------------------------------------------
sudo systemctl enable mongod
echo

read -p "Press [Enter] key to next step..." key
echo

sudo vi /etc/mongod.conf
echo
 

2-1. MongoDB 설정 파일

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
  directoryPerDB: true

# network interfaces
net:
  port: 27017
  http:
    enabled: true
    RESTInterfaceEnabled: true

# security
security:
  authorization: enabled


3. 다운로드

shell_scripts.zip


Posted by jungtae17
,

새로운 DB로 백업 DB를 전체 복구하는 방법


1. 새로운 DB 설치를 위하여 MongoDB 서버를 실행한다. (DB 생성, 인증 생략)

mongod.exe ^
  --port 27017 ^
  --dbpath "{새로운 DB 경로}" ^
  --directoryperdb ^
  --noauth ^
  --rest

2. 백업된 DB를 복구한다. (전체 복구)

mongorestore.exe ^
  /port 27017 ^
  /drop ^
  "{백업 DB 경로}"

3. 재시작을 위하여 MongoDB 서버를 종료한다.


4. 운영을 위한 MongoDB 서버를 실행한다. (인증 포함)

mongod.exe ^

  --port 27017 ^

  --dbpath "{새로운 DB 경로}" ^

  --directoryperdb ^

  --auth ^

  --rest



Posted by jungtae17
,

MongoDB 드라이버에서 보안 연결하는 방법 (MongoDB 3.0 이상)


Connection String (URI)

"mongodb://{사용자ID}:{암호}@{MongoDB주소}/{DB이름}?authMechanism=SCRAM-SHA-1"


참고> https://docs.mongodb.org/manual/reference/connection-string/#authentication-options



URI 사용 시 사용자ID 또는 암호 문자열에 특수문자(영문자와 숫자를 제외한 문자)가 포함된 경우 URL Encoding 방식으로 사용할 수 있음.

사용예>

abc@123 ===> abc%40123

xyz&123$abc ===> xyz%26123%24abc


참고> http://www.w3schools.com/tags/ref_urlencode.asp



Posted by jungtae17
,