Rocky Linux 8.5にMongoDB5.0のユーザーを作成する

環境
# cat /etc/redhat-release
Rocky Linux release 8.5 (Green Obsidian)
# mongod –version
db version v5.0.6

使用例
1.ユーザー認証を設定します
# sudo nano /etc/mongod.conf
修正前
#security:
修正後
security:
authorization: enabled
mongodを再起動して設定を反映させます
# sudo systemctl restart mongod

ステータスを確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# sudo systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor pres>
Active: active (running) since Mon 2022-03-07 15:45:41 EST; 26s ago
# sudo systemctl status mongod ● mongod.service - MongoDB Database Server Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor pres> Active: active (running) since Mon 2022-03-07 15:45:41 EST; 26s ago
# sudo systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor pres>
   Active: active (running) since Mon 2022-03-07 15:45:41 EST; 26s ago

2.mongodサーバーに接続します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# mongo
MongoDB shell version v5.0.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("deb5aa5f-71f8-453a-b99f-e9b6eaaf7d3c") }
MongoDB server version: 5.0.6
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
>
# mongo MongoDB shell version v5.0.6 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("deb5aa5f-71f8-453a-b99f-e9b6eaaf7d3c") } MongoDB server version: 5.0.6 ================ Warning: the "mongo" shell has been superseded by "mongosh", which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in an upcoming release. For installation instructions, see https://docs.mongodb.com/mongodb-shell/install/ ================ Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see https://docs.mongodb.com/ Questions? Try the MongoDB Developer Community Forums https://community.mongodb.com >
# mongo
MongoDB shell version v5.0.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("deb5aa5f-71f8-453a-b99f-e9b6eaaf7d3c") }
MongoDB server version: 5.0.6
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
        https://community.mongodb.com
>

3.ユーザーを作成します
> use admin
switched to db admin

ユーザー「adminuser」を作成します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
> db.createUser(
... {user: "adminuser",pwd: passwordPrompt(), roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
... }
... )
Enter password: #パスワード入力
Successfully added user: {
"user" : "adminuser",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
},
"readWriteAnyDatabase"
]
}
> db.createUser( ... {user: "adminuser",pwd: passwordPrompt(), roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ] ... } ... ) Enter password: #パスワード入力 Successfully added user: { "user" : "adminuser", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" }, "readWriteAnyDatabase" ] }
> db.createUser(
... {user: "adminuser",pwd: passwordPrompt(), roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
... }
... )
Enter password:     #パスワード入力
Successfully added user: {
        "user" : "adminuser",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                },
                "readWriteAnyDatabase"
        ]
}

exit
bye

4.作成したユーザーでMongoDBログインします

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# mongo -u adminuser -p --authenticationDatabase admin
MongoDB shell version v5.0.6
Enter password: #設定したパスワード入力
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ccad647c-0c0f-44bb-bf54-36c562c06d90") }
MongoDB server version: 5.0.6
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
>
# mongo -u adminuser -p --authenticationDatabase admin MongoDB shell version v5.0.6 Enter password: #設定したパスワード入力 connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("ccad647c-0c0f-44bb-bf54-36c562c06d90") } MongoDB server version: 5.0.6 ================ Warning: the "mongo" shell has been superseded by "mongosh", which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in an upcoming release. For installation instructions, see https://docs.mongodb.com/mongodb-shell/install/ ================ >
# mongo -u adminuser -p --authenticationDatabase admin
MongoDB shell version v5.0.6
Enter password:  #設定したパスワード入力
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ccad647c-0c0f-44bb-bf54-36c562c06d90") }
MongoDB server version: 5.0.6
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
>

 

Rocky Linux 8

Posted by arkgame