1. 程式人生 > >linux下安裝配置mongodb

linux下安裝配置mongodb

一:下載

下載地址:

二:安裝

2.1 解壓

[[email protected] download]# ls -ltr
total 71552
-rw-r--r-- 1 root root 32979820 Jun 15 10:01 mysql-5.6.20.tar.gz
drwxr-xr-x 4 root root     4096 Jun 16 13:39 mysql
-rw-r--r-- 1 root root 40280527 Jun 25 11:54 mongodb-linux-x86_64-3.0.4.tgz
[[email protected] download]# tar -xvf mongodb-linux-x86_64-3.0.4.tgz 
mongodb-linux-x86_64-3.0.4/README
mongodb-linux-x86_64-3.0.4/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-3.0.4/GNU-AGPL-3.0
mongodb-linux-x86_64-3.0.4/bin/mongodump
mongodb-linux-x86_64-3.0.4/bin/mongorestore
mongodb-linux-x86_64-3.0.4/bin/mongoexport
mongodb-linux-x86_64-3.0.4/bin/mongoimport
mongodb-linux-x86_64-3.0.4/bin/mongostat
mongodb-linux-x86_64-3.0.4/bin/mongotop
mongodb-linux-x86_64-3.0.4/bin/bsondump
mongodb-linux-x86_64-3.0.4/bin/mongofiles
mongodb-linux-x86_64-3.0.4/bin/mongooplog
mongodb-linux-x86_64-3.0.4/bin/mongoperf
mongodb-linux-x86_64-3.0.4/bin/mongod
mongodb-linux-x86_64-3.0.4/bin/mongos
mongodb-linux-x86_64-3.0.4/bin/mongo
[
[email protected]
download]# ls -ltr total 71556 -rw-r--r-- 1 root root 32979820 Jun 15 10:01 mysql-5.6.20.tar.gz drwxr-xr-x 4 root root 4096 Jun 16 13:39 mysql -rw-r--r-- 1 root root 40280527 Jun 25 11:54 mongodb-linux-x86_64-3.0.4.tgz drwxr-xr-x 3 root root 4096 Jun 25 11:54 mongodb-linux-x86_64-3.0.4


#為了方便管理,把安裝檔案移動到/usr/local

[[email protected] download]# mv mongodb-linux-x86_64-3.0.4 /usr/local/mongodb

2.2 建立相關目錄

為mongodb建立資料庫存放的位置和日誌檔案

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

[[email protected] mongodb]# mkdir data

[[email protected] mongodb]# touch logs

[[email protected]

 mongodb]# ls -ltr

total 72

-rw-r--r-- 1 1046 1046 22660 Jun 15 23:51 THIRD-PARTY-NOTICES

-rw-r--r-- 1 1046 1046  1359 Jun 15 23:51 README

-rw-r--r-- 1 1046 1046 34520 Jun 15 23:51 GNU-AGPL-3.0

drwxr-xr-x 2 root root  4096 Jun 25 11:54 bin

drwxr-xr-x 2 root root  4096 Jun 25 12:00 data

-rw-r--r-- 1 root root     0 Jun 25 12:00 logs

2.3 建立Linux使用者

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

[[email protected] init.d]# useradd -g mongodb mongodb

[[email protected] init.d]# chown -R mongodb:mongodb /usr/local/mongodb

2.4 配置PATH

#mongodb使用者:

PATH末尾新增Mongodb 安裝檔案/bin路徑:

[[email protected] init.d]# su - mongodb 

[[email protected] ~]$ vi .bash_profile 

PATH=$PATH:$HOME/bin:/usr/local/mongodb/bin

[[email protected] ~]$ source .bash_profile

這樣就可以不用進入到/usr/local/mongodb/bin路徑下使用mongo命令了,直接輸入mongo命令即可。

#root使用者省略

三:啟動mongodb

#驗證是否安裝成功

[[email protected] ~]$ mongod --dbpath=/usr/local/mongodb/data --fork --logpath=/usr/local/mongodb/logs
about to fork child process, waiting until server is ready for connections.
forked process: 12135
child process started successfully, parent exiting
 
[[email protected] bin]# mongo
MongoDB shell version: 3.0.4
connecting to: test
Server has startup warnings: 
2015-06-25T12:02:26.238+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-06-25T12:02:26.238+0800 I CONTROL  [initandlisten] 
2015-06-25T12:02:26.247+0800 I CONTROL  [initandlisten] 
2015-06-25T12:02:26.247+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-06-25T12:02:26.247+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-06-25T12:02:26.247+0800 I CONTROL  [initandlisten] 
2015-06-25T12:02:26.247+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-06-25T12:02:26.247+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-06-25T12:02:26.247+0800 I CONTROL  [initandlisten] 
> show dbs;
local  0.078GB

