Docs Menu

createRole

createRole

Creates a role and specifies its privileges. The role applies to the database on which you run the command. The createRole command returns a duplicate role error if the role already exists in the database.

mongosh에서 이 명령을 db.createRole() 헬퍼 메서드를 통해서도 실행할 수 있습니다.

헬퍼 메서드는 mongosh 사용자에게 편리하지만 데이터베이스 명령과 동일한 수준의 정보를 반환하지 못할 수 있습니다. 편의가 필요하지 않거나 추가 리턴 필드가 필요한 경우 데이터베이스 명령을 사용합니다.

이 명령은 다음 환경에서 호스팅되는 배포에서 사용할 수 있습니다.

  • MongoDB Atlas: 클라우드에서의 MongoDB 배포를 위한 완전 관리형 서비스

중요

이 명령은 M0, M2, M5, M10+ 및 Flex 클러스터에서 지원되지 않습니다. 자세한 내용은 지원되지 않는 명령을 참조하세요.

명령은 다음과 같은 구문을 가집니다:

db.adminCommand(
{
createRole: "<new role>",
privileges: [
{ resource: { <resource> }, actions: [ "<action>", ... ] },
...
],
roles: [
{ role: "<role>", db: "<database>" } | "<role>",
...
],
authenticationRestrictions: [
{
clientSource: ["<IP>" | "<CIDR range>", ...],
serverAddress: ["<IP>" | "<CIDR range>", ...]
},
...
],
writeConcern: <write concern document>,
comment: <any>
}
)

createRole 명령에는 다음과 같은 필드가 있습니다.

필드
유형
설명

createRole

문자열

The name of the new role.

privileges

배열

The privileges to grant the role. A privilege consists of a resource and permitted actions. For the syntax of a privilege, see the privileges array.

You must include the privileges field. Use an empty array to specify no privileges.

roles

배열

An array of roles from which this role inherits privileges.

You must include the roles field. Use an empty array to specify no roles to inherit from.

authenticationRestrictions

배열

선택 사항.

The authentication restrictions the server enforces on the role. Specifies a list of IP addresses and CIDR ranges users granted this role are allowed to connect to and/or which they can connect from.

writeConcern

문서

선택 사항입니다. 작업에 대한 쓰기 고려 수준입니다. 쓰기 고려 사양을 참조하세요.

comment

any

선택 사항. 이 명령에 첨부할 사용자 제공 코멘트입니다. 설정되면 이 설명은 다음 위치에서 이 명령의 레코드와 함께 표시됩니다.

댓글은 유효한 모든 BSON types (문자열, 정수, 객체, 배열 등)이 될 수 있습니다.

roles 필드에서 기본 제공 역할사용자 정의 역할을 모두 지정할 수 있습니다.

createRole가 실행되는 동일한 데이터베이스에 존재하는 역할을 지정하려면 역할의 이름과 함께 역할을 지정할 수 있습니다.

"readWrite"

또는 다음과 같이 문서로 역할을 지정할 수도 있습니다.

{ role: "<role>", db: "<database>" }

다른 데이터베이스에 존재하는 역할을 지정하려면 문서를 사용하여 역할을 지정합니다.

authenticationRestrictions 문서에는 다음 필드 포함될 수 있습니다. authenticationRestrictions 문서에 인식할 수 없는 필드가 포함되어 있으면 서버에서 오류를 발생시킵니다.

필드 이름
설명

clientSource

IP 주소 및/또는 CIDR 범위 배열

존재하는 경우, 사용자를 인증할 때 서버는 클라이언트의 IP 주소가 지정된 목록에 있거나 목록에 있는 CIDR 범위에 속하는지 확인합니다. 클라이언트의 IP 주소가 없는 경우 서버는 사용자를 인증하지 않습니다.

serverAddress

IP 주소 및/또는 CIDR 범위 배열

클라이언트가 연결할 수 있는 IP 주소 또는 CIDR 범위의 목록입니다. 존재하는 경우, 서버는 주어진 목록의 IP 주소를 통해 클라이언트의 연결이 승인되었는지 확인합니다. 인식할 수 없는 IP 주소를 통해 연결이 수락된 경우 서버는 사용자를 인증하지 않습니다.

중요

사용자가 인증 제한이 호환되지 않는 여러 역할을 상속하는 경우 해당 사용자는 사용할 수 없게 됩니다.

예를 들어, 사용자가 clientSource 필드가 ["198.51.100.0"]인 역할과 clientSource 필드가 ["203.0.113.0"]인 다른 역할을 상속하는 경우 서버는 사용자를 인증할 수 없습니다.

MongoDB 의 인증 에 대한 자세한 내용 은 자체 관리형 배포서버에 대한 인증을 참조하세요.

A role's privileges apply to the database where the role is created. The role can inherit privileges from other roles in its database. A role created on the admin database can include privileges that apply to all databases or to the cluster and can inherit privileges from roles in other databases.

To create a role in a database, you must have:

Built-in roles userAdmin and userAdminAnyDatabase provide createRole and grantRole actions on their respective resources.

To create a role with authenticationRestrictions specified, you must have the setAuthenticationRestriction 조치 on the database resource which the role is created.

The following createRole command creates the myClusterwideAdmin role on the admin database:

db.adminCommand({ createRole: "myClusterwideAdmin",
privileges: [
{ resource: { cluster: true }, actions: [ "addShard" ] },
{ resource: { db: "config", collection: "" }, actions: [ "find", "update", "insert", "remove" ] },
{ resource: { db: "users", collection: "usersCollection" }, actions: [ "update", "insert", "remove" ] },
{ resource: { db: "", collection: "" }, actions: [ "find" ] }
],
roles: [
{ role: "read", db: "admin" }
],
writeConcern: { w: "majority" , wtimeout: 5000 }
})