1. 程式人生 > >Linux平臺安裝MongoDB(轉)

Linux平臺安裝MongoDB(轉)

 

一、下載完安裝包,並解壓 tgz(以下演示的是 64 位 Linux上的安裝) 。

  curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz    # 下載
  tar -zxvf mongodb-linux-x86_64-3.0.6.tgz                                   # 解壓

  mv  mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb                         # 將解壓包拷貝到指定目錄

 

二、MongoDB 的可執行檔案位於 bin 目錄下,所以可以將其新增到 PATH 路徑中:

  export PATH=<<span class="pln" style="margin: 0px; padding: 0px;">mongodb-install-directory>/bin:$PATH

    為 MongoDB 的安裝路徑。如本文的 /usr/local/mongodb 。

 

三、建立資料庫目錄

    MongoDB的資料儲存在data目錄的db目錄下,但是這個目錄在安裝過程不會自動建立,所以你需要手動建立data目錄,並在data目錄中建立db目錄。以下例項中我們將data目錄創建於根目錄下(/)。

    注意:/data/db 是 MongoDB 預設的啟動的資料庫路徑(--dbpath)。

    mkdir -p /data/db

四、命令列中執行 MongoDB 服務

   ①可以在 /usr/local/mongodb/bin/ 目錄下使用命令:  

    ./mongod

   ②在 /usr/local/mongodb/ 下新增conf目錄,並新增mongodb.conf配置檔案。

    ./mongod -f /usr/local/mongodb/conf/mongodb.conf   或者
    ./mongod --config /usr/local/mongodb/conf/mongodb.conf

    mongodb.conf
配置檔案內容如下:
      dbpath=/data/db
      bind_ip=127.0.0.1
      port=27017
      fork=true
      logappend=true
      shardsvr=true
      pidfilepath=/usr/local/mongodb/mongo.pid
      logpath=/usr/local/mongodb/log/mongodb.log

五、設定開機啟動mongo

   方法一、①在/etc/init.d/目錄下新增mongod指令碼,

       ②新增指令碼執行許可權  chmod +x /etc/init.d/mongod 

       ③設定開機啟動  chkconfig mongod on 

      mongo指令碼內容如下:

      #!/bin/bash

       #chkconfig: 2345 80 90

       #description: mongodb
       start() {
       /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf
       }

       stop() {
       /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf --shutdown
       }

       case "$1" in
       start)
       start
       ;;
       stop)
       stop
       ;;
       restart)
       stop
       start
       ;;
       *)
       echo $"Usage: $0 {start|stop|restart}"
       exit 1
       esac

   方法二、將 /usr/local/mongodb/bin/mongod

         --dbpath /data/db

         --fork

         --port 27017

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

         --logappend  

        新增到 /etc/rc.local 

     

一、下載完安裝包,並解壓 tgz(以下演示的是 64 位 Linux上的安裝) 。

  curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz    # 下載
  tar -zxvf mongodb-linux-x86_64-3.0.6.tgz                                   # 解壓

  mv  mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb                         # 將解壓包拷貝到指定目錄

 

二、MongoDB 的可執行檔案位於 bin 目錄下,所以可以將其新增到 PATH 路徑中:

  export PATH=<<span class="pln" style="margin: 0px; padding: 0px;">mongodb-install-directory>/bin:$PATH

    為 MongoDB 的安裝路徑。如本文的 /usr/local/mongodb 。

 

三、建立資料庫目錄

    MongoDB的資料儲存在data目錄的db目錄下,但是這個目錄在安裝過程不會自動建立,所以你需要手動建立data目錄,並在data目錄中建立db目錄。以下例項中我們將data目錄創建於根目錄下(/)。

    注意:/data/db 是 MongoDB 預設的啟動的資料庫路徑(--dbpath)。

    mkdir -p /data/db

四、命令列中執行 MongoDB 服務

   ①可以在 /usr/local/mongodb/bin/ 目錄下使用命令:  

    ./mongod

   ②在 /usr/local/mongodb/ 下新增conf目錄,並新增mongodb.conf配置檔案。

    ./mongod -f /usr/local/mongodb/conf/mongodb.conf   或者
    ./mongod --config /usr/local/mongodb/conf/mongodb.conf

    mongodb.conf
配置檔案內容如下:
      dbpath=/data/db
      bind_ip=127.0.0.1
      port=27017
      fork=true
      logappend=true
      shardsvr=true
      pidfilepath=/usr/local/mongodb/mongo.pid
      logpath=/usr/local/mongodb/log/mongodb.log

五、設定開機啟動mongo

   方法一、①在/etc/init.d/目錄下新增mongod指令碼,

       ②新增指令碼執行許可權  chmod +x /etc/init.d/mongod 

       ③設定開機啟動  chkconfig mongod on 

      mongo指令碼內容如下:

      #!/bin/bash

       #chkconfig: 2345 80 90

       #description: mongodb
       start() {
       /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf
       }

       stop() {
       /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf --shutdown
       }

       case "$1" in
       start)
       start
       ;;
       stop)
       stop
       ;;
       restart)
       stop
       start
       ;;
       *)
       echo $"Usage: $0 {start|stop|restart}"
       exit 1
       esac

   方法二、將 /usr/local/mongodb/bin/mongod

         --dbpath /data/db

         --fork

         --port 27017

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

         --logappend  

        新增到 /etc/rc.local