centos-mongodb

centos 7 + mongodb 4.0

  • create mongodb-org.repo inside /etc/yum.repos.d/ directory

past following contents

[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
  • Installing MongoDB
    sudo yum install mongodb-org

  • Starting MongoDB

    sudo systemctl start mongod
    sudo systemctl enable mongod
  • Verifying MongoDB Installation

    // 进入命令行
    mongo

    db.version()

    //启动服务
    mongod --dbpath /home/tyler/mongodb/stock

    // 使用配置文件启动
    mongod -f /etc/mongod.conf

Failed to unlink socket file /tmp/mongodb-27017.sock Unknown error

delete the /tmp/mongodb-27017.sock file manually and start the mongod process.

Failed to set up listener: SocketException: Address already in use

sudo service mongod stop
sudo mongod
  • Configuring MongoDB
    编辑配置文件 /etc/mongod.conf
security:
authorization: enabled

The authorization option enables Role-Based Access Control (RBAC) that regulates users access to database resources and operations. If this option is disabled each user will have access to any database and will be able to execute any action.

sudo systemctl restart mongod
  • Creating Administrative MongoDB User
    mongo
    use admin

    db.createUser(
    {
    user: "mongoSuperAdmin",
    pwd: "cow2wsx1qaz",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
    }
    )

    quit()
mongo -u mongoSuperAdmin -p --authenticationDatabase admin
use admin
show users

mongo manual