在CenterOS上安装MongoDB

Install MongoDB

  • Step1: 添加yum repo (如需安装其他版本,只需将3.2改成其他版本即可)

    vi /etc/yum.repos.d/mongodb-org-3.2.repo 填入以下内容:

    [mongodb-org-3.2]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
    gpgcheck=0
    enabled=1

  • Step2: 安装
    sudo yum install -y mongodb-org

    该package包含了mongodb-org-server, mongodb-org-mongos, mongodb-org-shell, mongodb-org-tools

Configure MongoDB

  • Step1: 更新conf
    vi /etc/mongod.conf,修改systemLog.pathstorage.dbPath
  • Step2: 更改owner
    sudo chown -R mongod:mongod 修改后的db/log路径
  • Step2: hugepage配置更改
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
  • Step4: 添加user
sudo service mongod start
mongo
use admin

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


vi /etc/mongod.conf # 在security下添加authorization: enabled
sudo service mongod restart
mongo
use admin
db.auth("admin", "adminPwd")
use `your db`

db.createUser(
{
user: "username",
pwd: "password",
roles: [
{ role: "readWrite", db: "your db" }
]
}
)

db.auth("username", "password")
exit
  • Step5: 配置开机启动

    sudo chkconfig mongod on

使用如下步骤安装后,启动脚本在/etc/rc.d/init.d/mongod, 配置文件在/etc/mongod.conf

坚持原创技术分享,您的支持将鼓励我继续创作!