1. 程式人生 > >Linux下簡單安裝MongoDB

Linux下簡單安裝MongoDB

在linux環境下安裝mongoDB,安裝步驟備忘一下。

  • 下載完成之後,將包mongodb-linux-i686-3.2.4.tgz上傳到linux系統的/usr/local目錄下;並解壓
 [root@centOS-1 local]# tar zxvf mongodb-linux-i686-3.2.4.tgz 
  • 將解壓後的mongoDB的目錄重新命名為mongoDB
[root@centOS-1 local]# mv mongodb-linux-i686-3.2.4 mongoDB
  • 然後為mongodb建立資料庫存放的位置和日誌檔案,預設是在/data/db下面,為了方便管理,就在mongodb下面建立了相應的目錄;我們在mongodb目錄下新建一個data目錄存放資料、新建一個log目錄存放日誌,然後在該目錄下新建一個日誌檔案,例如我們命名為mongodb.log
[root@centOS-1 local]# cd mongoDB/
[root@centOS-1 mongoDB]# mkdir data
[root@centOS-1 mongoDB]# mkdir log
[root@centOS-1 mongoDB]# touch mongodb.log
[root@centOS-1 mongoDB]#
  • 進到mongodb下面的bin目錄下檢視mongodb的幫助文件和我們用到的啟動引數
[root@centOS-1 bin]# ./mongod -h
  • 使用mongod命令建立一個mongodb資料庫連結,資料庫的路徑為/mongodb/data,日誌路徑為/mongodb/log/mongodb.log
    ;由於我們是32位的linux系統,32bit版本的mongodb預設不開journal記錄,需要在執行時得加上–journal引數,同時mongodb自3.0版本後新增了wiredTiger的資料儲存引擎,而我們的系統採用的是centos版本,預設的wiredTiger不能用,
    那麼採用指定引擎為mmapv1;為了在後臺開啟服務程序,需加上–fork引數。

啟動命令如下:

[[email protected]1 bin]# ./mongod  --dbpath ../data/ --logpath ../log/mongodb.log --journal --storageEngine=mmapv1 --fork
about to fork child process, waiting until server is ready for connections.
forked process: 1823
child process started successfully, parent exiting
[[email protected]
1 bin]#

由此可見mongoDB已經啟動成功。

  • 驗證mongoDB是否啟動成功,mongoDB資料庫預設監聽的埠是27017,因此使用netstat命令檢視此埠是否開啟
[root@centOS-1 mongoDB]# netstat -apn|grep 27017
tcp        0      0 0.0.0.0:27017               0.0.0.0:*                   LISTEN      1823/./mongod       
unix  2      [ ACC ]     STREAM     LISTENING     14442  1823/./mongod       /tmp/mongodb-27017.sock
[root@centOS-1 mongoDB]# 

可以看到mongoDB已經正確開啟

  • 關閉mongDB的方式
    也可以進入到mongo資料庫裡面進行操作
    ./mongo
    use admin
    db.shutdownServer();
[[email protected]1 bin]# ./mongo
MongoDB shell version: 3.2.4
connecting to: test
Server has startup warnings: 
2016-03-25T04:10:55.928+0800 I CONTROL  [initandlisten] 
2016-03-25T04:10:55.928+0800 I CONTROL  [initandlisten] ** WARNING: This 32-bit MongoDB binary is deprecated
2016-03-25T04:10:55.928+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2016-03-25T04:10:55.928+0800 I CONTROL  [initandlisten] 
2016-03-25T04:10:55.928+0800 I CONTROL  [initandlisten] 
2016-03-25T04:10:55.929+0800 I CONTROL  [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
2016-03-25T04:10:55.929+0800 I CONTROL  [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
2016-03-25T04:10:55.929+0800 I CONTROL  [initandlisten] **       See http://dochub.mongodb.org/core/32bit
2016-03-25T04:10:55.929+0800 I CONTROL  [initandlisten] 
> 
> use admin
switched to db admin
> db.shutdownServer();
server should be down...
2016-03-25T04:16:19.549+0800 I NETWORK  [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2016-03-25T04:16:19.552+0800 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2016-03-25T04:16:19.553+0800 I NETWORK  [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed 
> 
  • 設定mongDB開機自啟動
    編輯/etc/rc.local檔案,將mongDB的啟動命令寫入到此檔案中即可
    /usr/local/mongoDB/bin/mongod –dbpath /usr/local/mongoDB/data/ –logpath /usr/local/mongoDB/log/mongodb.log –journal –storageEngine=mmapv1 –fork

    [[email protected] ~]# vim /etc/rc.local

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
#start mongoDB
/usr/local/mongoDB/bin/mongod  --dbpath /usr/local/mongoDB/data/ --logpath /usr/local/mongoDB/log/mongodb.log  --journal --storageEngine=mmapv1 --fork