MongoDB 安装及测试

为了学习先单机安装,尝试一下mongodb的魅力。当前最新版本为3.0,目前我们以2.6.8版本为例,后续再专门了解一下mongodb 3.0。

下载地址:http://www.mongodb.org/downloads

一、下载安装、配置

下载:wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.8.tgz(也可以通过下载源码编译)
解压:tar-zxvf mongodb-linux-x86_64-2.6.8.tgz
重命名:mv mongodb-linux-x86_64-2.6.8 mongodb-2.6.8
创建数据和日志存储文件夹:
cd mongodb-2.6.8
mkdir  data
mkdir  logs

touch ./logs/mongodb.log

安装目录bin下文件:

-rwxr-xr-x. 1 mongo mongo 23377044 Feb 24 10:21 bsondump
-rwxr-xr-x. 1 mongo mongo 11658496 Feb 24 10:21 mongo
-rwxr-xr-x. 1 mongo mongo 23565076 Feb 24 10:21 mongod
-rwxr-xr-x. 1 mongo mongo 23448820 Feb 24 10:20 mongodump
-rwxr-xr-x. 1 mongo mongo 23396212 Feb 24 10:20 mongoexport
-rwxr-xr-x. 1 mongo mongo 23441152 Feb 24 10:21 mongofiles
-rwxr-xr-x. 1 mongo mongo 23416180 Feb 24 10:20 mongoimport
-rwxr-xr-x. 1 mongo mongo 23387380 Feb 24 10:21 mongooplog
-rwxr-xr-x. 1 mongo mongo 23194084 Feb 24 10:21 mongoperf
-rwxr-xr-x. 1 mongo mongo 23487572 Feb 24 10:20 mongorestore
-rwxr-xr-x. 1 mongo mongo 18136944 Feb 24 10:21 mongos
-rwxr-xr-x. 1 mongo mongo 23437780 Feb 24 10:21 mongostat
-rwxr-xr-x. 1 mongo mongo 23380116 Feb 24 10:21 mongotop

二、启动、关闭

1、正常启动

启动mongodb(如果采用安全认证模式,需要加上--auth选项,--fork以创建子进程的方式运行):

./bin/mongod --dbpath=/home/mongo/mongodb-2.6.8/data --logpath=/home/mongo/mongodb-2.6.8/logs/mongodb.log --logappend &

默认启动27017端口。

2、以修复模式启动
mongod --repair
以修复模式启动数据库。

3、终止服务器进程

>use admin;

>db.shutdownServer();

终止数据库服务器进程

也可以:ps -ef |grep mongod   通过kill -9 pid 关闭

mongod命令说明:

[mongo@salve-f mongodb-2.6.8]$ ./bin/mongod --help
Options:

General options:
  -h [ --help ]               show this usage information
  --version                   show version information
  -f [ --config ] arg         configuration file specifying additional options
  -v [ --verbose ] [=arg(=v)] be more verbose (include multiple times for more 
                              verbosity e.g. -vvvvv)
  --quiet                     quieter output
  --port arg                  specify port number - 27017 by default
  --bind_ip arg               comma separated list of ip addresses to listen on
                              - all local ips by default
  --maxConns arg              max number of simultaneous connections - 1000000 
                              by default
  --logpath arg               log file to send write to instead of stdout - has
                              to be a file, not directory
  --syslog                    log to system's syslog facility instead of file 
                              or stdout
  --syslogFacility arg        syslog facility used for monogdb syslog message
  --logappend                 append to logpath instead of over-writing
  --timeStampFormat arg       Desired format for timestamps in log messages. 
                              One of ctime, iso8601-utc or iso8601-local
  --pidfilepath arg           full path to pidfile (if not set, no pidfile is 
                              created)
  --keyFile arg               private key for cluster authentication
  --setParameter arg          Set a configurable parameter
  --httpinterface             enable http interface
  --clusterAuthMode arg       Authentication mode used for cluster 
                              authentication. Alternatives are 
                              (keyFile|sendKeyFile|sendX509|x509)
  --nounixsocket              disable listening on unix sockets
  --unixSocketPrefix arg      alternative directory for UNIX domain sockets 
                              (defaults to /tmp)
  --fork                      fork server process
  --auth                      run with security
  --noauth                    run without security
  --ipv6                      enable IPv6 support (disabled by default)
  --jsonp                     allow JSONP access via http (has security 
                              implications)
  --rest                      turn on simple rest api
  --slowms arg (=100)         value of slow for profile and console log
  --profile arg               0=off 1=slow, 2=all
  --cpu                       periodically show cpu and iowait utilization
  --sysinfo                   print some diagnostic system information
  --dbpath arg                directory for datafiles - defaults to /data/db
  --directoryperdb            each database will be stored in a separate 
                              directory
  --noIndexBuildRetry         don't retry any index builds that were 
                              interrupted by shutdown
  --noprealloc                disable data file preallocation - will often hurt
                              performance
  --nssize arg (=16)          .ns file size (in MB) for new databases
  --quota                     limits each database to a certain number of files
                              (8 default)
  --quotaFiles arg            number of files allowed per db, implies --quota
  --smallfiles                use a smaller default file size
  --syncdelay arg (=60)       seconds between disk syncs (0=never, but not 
                              recommended)
  --upgrade                   upgrade db if needed
  --repair                    run repair on all dbs
  --repairpath arg            root directory for repair files - defaults to 
                              dbpath
  --noscripting               disable scripting engine
  --notablescan               do not allow table scans
  --journal                   enable journaling
  --nojournal                 disable journaling (journaling is on by default 
                              for 64 bit)
  --journalOptions arg        journal diagnostic options
  --journalCommitInterval arg how often to group/batch commit (ms)
  --shutdown                  kill a running server (for init scripts)

Replication options:
  --oplogSize arg       size to use (in MB) for replication op log. default is 
                        5% of disk space (i.e. large is good)

Master/slave options (old; use replica sets instead):
  --master              master mode
  --slave               slave mode
  --source arg          when slave: specify master as <server:port>
  --only arg            when slave: specify a single database to replicate
  --slavedelay arg      specify delay (in seconds) to be used when applying 
                        master ops to slave
  --autoresync          automatically resync if slave data is stale

Replica set options:
  --replSet arg           arg is <setname>[/<optionalseedhostlist>]
  --replIndexPrefetch arg specify index prefetching behavior (if secondary) 
                          [none|_id_only|all]

Sharding options:
  --configsvr           declare this is a config db of a cluster; default port 
                        27019; default dir /data/configdb
  --shardsvr            declare this is a shard db of a cluster; default port 

三、客户端命令测试

使用客户端命令连接mongodb:

> mongo (可以指定IP、端口、及数据库等)

> use test_db; //创建数据库

> db.createCollection("test"); //创建表

> db.test_db.save({"name":"jetty","age":25}); //添加数据

> db.test_db.test.find(); //查询数据

{ "_id" : ObjectId("5503f6e856d0297944b9c8f0"), "name" : "jetty", "age" : 25 }

你可能感兴趣的:(mongodb)