天天看點

MongoDB初探#安裝+簡單操作

tar包安裝MongoDB

解壓并建立目錄mongodb結構

# useradd mongodb

# tar zxvf mongodb-linux-x86_64-2.4.6.tgz -C /usr/local/

# mv /usr/local/mongodb-linux-x86_64-2.4.6 /usr/local/mongodb

# chmown -R mongodb:mongodb /usr/local/mongodb

# su - mongodb 

# mkdir /usr/local/mongodb/{log,data,run,etc}

編寫配置檔案

vim  /usr/local/mongodb/etc/mongod.conf

------------------------------------------------------------

port = 27017

fork = true # daemonize it !

#journal = true #

rest = true

logappend = true

#auth = true                 #首次啟動不能配置該選項,設定後,連結mongodb資料庫需要驗證使用者名和密碼

#keyFile = /usr/local/mongodb/key/db1

dbpath = /usr/local/mongodb/data/

logpath = /usr/local/mongodb/log/mongod.log

pidfilepath = /usr/local/mongodb/run/mongod.pid

-------------------------------------------------------------

編寫簡單的啟動腳本

# vim /usr/local/mongodb/start.sh

--------------------------------------------------

#!/bin/sh

#/usr/local/mongodb/bin/mongod --replSet rs1 --keyFile=/usr/local/mongodb/key/db1 --config=/usr/local/mongo

db/etc/mongod.conf

/usr/local/mongodb/bin/mongod --config=/usr/local/mongodb/etc/mongod.conf

if [ $? = 0 ];then

        echo "mongodb is running!!!"

else

        echo "Error ! mongodb is not running!!!"

fi

--------------------------------------------------

# vim /usr/local/mongodb/shutdown.sh

--------------------------------------------------

#!/bin/sh

ps -ef | grep /usr/local/mongodb/ | grep -v grep | awk '{print $2}' | xargs -r kill -2

if [ $? = 0 ];then

        echo "mongodb is stopd!!!"

else

        echo "Error ! mongodb is not stopd!!"

fi

--------------------------------------------------

啟動停止mongodb測試

啟動

# /usr/local/mongodb/start.sh

同時檢視日志

# tail -f /usr/local/mongodb/log/mongod.log

停止

# /usr/local/mongodb/shutdown.sh

簡單的操作

mongodb用戶端連接配接

  第一次啟動mongodb沒有啟用驗證,是以直接連結即可:

#連接配接

# /usr/local/mongodb/bin/mongo 192.168.0.223:27017

MongoDB shell version: 2.4.6

connecting to: 192.168.0.223:27017/test

>

#退出

>quit()

添加超級管理者

>use admin

> db.addUser('admin','123456');

{

        "user" : "admin",

        "readOnly" : false,

        "pwd" : "95ec4261124ba5951720b199908d892b",

        "_id" : ObjectId("54f6b755c7b5d883ce4ad4c1")

}

PS:開啟驗證之後,添加資料庫和添加使用者都需要驗證此超級使用者停

test case:

>quit()

# /usr/local/mongodb/shutdown.sh

vim  /usr/local/mongodb/etc/mongod.conf

------------------------------------------------------------

port = 27017

fork = true # daemonize it !

#journal = true #

rest = true

logappend = true

auth = true                 #開啟驗證,連結mongodb資料庫需要驗證使用者名和密碼

#keyFile = /usr/local/mongodb/key/db1

dbpath = /usr/local/mongodb/data/

logpath = /usr/local/mongodb/log/mongod.log

pidfilepath = /usr/local/mongodb/run/mongod.pid

-------------------------------------------------------------

# /usr/local/mongodb/start.sh

# /usr/local/mongodb/bin/mongo -uadmin -p123456 192.168.0.223:27017

建立資料庫:

>use test_data;

為該資料庫添加使用者,并設定密碼

需要先驗證超級管理者密碼:

>use admin

>db.auth('admin','123456')

1

傳回1代表驗證成功,可以為此表設定密碼

>use admin

> db.addUser('test1','123456');

{

        "user" : "test1",

        "readOnly" : false,

        "pwd" : "0b71c0ec16b5d1ad250741e47bd5f88d",

        "_id" : ObjectId("54f6b79ec7b5d883ce4ad4c2")

}

檢視目前資料庫

>db

檢視所有資料庫

>show dbs

已經連結狀态,驗證使用者名和密碼

>db.auth('username','password')

Linux shell環境下 mongdb使用

待續……

mongoDB副本集,如何知道主節點在哪台伺服器上:

      可通過rs.status()指令檢視

繼續閱讀