可以看到有Local資料庫,說明已安裝成功。

四:建配置檔案

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

[[email protected] mongodb]$ vi mongod.cnf

新增:

logpath=/usr/local/mongodb/logs
logappend = true 
#fork and run in background
fork = true
#port = 27017
dbpath=/usr/local/mongodb/data
#location of pidfile
pidfilepath=/usr/local/mongodb/mongod.pid 

除了以mongod --dbpath=/usr/local/mongodb/data --fork --logpath=/usr/local/mongodb/logs這種方式啟動資料庫外,還可以通過配置檔案啟動資料庫:

之前開啟資料庫了,所以先關庫:

可以使用db.shutdownServer()命令

> use admin
switched to db admin
> db.shutdownServer()
2015-06-25T15:15:57.839+0800 I NETWORK  DBClientCursor::init call() failed
server should be down...
2015-06-25T15:15:57.841+0800 I NETWORK  trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2015-06-25T15:15:57.842+0800 W NETWORK  Failed to connect to 127.0.0.1:27017, reason: errno:104 Connection reset by peer
2015-06-25T15:15:57.842+0800 I NETWORK  reconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
  

#通過配置檔案連線資料庫

[[email protected] mongodb]$ mongod --config /usr/local/mongodb/mongod.cnf

about to fork child process, waiting until server is ready for connections.

forked process: 2059

child process started successfully, parent exiting

五:設定開機啟動

[[email protected] mongodb]$ su -

Password: 

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

新增:

/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongod.cnf

#重啟機器,檢測是否生效

[[email protected] ~]# reboot

--過一小會兒--

[[email protected] ~]# ps -ef | grep mongod

root      1828     1  0 17:32 ?        00:00:00 /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data --fork --logpath=/usr/local/mongodb/logs

root      1900  1624  0 17:33 pts/1    00:00:00 grep mongod

看到資料庫已經起來了。

六:怎樣使用以service方式啟動資料庫

前面啟動資料庫的方式略顯繁瑣,所以想用service mongodb start方式啟動資料庫。

預設service mongodb status會報錯(由於該服務沒有加入到service中

[[email protected] bin]# service mongodb status

mongodb: unrecognized service

需要進行如下配置:

[[email protected] ~]#vi /etc/init.d/mongod

新增:

#!/bin/sh
CONFIG=/usr/local/mongodb/mongod.cnf
PROGRAM=/usr/local/mongodb/bin/mongod
MONGOPID=`ps -ef | grep 'mongod --config' | grep -v grep | awk '{print $2}'`
 
test -x $PROGRAM || exit 0
 
case "$1" in
  start)
      echo "Starting MongoDB Server..."
      $PROGRAM --config $CONFIG &
  ;;
  stop)
      echo "Stopping MongoDB Server..."
      if [ ! -z "$MONGOPID" ]; then
          kill -15 $MONGOPID
      fi
  ;;
  status)
      if [  -z "$MONGOPID" ]; then
          echo "MongoDB is not running!"
      else
          echo "MongoDB is running!("$MONGOPID")"
      fi
  ;;
  restart)
     echo "Shutting down MongoDB Server..."
     if [ ! -z "$MONGOPID" ]; then
         kill -15 $MONGOPID
     fi
     echo "Starting MongoDB..."
     $PROGRAM --config $CONFIG &
  ;;
  *)
  log_success_msg "Usage:/etc/init.d/mongod {start|stop|status|restart}" 
exit 1
esac
exit 0          

#為該檔案賦予執行許可權

[[email protected] ~]# ls -l /etc/init.d/mongod 

-rw-r--r--. 1 root root 529 Jun 25 17:33 /etc/init.d/mongod

[[email protected] ~]# chmod 744 /etc/init.d/mongod

[[email protected] ~]# ls -l /etc/init.d/mongod

-rwxr--r--. 1 root root 529 Jun 25 17:33 /etc/init.d/mongod

測試效果:

[[email protected] mongodb]# service mongod stop

Stopping MongoDB Server

Terminated

Terminated

[[email protected] mongodb]# service mongod start

Starting MongoDB Server

[[email protected] mongodb]# about to fork child process, waiting until server is ready for connections.

forked process: 3632

child process started successfully, parent exiting