MongoDB在CentOS下的安装、启动和配置

1、安装

下载链接:http://www.mongodb.org/downloads

 

下载安装包

下载版本:rhel70-3.0.0

下载链接:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.0.0.tgz

可以在windowns下载后通过ssh工具上传到CentOS或直接用下面命令下载

[[email protected] software]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.0.0.tgz

 下载完后,解压:

[[email protected] software]# tar -zxvf mongodb-linux-x86_64-rhel70-3.0.0.tgz
mongodb-linux-x86_64-rhel70-3.0.0/README
mongodb-linux-x86_64-rhel70-3.0.0/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel70-3.0.0/GNU-AGPL-3.0
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongodump
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongorestore
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongoexport
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongoimport
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongostat
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongotop
mongodb-linux-x86_64-rhel70-3.0.0/bin/bsondump
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongofiles
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongooplog
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongoperf
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongod
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongos
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongo
[[email protected] software]#

 

一般我们将安装的软件安装在/usr/local目录下,所以将解压后的文件移至/usr/local目录,并重命名,如下

[[email protected]clinton software]# mv mongodb-linux-x86_64-rhel70-3.0.0 /usr/local/mongodb
[[email protected] software]# cd /usr/local/

 

我们在mongodb目录下新建一个目录data用于存放数据,新建一个目录log用于存放日志,然后在该目录下新建一个日志文件mongodb.log,操作如下

[[email protected] local]# cd mongodb/
[[email protected] mongodb]# ll
总用量 68
drwxr-xr-x. 2 root root  4096 3月  12 15:55 bin
-rw-r--r--. 1 root root 34520 2月  28 01:43 GNU-AGPL-3.0
-rw-r--r--. 1 root root  1359 2月  28 01:43 README
-rw-r--r--. 1 root root 22660 2月  28 01:43 THIRD-PARTY-NOTICES
[[email protected] mongodb]# mkdir data
[[email protected] mongodb]# mkdir log
[[email protected] mongodb]# cd log/
[[email protected] log]# touch mongodb.log

 

2、启动mongodb

将目录定位到/usr/local/mongodb

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

 

使用mongod命令建立一个mongodb数据库链接,端口号设置为10001,数据库的路径为/usr/local/mongodb/data,日志路径为/usr/local/mongodb/log/mogodb.log

注:可用  --help查看命令参数的作用

[[email protected] mongodb]# ./bin/mongod --port 10001 --dbpath data/ --logpath log/mongodb.log
2015-03-12T16:18:33.489+0800 I CONTROL  log file "/usr/local/mongodb/log/mongodb.log" exists; moved to "/usr/local/mongodb/log/mongodb.log.2015-03-12T08-18-33".

 以上方式是在前台启动Mongodb进程,如果Session窗口关闭,Mongodb进程也随之停止,我们的客户端要连接需要另外开启一个终端。不过Mongodb同时还提供了一种后台Daemon方式启动,只需要加上一个"--fork"参数即可,值得注意的是,用到了"--fork"参数就必须启用"--logpath"参数。如下所示:

[[email protected] mongodb]# ./bin/mongod --port 10001 --dbpath data/ --fork
BadValue --fork has to be used with --logpath or --syslog
try './bin/mongod --help' for more information
[[email protected] mongodb]# ./bin/mongod --port 10001 --dbpath data/ --fork --logpath=log/mongodb.log
about to fork child process, waiting until server is ready for connections.
forked process: 6288
child process started successfully, parent exiting
[[email protected] mongodb]# ps -ef|grep mongodb
root      6288     1  1 16:27 ?        00:00:00 ./bin/mongod --port 10001 --dbpath data/ --fork --logpath=log/mongodb.log
root      6304  2292  0 16:27 pts/0    00:00:00 grep --color=auto mongodb
[[email protected] mongodb]#

 

3、客户端连接

将目录切换到mongodb目录下,并使用mongo命令来连接该数据库

[[email protected] mongodb]# pwd
/usr/local/mongodb
[[email protected] mongodb]# ./bin/mongo localhost:10001
MongoDB shell version: 3.0.0
connecting to: localhost:10001/test
Server has startup warnings:
2015-03-12T16:27:20.453+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-03-12T16:27:20.453+0800 I CONTROL  [initandlisten]
2015-03-12T16:27:20.453+0800 I CONTROL  [initandlisten]
2015-03-12T16:27:20.453+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-03-12T16:27:20.453+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-03-12T16:27:20.453+0800 I CONTROL  [initandlisten]
2015-03-12T16:27:20.453+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-03-12T16:27:20.453+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-03-12T16:27:20.453+0800 I CONTROL  [initandlisten]
>

 

往数据库中插入值

> db.foo.save({a:1})
WriteResult({ "nInserted" : 1 })
>

 从数据库中查询

> db.foo.find()
{ "_id" : ObjectId("55014b84b66287928352f381"), "a" : 1 }
>

 

通过浏览器访问

在浏览器地址栏输入: http://localhost:10001/ 然后回车访问 (localhost可以换成具体的服务器IP)

可以看到如下提示:It looks like you are trying to access MongoDB over HTTP on the native driver port.

不知道什么原因,google了一下,发现在官网有如下解释:

 

--httpinterface

New in version 2.6.

Enables the HTTP interface. Enabling the interface can increase network exposure.

Leave the HTTP interface disabled for production deployments. If you do enable this interface, you should only allow trusted clients to access this port. See Firewalls.

 

 

应该是自2.6版本以后默认就不开启HTTP接口访问,于是重启加上--httpinterface启动

[[email protected] mongodb]# ./bin/mongod --port 10001 --dbpath data/ --fork --logpath=log/mongodb.log --httpinterface
about to fork child process, waiting until server is ready for connections.
forked process: 7760
child process started successfully, parent exiting

 

再在地址栏中输入:http://localhost:10001/ 回车

发现仍然有上面的提示:It looks like you are trying to access MongoDB over HTTP on the native driver port.

 

之前的版本直接访问10001端口会提示:You are trying to access MongoDB on the native driver port. For http diagnostic access, add 1000 to the port number

 

于是把端口加上1000,http://localhost:11001/,回车后,就能够访问到Monodb的服务端web页面

 

在CentOS中

MongoDB在CentOS下的安装、启动和配置
 

在windowns中

MongoDB在CentOS下的安装、启动和配置
 

 

4、通过配置文件来配置Mongodb

创建mongodb的配置文件conf/mongodb.conf

 

[[email protected] mongodb]# pwd
/usr/local/mongodb
[[email protected] mongodb]# mkdir conf
[[email protected] mongodb]# cd conf
[[email protected] conf]# vim mongodb.conf

 

在mongodb.conf中加入下面内容:

 

port=10001
dbpath=/usr/local/mongodb/data/
logpath=/usr/local/mongodb/log/mongodb.log
logappend=true
fork=true

 

解释说明:

port=10001【代表端口号,如果不指定则默认为 27017 】

dbpath=data/ 【数据库路径】

logpath=log/mongodb.log 【日志路径】

logappend=true 【日志文件自动累加,而不是覆盖】

fork=true 【后台执行方式启动】

 

启动mongodb服务

[[email protected] mongodb]# ./bin/mongod -f conf/mongodb.conf --httpinterface
about to fork child process, waiting until server is ready for connections.
forked process: 8421
child process started successfully, parent exiting

 

然后访问方式和上面一样

 

停掉服务

关闭mongodb可以用如下方式

[[email protected] mongodb]# ps -ef|grep mongodb
root      8421     1  0 17:35 ?        00:00:01 ./bin/mongod -f conf/mongodb.conf --httpinterface
root      8459  2292  0 17:38 pts/0    00:00:00 grep --color=auto mongodb
[[email protected] mongodb]# kill -9 8421
[[email protected] mongodb]#