---
title: "mongodb开发环境下的搭建入门"
author: "braveCS"
date: "2015-06-26T22:32:55Z"
tags: ["运维"]
categories: ["开源软件"]
slug: "mongodb"
draft: false
lastmod: "2015-06-26T22:32:55Z"
id: 2222433
url: "https://bravecs.iteye.com/blog/2222433"
---

**linux下安装mongodb**

1）官网下载mongodb-linux-x86\_64-rhel62-3.0.4.gz

2）linux 解压 

gzip -d mongodb-linux-x86\_64-rhel62-3.0.4.gz;

mv mongodb-linux-x86\_64-rhel62-3.0.4 mongodb-linux-x86\_64-rhel62-3.0.4.tar

tar xvf  mongodb-linux-x86\_64-rhel62-3.0.4.tar

mkdir /opt/mongodb

把解压后的 mongodb-linux-x86\_64-rhel62-3.0.4移动到/opt/mongodb

3）创建mongo数据文件目录、mongodb日志文件、mongod的配置文件、mongod的pid文件

mkdir /opt/mongodb/data

touch /opt/mongodb/mongod.log

touch /opt/mongodb/mongod.cfg

touch /opt/mongodb/mongod.pid

 

4)配置mongod的配置文件，把下面的配置信息写到mongod.cfg里

===================================================================

systemLog:

   destination: file

   path: "/opt/mongodb/mongod.log"

   logAppend: true

storage:

   dbPath: "/opt/mongodb/data"

   journal:

      enabled: true

net:

   bindIp: 192.168.52.150

   port: 27017

processManagement:

   pidFilePath: "/opt/mongodb/mongod.pid"

   fork: true

===================================================================

 

5）设置mongodb文件权限 chmod -R 755 /opt/mongodb/*

 

6）启动： cd 到/opt/mongodb/mongodb-linux-x86\_64-rhel62-3.0.4/bin

mongod -f /opt/mongodb/mongod.cfg

 

7)关闭：不能使用kill -9 进程

mongo

use admin

db.shutdownServer()

 

添加管理员用户

/\*\*mongodb的初始化脚本增加用户,

 * 1）把下列脚本写入mongodb-init.js

 * 2）执行 ./mongo 192.168.52.150:27017/admin --shell  mongo-ini.js

 * 3）以后用shell登录使用： ./mongo 192.168.52.150:27017/admin -u root -p 123

 \*/

db.createUser({"user":"root","pwd":"123","roles":\["dbOwner","root"]});

 

添加完管理员，修改mongod.conf的配置，增加下

============================

security:

   authorization: enabled  

=============================

 

重启

/\*\*mongodb的初始化脚本增加用户,

 * 1）把下列脚本写入mongodb-init.js

 * 2）执行 ./mongo 192.168.52.150:27017/exchange  --shell  mongo-ini.js -u root -p 123

 * 3）以后用shell登录使用： ./mongo 192.168.52.150:27017/exchange -u abc -p abc

 \*/

db.createUser({"user":"abc","pwd":" abc ","roles":\[{role:"dbOwner",db:"exchange"}]});