2023. 4. 30. 11:49ㆍ데이터베이스/MONGODB
Create Role
use 데이터베이스
db.createRole(
{
role: "이름",
privileges: [
{ resource: { db: "데이터베이스 이름", collection: "" }, actions: [ "find", "update", "insert", "remove" ] }
],
roles: []
}
)
아래 참조의 createRole() 을 보면 각 필드가 어떤 의미인지 상세하게 알 수 있다.
몇 가지 주요한 사항을 말해보면,
- collection에 빈 값을 주면 해당 데이터베이스의 모든 컬렉션에 적용하겠다라는 의미가 되고,
- roles에는 이전에 만든 role을 상속 받거나 할 수 있다.
Create User
db.createUser({
user: "사용자 이름",
pwd: "패스워드",
roles: [
"ploy_api"
],
authenticationRestrictions: [
{
clientSource: ["클라이언트 쪽 ip 주소"],
serverAddress: ["서버 쪽 ip 주소"]
}
]
})
로컬 전용인 경우에는 authenticationRestrictions 옵션을 주지 않아도 된다.
Remote Access
여담으로 외부에서 데이터베이스를 연결하고자 할 경우에는 특정 수준까지 Ip와 Port를 열어주어야 한다.
1. mongod.conf 찾기 (본인의 경우 /etc/mongod.conf 였음)
2. 위 파일을 열어 bindIp를 원하는 수준까지 수정
3. mongod 서버 다시 시작
sudo systemctl restart mongod
4. port 열기
sudo ufw allow 27017/tcp
References
db.createRole() — MongoDB Manual
Docs Home → MongoDB Manual db.createRole(role, writeConcern)Creates a role in a database. You can specify privileges for the role by explicitly listing the privileges or by having the role inherit privileges from other roles or both. The role applies to
www.mongodb.com
db.createUser() — MongoDB Manual
Docs Home → MongoDB Manual db.createUser(user, writeConcern)Creates a new user for the database on which the method is run. db.createUser() returns a duplicate user error if the user already exists on the database.mongosh MethodThis page documents a mong
www.mongodb.com