在linux环境下安装mongoDB,安装步骤备忘一下。
- 到mongoDB的官网下载mongoDB在Linux环境下最新版本安装包,目前最新的版本是3.2.4;由于我的Linux环境是CentOS6.5
32位的系统,所以下载链接是https://fastdl.mongodb.org/linux/mongodb-linux-i686-3.2.4.tgz - 下载完成之后,将包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参数。
启动命令如下:
[root@centOS-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
[root@centOS-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();
[root@centOS-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[root@centOS-1 ~]# 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