安裝配置MongoDB資料庫

來源:互聯網
上載者:User

標籤:echo   shadow   成功   comm   isa   x86_64   server   linux   安裝   

                                                                    安裝配置MongoDB資料庫


實驗環境:

系統內容:rhel6 x64 selinux and iptables disabled


一、安裝MongoDB


https://www.mongodb.com/download-center#community


[[email protected] ~]# ls
mongodb-linux-x86_64-rhel62-4.0.0


[[email protected] ~]# mv mongodb-linux-x86_64-rhel62-4.0.0/ /usr/local/mongodb    #移動解壓檔案夾到MongoDB安裝目錄


[[email protected] ~]# mkdir -p /home/data/mongodb/mongodb_data     #建立MongoDB資料庫存放路徑


[[email protected] ~]# mkdir -p /home/data/mongodb/mongodb_log         #建立MongoDB資料庫日誌存放路徑


#啟動MongoDB

[[email protected] ~]# /usr/local/mongodb/bin/mongod --port 27017 --fork --dbpath=/home/data/mongodb/mongodb_data/ --logpath=/home/data/mongodb/mongodb_log/mongodb.log --logappend


查看連接埠

[[email protected] ~]# netstat -antlp     #查看MongoDB是否啟動


[[email protected] ~]# cd /usr/local/mongodb/bin/


[[email protected] bin]# ./mongo     #進入MongoDB資料庫控制台



> 2+3
5
> 2*3
6
>

> use admin            #進入admin資料庫

switched to db admin
> db.shutdownServer()    #關閉MongoDB資料庫

server should be down...
2018-07-22T21:25:17.131+0800 I NETWORK  [js] trying reconnect to 127.0.0.1:27017 failed
2018-07-22T21:25:17.131+0800 I NETWORK  [js] reconnect 127.0.0.1:27017 failed failed
> exit     #退出

bye

[[email protected] bin]# netstat -antlp | grep :27017     已經關閉
[[email protected] bin]# 


再次進入MongoDB資料庫控制台時如下報錯:

[[email protected] bin]# ./mongo
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27017
2018-07-22T22:07:54.310+0800 E QUERY    [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
[email protected]/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed


解決途徑:

(1)  cd /usr/local/mongodb/bin
(2) ./mongod --dbpath=/home/data/mongodb/mongodb_data/

(3) 上一步路徑設定成功,但是不要關閉這個介面,需要重新開啟一個新的視窗來啟動mongo

(4)在另一個終端:cd /usr/local/mongodb/
                             ./bin/mongo

做下面時,先關閉MongoDB資料庫


二、設定MongoDB資料庫


[[email protected] bin]# cd /usr/local/mongodb/      #進入MongoDB安裝目錄

[[email protected] mongodb]# vim mongodb.conf    #編寫設定檔

port=27017 #連接埠號碼

dbpath=/home/data/mongodb/mongodb_data/     #資料庫路徑

logpath=/home/data/mongodb/mongodb_log/mongodb.log    #日誌輸出檔案路徑

logappend=true #日誌輸出方式


[[email protected] ~]# cd /usr/local/mongodb/

[[email protected] bin]# pwd
/usr/local/mongodb/bin
[[email protected] bin]# ./mongod -f mongodb.conf 

查看連接埠:


三、Linux下註冊mongodb服務


進入 /etc/init.d/ 目錄

[[email protected] bin]# cd /etc/init.d/

建立mongodb檔案

[[email protected] init.d]# vim mongodb

#!/bin/sh  
#  
#  ### BEGIN INIT INFO
# Short-Description: mongodb    
# Description: mongo db server    
### END INIT INFO    


PROGRAM=/usr/local/mongodb/bin/mongod
MONGOPID=`ps -ef | grep 'mongod' | grep -v grep | awk '{print $2}'`

test -x $PROGRAM || exit 0

case "$1" in
  start)
     ulimit -n 3000
     echo "Starting MongoDB server"
     $PROGRAM --fork --quiet -f /usr/local/mongodb/mongodb.conf
     echo "Started MongoDB server"
     ;;
  stop)
     echo "Stopping MongoDB server"
     if [ ! -z "$MONGOPID" ]; then
        kill -15 $MONGOPID
     fi
     echo "Stopped MongoDB server"
     ;;
  status)
     ;;
  *)
     echo "Usage: mongodb {start|stop|status}"
     exit 1
esac

exit 0
        
儲存完畢,在執行以下幾條命令即可:

[[email protected] init.d]# chmod +x mongodb


測試:

[[email protected] init.d]# /etc/init.d/mongodb start



[[email protected] init.d]# netstat -antlp | grep :27017






安裝配置MongoDB資料庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.