--dbpath <directory path>
/data/db
로 지정되지만, 원하는 위치로 지정하고 싶은 경우 directory path를 지정하면 된다.--port <port number>
--auth
--bind_ip <ip>
--bind_ip <ip>,<ip>
0.0.0.0
을 지정하면 된다.Command Line Options으로 지정할 수 있지만, 매 실행마다 입력해야 되는 불편함이 있다. 그래서 Configuration File로 관리하는데, Command Line과 Configuration File Option의 맵핑은 아래와 같다.
Command Line Options (https://docs.mongodb.com/manual/reference/program/mongod/#options)
--dbpath
--logpath
--bind_ip
--replSet
--keyFile
--sslPEMKey
--sslCAKey
--sslMode
--fork
Configuration File Options (https://docs.mongodb.com/manual/reference/configuration-options/)
Configuration File은 YAML 파일로 작성되며 아래와 같다.
storage:
dbPath: "/data/db"
systemLog:
path: "/data/log/mongod.log"
destination: "file"
replication:
# 주석을 달 수 있음
replSetName: "M103"
# Command Line Options
mongod --dbpath /data/db --logpath /data/log/mongod.log --fork --replSet "M103" --keyFile /data/keyfile --bind_ip "127.0.0.1,192.168.103.100" --tlsMode requireTLS --tlsCAFile "/etc/tls/TLSCA.pem" --tlsCertificateKeyFile "/etc/tls/tls.pem"
# Configuration File Options
storage:
dbPath: "/data/db"
systemLog:
path: "/data/log/mongod.log"
destination: "file"
replication:
replSetName: M103
net:
bindIp : "127.0.0.1,192.168.103.100"
tls:
mode: "requireTLS"
certificateKeyFile: "/etc/tls/tls.pem"
CAFile: "/etc/tls/TLSCA.pem"
security:
keyFile: "/data/keyfile"
processManagement:
fork: true
/etc/mongod.conf
는 Linux에 설치시 기본으로 설정 파일이 생성되는 곳)mongod --config "/etc/mongod.conf"
mongod -f "/etc/mongod.conf"
// Creating index with Database Command
db.runCommand({
"createIndexes":"<collection_name>",
"indexes":[
{
"key":{ "product": 1 },
"name": "name_index"
}
]
}
)
// Creating index with Shell Helper
db.<collection>.createIndex(
{ "product": 1 },
{ "name": "name_index" }
)
db.getLogComponents()
를 실행하면 Log 설정을 확인할 수 있음"verbosity" : 0,
"accessControl" : {
"verbosity" : -1
},
"command" : {
"verbosity" : -1
},
"control" : {
"verbosity" : -1
},
"executor" : {
"verbosity" : -1
},
"geo" : {
"verbosity" : -1
},
"index" : {
"verbosity" : -1
},
"network" : {
"verbosity" : -1,
"asio" : {
"verbosity" : -1
},
"bridge" : {
"verbosity" : -1
},
"connectionPool" : {
"verbosity" : -1
}
}
Log Message Serverity Levels
Log를 확인할 수 있는 방법
db.adminCommand({ "getLog": "global" })
실행tail -f <log 저장 위치>
실행system.profile
이라는 collection이 생성된다.db.getProfilingLevel()
db.setProfilingLevel(1)
security:
authorization: enabled
db.createUser(
{
user: "root",
pwd: "root",
roles: ["root"]
}
)
// 권한 없이 접속했기 때문에 만들수 없음
db.createUser(
{
user: "modolee",
pwd: "modolee123",
roles: ["readWrite"]
}
)
// uncaught exception: Error: couldn't add user: command createUser requires authentication
mongo --host 127.0.0.1:27017 \
--username root \
--password root \
--authenticationDatabase admin
// 특정 database와 collecion을 지정
{ db: "products", collection: "inventory" }
// 모든 database와 collection을 지정
{ db: "", collection: "" }
// 모든 database와 특정 collection을 지정
{ db: "", collection: "accounts" }
// 특정 database와 모든 collection을 지정
{ db: "products", collection: "" }
// cluster를 지정
{ cluster: true }
// cluster에서 shutdown을 허용
{ resource: { cluster: true }, actions: ["shutdown"] }
db.createUser(
{
user: 'security_officer',
pwd: 'h3ll0th3r3',
roles: [{ db: 'admin', role: 'userAdmin' }]
}
)
db.createUser(
{
user: 'dba',
pwd: 'c1lynd3rs',
roles: [{ db: 'm103', role: 'dbAdmin' }]
}
)
db.grantRolesToUser('dba', [{ db: 'playround', role: 'dbOwner' }])
db.runCommand({rolesInfo: {role:'dbOwner', db:'playground'}, showPrivileges: true})
실행 명령
mongostat --port 27000 --username root --authenticationDatabase=admin
실행 결과
insert query update delete getmore command dirty used flushes vsize res qrw arw net_in net_out conn time
*0 *0 *0 *0 0 0|0 0.0% 0.0% 0 6.58G 37.0M 0|0 1|0 111b 50.5k 3 Mar 22 20:37:33.603
*0 *0 *0 *0 0 1|0 0.0% 0.0% 1 6.58G 37.0M 0|0 1|0 112b 50.7k 3 Mar 22 20:37:34.601
*0 *0 *0 *0 0 0|0 0.0% 0.0% 0 6.58G 37.0M 0|0 1|0 111b 50.5k 3 Mar 22 20:37:35.602
*0 *0 *0 *0 0 1|0 0.0% 0.0% 0 6.58G 37.0M 0|0 1|0 112b 50.6k 3 Mar 22 20:37:36.602
기본 포트를 사용할 경우 접속이 안되지 반드시 다른 포트로 지정해서 사용할 것
could not connect to server: connection() error occured during connection handshake: auth error: unable to authenticate using mechanism "SCRAM-SHA-256": (AuthenticationFailed) Authentication failed.