最近准备开始学习 MongoDB,了解一门 NoSQL 是非常必要的,今天学习了 MongoDB 的安装,步骤还是比较简单的。
1 环境信息
笔记本虚机: Red Hat Enterprise Linux Server release 6.2 MongoDB: 2.2.1
2 创建用户和目录
2.1 创建用户
1 2
[root@redhatB database]# groupadd mongo [root@redhatB database]# useradd -g mongo mongo
2.2 创建数据目录
1 2
[root@redhatB database]# mkdir -p /database/mongodb/data/ [root@redhatB database]# chown -R mongo:mongo /database/mongodb/data/
2.3 创建日志目录
1 2
[root@redhatB database]# mkdir -p /var/applog/mongo_log/ [root@redhatB database]# chown -R mongo:mongo /var/applog/mongo_log/
3 下载 MongoDB 2.2.1
http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.2.1.tgz
4 解压
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
[root@redhatB soft_bak]# tar zxvf mongodb-linux-i686-2.2 .1 .tgz mongodb-linux-i686-2.2 .1 /GNU-AGPL-3.0 mongodb-linux-i686-2.2 .1 /README mongodb-linux-i686-2.2 .1 /THIRD-PARTY-NOTICES mongodb-linux-i686-2.2 .1 /bin/mongodump mongodb-linux-i686-2.2 .1 /bin/mongorestore mongodb-linux-i686-2.2 .1 /bin/mongoexport mongodb-linux-i686-2.2 .1 /bin/mongoimport mongodb-linux-i686-2.2 .1 /bin/mongostat mongodb-linux-i686-2.2 .1 /bin/mongotop mongodb-linux-i686-2.2 .1 /bin/mongooplog mongodb-linux-i686-2.2 .1 /bin/mongofiles mongodb-linux-i686-2.2 .1 /bin/bsondump mongodb-linux-i686-2.2 .1 /bin/mongoperf mongodb-linux-i686-2.2 .1 /bin/mongosniff mongodb-linux-i686-2.2 .1 /bin/mongod mongodb-linux-i686-2.2 .1 /bin/mongos mongodb-linux-i686-2.2 .1 /bin/mongo [root@redhatB soft_bak]# mv mongodb-linux-i686-2.2 .1 /opt/mongodb2.2 .1 [root@redhatB opt]# chown -R mongo:mongo mongodb2.2 .1
5 修改环境变量
修改 mongo 用户 .bash_profile 文件,增加以下。
1 2 3 4
export MONGO_HOME =/opt/mongodb2.2.1 export MONGO_DATA =/database/mongodb/data/ export PATH =$MONGO_HOME /bin:$PATH[mongo@redhatB ~]$ source .bash_profile
6 启动 MongoDB
1
nohup mongod --dbpath=/database/m ongodb/data/ --logpath=/var/ applog/mongo_log/m ongo.log --logappend --port=27017 > /var/ applog/mongo_log/ start_mongodb.log 2 >&1 &
7 查看进程
1 2 3 4 5 6
[mongo@redhatB ~]$ ps -ef | grep mongo root 5565 4969 0 14 :58 pts/1 00:00:00 su - mongo mongo 5567 5565 0 14 :58 pts/1 00:00:00 -bash mongo 5675 5567 2 15 :05 pts/1 00:00:00 mongod --dbpath=/database/mongodb/data/ --logpath=/var/applog/mongo_log/mongo.log --logappend --port=27017 mongo 5684 5567 10 15 :05 pts/1 00:00:00 ps -ef mongo 5685 5567 0 15 :05 pts/1 00:00:00 grep mongo
8 第一次连接
1 2 3 4 5 6
[mongo@redhatB ~]$ mongo MongoDB shell version: 2.2.1 connecting to: test > > show dbs; local (empty)
备注:默认连接的是本地的 test 库,到这步 MongoDB 已经安装完成了,但通过 “show dbs” 命令,查不到 test 库信息。
9 插入数据
1 2
db.test_1 .save ( { 1 :"francs" } ) db.test_1 .save ( { 2 :"fpzhou" } )
10 查询数据
1 2 3 4
db.test_1.find (); > db.test_1.find (); { "_id" : ObjectId("50a0a07c7b54486ddb7f7593" ), "1" : "francs" } { "_id" : ObjectId("50a0a07f7b54486ddb7f7594" ), "2" : "fpzhou" }
11 再次查看数据库列表
1 2 3
> show dbs; local (empty) test 0.0625GB
备注:这时 test 库已创建并有数据了。
12 关闭 MongoDB
1 2 3 4 5 6 7
> use admin;switched to db admin > db.shutdownServer();Mon Nov 12 15 :11 :32 DBClientCursor::init call() failed Mon Nov 12 15 :11 :32 query failed : admin.$cmd { shutdown: 1.0 } to : 127.0 .0.1 :27017 server should be down... Mon Nov 12 15 :11 :32 trying reconnect to 127.0 .0.1 :27017 Mon Nov 12 15 :11 :32 reconnect 127.0 .0.1 :27017 failed couldn't connect to server 127.0 .0.1 :27017
13 参考
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-linux/ http://blog.163.com/dazuiba_008/blog/static/363349812011928433493/
原创文章,作者:306829225,如若转载,请注明出处:https://blog.ytso.com/237906.html