Help us understand the problem. What is going on with this article?

CentOS 7(さくらVPS)にMongoDB構築

More than 3 years have passed since last update.

CentOS 7にMongoDB 3.4を構築

MongoDBリポジトリ追加

vim /etc/yum.repos.d/mongodb-org-3.4.repo
monbodb-org-3.4.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc

MongoDBをインストール

yum install mongodb-org

MongoDBサーバーを起動

systemctl start mongod.service

ユーザー認証有効化

ユーザーはデータベースごとに作成、adminデータベースで作成すると管理者

管理ユーザーを作成

mongo


use admin

db.createUser({
    user: '[ユーザー名]',
    pwd: '[パスワード]',
    roles: [{
        role: 'userAdminAnyDatabase',
        db: 'admin'
    }]
})
  • Control+C等でmongoシェルを終了

MongoDBサーバーを停止

systemctl stop mongod.service

認証モードを有効化

vim /etc/mongod.conf
mongod.conf
#security:
↓
security:
  authorization: enabled
  • MongoDBサーバーを起動
systemctl start mongod.service

Mongoシェルにadminユーザーでログイン

mongo


db.auth("[ユーザ名]","[パスワード]")
# 1と返ってきたら成功

通常ユーザーを作成

use [データベース名]


db.createUser({
    user: '[ユーザー名]',
    pwd: '[パスワード]',
    roles: [{
        role: 'readWrite',  # readWriteで読み書き可、readで読み取り専用
        db: '[データベース名]'
    }]
})

自動起動を有効化

systemctl enable mongod.service

参考

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした