Hot For Coding

Linux下安装MongoDB教程

MongoDB是NoSQL中的一种,随着越来越多公司产品使用,已经开始逐步取代一些高性能查询的地位了。它使用简单,安装更简单。

下载MongoDB

-bash-3.2# wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.4.1.tgz
--2013-03-29 23:25:20--  http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.4.1.tgz
Resolving fastdl.mongodb.org... 205.251.212.177, 205.251.212.190, 205.251.212.16, ...
Connecting to fastdl.mongodb.org|205.251.212.177|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 93077588 (89M) [application/x-tar]
Saving to: `mongodb-linux-i686-2.4.1.tgz'
 
100%[============================================================================================================================>] 93,077,588   114K/s   in 14m 6s  
 
2013-03-29 23:39:27 (107 KB/s) - `mongodb-linux-i686-2.4.1.tgz' saved [93077588/93077588]
 
-bash-3.2# ls
db  install.log  install.log.syslog  logs  mongodb-linux-i686-2.4.1.tgz
-bash-3.2#

解压任意目录

-bash-3.2# tar -xvf mongodb-linux-i686-2.4.1.tgz 
mongodb-linux-i686-2.4.1/README
mongodb-linux-i686-2.4.1/THIRD-PARTY-NOTICES
mongodb-linux-i686-2.4.1/GNU-AGPL-3.0
mongodb-linux-i686-2.4.1/bin/mongodump
mongodb-linux-i686-2.4.1/bin/mongorestore
mongodb-linux-i686-2.4.1/bin/mongoexport
mongodb-linux-i686-2.4.1/bin/mongoimport
mongodb-linux-i686-2.4.1/bin/mongostat
mongodb-linux-i686-2.4.1/bin/mongotop
mongodb-linux-i686-2.4.1/bin/mongooplog
mongodb-linux-i686-2.4.1/bin/mongofiles
mongodb-linux-i686-2.4.1/bin/bsondump
mongodb-linux-i686-2.4.1/bin/mongoperf
mongodb-linux-i686-2.4.1/bin/mongosniff
mongodb-linux-i686-2.4.1/bin/mongod
mongodb-linux-i686-2.4.1/bin/mongos
mongodb-linux-i686-2.4.1/bin/mongo
-bash-3.2#

创建一个存储数据的目录

-bash-3.2# mkdir db
-bash-3.2# ls
db  install.log  install.log.syslog  logs  mongodb-linux-i686-2.4.1  mongodb-linux-i686-2.4.1.tgz
-bash-3.2#

创建一个存储日志的目录以及文件

-bash-3.2# mkdir log
-bash-3.2# ls
db  install.log  install.log.syslog  log  logs  mongodb-linux-i686-2.4.1  mongodb-linux-i686-2.4.1.tgz
-bash-3.2# cd log
-bash-3.2# touch mongodb.log
-bash-3.2# chmod 777 mongodb.log 
-bash-3.2# ls
mongodb.log
-bash-3.2#

启动MongoDB

-bash-3.2# cd ..
-bash-3.2# ls
db  install.log  install.log.syslog  log  logs  mongodb-linux-i686-2.4.1  mongodb-linux-i686-2.4.1.tgz
-bash-3.2# cd mongodb-linux-i686-2.4.1
-bash-3.2# ./bin/mongod --dbpath=/root/db --logpath=/root/log/mongodb.log
Fri Mar 29 23:49:21.920 
Fri Mar 29 23:49:21.920 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Fri Mar 29 23:49:21.920 
all output going to: /root/log/mongodb.log
log file [/root/log/mongodb.log] exists; copied to temporary file [/root/log/mongodb.log.2013-03-29T15-49-21]

客户端连接

-bash-3.2# ./bin/mongo
MongoDB shell version: 2.4.1
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
Server has startup warnings: 
Fri Mar 29 23:49:21.942 [initandlisten] 
Fri Mar 29 23:49:21.942 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Fri Mar 29 23:49:21.942 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
Fri Mar 29 23:49:21.942 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.
Fri Mar 29 23:49:21.942 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Fri Mar 29 23:49:21.942 [initandlisten] 
> show dbs 
local   0.03125GB
>

查看日志

-bash-3.2# cat ../log/mongodb.log
Fri Mar 29 23:49:21.942 [initandlisten] MongoDB starting : pid=24536 port=27017 dbpath=/root/db 32-bit host=localhost.localdomain
Fri Mar 29 23:49:21.942 [initandlisten] 
Fri Mar 29 23:49:21.942 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Fri Mar 29 23:49:21.942 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
Fri Mar 29 23:49:21.942 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.
Fri Mar 29 23:49:21.942 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Fri Mar 29 23:49:21.942 [initandlisten] 
Fri Mar 29 23:49:21.942 [initandlisten] db version v2.4.1
Fri Mar 29 23:49:21.942 [initandlisten] git version: 1560959e9ce11a693be8b4d0d160d633eee75110
Fri Mar 29 23:49:21.942 [initandlisten] build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Fri Mar 29 23:49:21.942 [initandlisten] allocator: system
Fri Mar 29 23:49:21.942 [initandlisten] options: { dbpath: "/root/db", logpath: "/root/log/mongodb.log" }
Fri Mar 29 23:49:21.944 [FileAllocator] allocating new datafile /root/db/local.ns, filling with zeroes...
Fri Mar 29 23:49:21.944 [FileAllocator] creating directory /root/db/_tmp
Fri Mar 29 23:49:22.013 [FileAllocator] done allocating datafile /root/db/local.ns, size: 16MB,  took 0.068 secs
Fri Mar 29 23:49:22.013 [FileAllocator] allocating new datafile /root/db/local.0, filling with zeroes...
Fri Mar 29 23:49:22.082 [FileAllocator] done allocating datafile /root/db/local.0, size: 16MB,  took 0.068 secs
Fri Mar 29 23:49:22.083 [initandlisten] command local.$cmd command: { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0  reslen:37 139ms
Fri Mar 29 23:49:22.083 [initandlisten] waiting for connections on port 27017
Fri Mar 29 23:49:22.083 [websvr] admin web console waiting for connections on port 28017
Fri Mar 29 23:51:39.357 [initandlisten] connection accepted from 127.0.0.1:53490 #1 (1 connection now open)
Fri Mar 29 23:52:25.981 [conn1] end connection 127.0.0.1:53490 (0 connections now open)
-bash-3.2#

MongoDB默认是不需要用户名与密码验证就能连接,这样不太安全,下一篇我们给MongoDB加上用户名与密码验证,提高安全性。

TITLE: Linux下安装MongoDB教程

LINK: https://www.qttc.net/298_install_mongodb_on_linux.html

NOTE: 转载内容请注明出处