CentOS安装MongoDB

前端之家收集整理的这篇文章主要介绍了CentOS安装MongoDB前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。

MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。

本文记录一下在CentOS 6.7上安装MongoDB-3.4.1的过程。

MongoDB安装

下载

进入mongoDB下载网站,本文下载的是mongodb-linux-x86_64-amazon-3.4.1.tgz。如下图所示:

使用wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.4.1.tgz命令下载

[root@dev18 srv]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.4.1.tgz
--2017-01-04 15:18:33--  https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.4.1.tgz
Resolving fastdl.mongodb.org... 54.182.2.224,54.182.2.83,54.182.2.248,...
Connecting to fastdl.mongodb.org|54.182.2.224|:443... connected.
HTTP request sent,awaiting response... 200 OK
Length: 100395275 (96M) [application/x-gzip]
Saving to: “mongodb-linux-x86_64-amazon-3.4.1.tgz”

100%[====================================================================================================================================================================================================================================>] 100,395,275 12.6M/s   in 17s     

2017-01-04 15:18:53 (5.76 MB/s) - “mongodb-linux-x86_64-amazon-3.4.1.tgz” saved [100395275/100395275]

[root@dev18 srv]#

解压

使用tar -zvxf mongodb-linux-x86_64-amazon-3.4.1.tgz 命令解压。

[root@dev18 mongodb]# tar -zvxf mongodb-linux-x86_64-amazon-3.4.1.tgz 
mongodb-linux-x86_64-amazon-3.4.1/README
mongodb-linux-x86_64-amazon-3.4.1/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-amazon-3.4.1/MPL-2
mongodb-linux-x86_64-amazon-3.4.1/GNU-AGPL-3.0
mongodb-linux-x86_64-amazon-3.4.1/bin/mongodump
mongodb-linux-x86_64-amazon-3.4.1/bin/mongorestore
mongodb-linux-x86_64-amazon-3.4.1/bin/mongoexport
mongodb-linux-x86_64-amazon-3.4.1/bin/mongoimport
mongodb-linux-x86_64-amazon-3.4.1/bin/mongostat
mongodb-linux-x86_64-amazon-3.4.1/bin/mongotop
mongodb-linux-x86_64-amazon-3.4.1/bin/bsondump
mongodb-linux-x86_64-amazon-3.4.1/bin/mongofiles
mongodb-linux-x86_64-amazon-3.4.1/bin/mongooplog
mongodb-linux-x86_64-amazon-3.4.1/bin/mongoreplay
mongodb-linux-x86_64-amazon-3.4.1/bin/mongoperf
mongodb-linux-x86_64-amazon-3.4.1/bin/mongod
mongodb-linux-x86_64-amazon-3.4.1/bin/mongos
mongodb-linux-x86_64-amazon-3.4.1/bin/mongo
[root@dev18 mongodb]#

进入解压后的目录,使用ll命令,查看解压后的目录中的内容

[root@dev18 mongodb]# cd mongodb-linux-x86_64-amazon-3.4.1
[root@dev18 mongodb-linux-x86_64-amazon-3.4.1]# ll
total 120
drwxr-xr-x 2 root root  4096 Jan  4 15:21 bin
-rw-rw-r-- 1  222  500 34520 Dec 21 03:13 GNU-AGPL-3.0
-rw-rw-r-- 1  222  500 16726 Dec 21 03:13 MPL-2
-rw-rw-r-- 1  222  500  1359 Dec 21 03:13 README
-rw-rw-r-- 1  222  500 55625 Dec 21 03:13 THIRD-PARTY-NOTICES
[root@dev18 mongodb-linux-x86_64-amazon-3.4.1]#

启动MongoDB

进入bin目录,使用./mongod命令启动MongoDB,出现异常:

[root@dev18 bin]# ./mongod
2017-01-04T15:38:20.174+0800 I CONTROL  [initandlisten] MongoDB starting : pid=30735 port=27017 dbpath=/data/db 64-bit host=dev18.gzhl.zhhl
2017-01-04T15:38:20.175+0800 I CONTROL  [initandlisten] db version v3.4.1
2017-01-04T15:38:20.175+0800 I CONTROL  [initandlisten] git version: 5e103c4f5583e2566a45d740225dc250baacfbd7
2017-01-04T15:38:20.175+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.0-fips 29 Mar 2010
2017-01-04T15:38:20.175+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2017-01-04T15:38:20.175+0800 I CONTROL  [initandlisten] modules: none
2017-01-04T15:38:20.175+0800 I CONTROL  [initandlisten] build environment:
2017-01-04T15:38:20.175+0800 I CONTROL  [initandlisten]     distmod: amazon
2017-01-04T15:38:20.175+0800 I CONTROL  [initandlisten]     distarch: x86_64
2017-01-04T15:38:20.175+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2017-01-04T15:38:20.175+0800 I CONTROL  [initandlisten] options: {}
2017-01-04T15:38:20.175+0800 I STORAGE  [initandlisten] exception in initAndListen: 29 Data directory /data/db not found.,terminating
2017-01-04T15:38:20.175+0800 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2017-01-04T15:38:20.175+0800 I NETWORK  [initandlisten] shutdown: going to flush diaglog...
2017-01-04T15:38:20.175+0800 I CONTROL  [initandlisten] now exiting
2017-01-04T15:38:20.175+0800 I CONTROL  [initandlisten] shutting down with code:100
[root@dev18 bin]#

如果此时,使用MongoDB shell ./mongo来看,遇到无法连接到服务器的错误信息 (Error: couldn't connect to server 127.0.0.1:27017,connection attempt Failed )。

[root@dev18 bin]# ./mongo
MongoDB shell version v3.4.1
connecting to: mongodb://127.0.0.1:27017
2017-01-04T15:40:55.030+0800 W NETWORK  [main] Failed to connect to 127.0.0.1:27017,in(checking socket for error after poll),reason: Connection refused
2017-01-04T15:40:55.031+0800 E QUERY    [main] Error: couldn't connect to server 127.0.0.1:27017,connection attempt Failed :
connect@src/mongo/shell/mongo.js:234:13
@(connect):1:6
exception: connect Failed
[root@dev18 bin]#

在本文中,创建一个db目录,然后启动的时候指定dbpath就可以了。使用如下命令:

./mongod --dbpath=/srv/mongodb/db

[root@dev18 bin]# ./mongod --dbpath=/srv/mongodb/db
2017-01-04T15:44:22.039+0800 I CONTROL  [initandlisten] MongoDB starting : pid=30798 port=27017 dbpath=/srv/mongodb/db 64-bit host=dev18.gzhl.zhhl
2017-01-04T15:44:22.039+0800 I CONTROL  [initandlisten] db version v3.4.1
2017-01-04T15:44:22.039+0800 I CONTROL  [initandlisten] git version: 5e103c4f5583e2566a45d740225dc250baacfbd7
2017-01-04T15:44:22.039+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.0-fips 29 Mar 2010
2017-01-04T15:44:22.039+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2017-01-04T15:44:22.039+0800 I CONTROL  [initandlisten] modules: none
2017-01-04T15:44:22.039+0800 I CONTROL  [initandlisten] build environment:
2017-01-04T15:44:22.039+0800 I CONTROL  [initandlisten]     distmod: amazon
2017-01-04T15:44:22.039+0800 I CONTROL  [initandlisten]     distarch: x86_64
2017-01-04T15:44:22.039+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2017-01-04T15:44:22.039+0800 I CONTROL  [initandlisten] options: { storage: { dbPath: "/srv/mongodb/db" } }
2017-01-04T15:44:22.062+0800 I STORAGE  [initandlisten] 
2017-01-04T15:44:22.062+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-01-04T15:44:22.062+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-01-04T15:44:22.062+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1403M,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] 
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user,which is not recommended.
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] 
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] 
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] 
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] 
2017-01-04T15:44:22.150+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/srv/mongodb/db/diagnostic.data'
2017-01-04T15:44:22.160+0800 I INDEX    [initandlisten] build index on: admin.system.version properties: { v: 2,key: { version: 1 },name: "incompatible_with_version_32",ns: "admin.system.version" }
2017-01-04T15:44:22.160+0800 I INDEX    [initandlisten] 	 building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2017-01-04T15:44:22.161+0800 I INDEX    [initandlisten] build index done.  scanned 0 total records. 0 secs
2017-01-04T15:44:22.161+0800 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.4
2017-01-04T15:44:22.162+0800 I NETWORK  [thread1] waiting for connections on port 27017

发现没有任何错误信息。然后,使用MongoDB shell ./mongo再来看一下:

[root@dev18 bin]# ./mongo
MongoDB shell version v3.4.1
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.1
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: 
2017-01-04T15:44:22.062+0800 I STORAGE  [initandlisten] 
2017-01-04T15:44:22.062+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-01-04T15:44:22.062+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] 
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user,which is not recommended.
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] 
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] 
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] 
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-01-04T15:44:22.141+0800 I CONTROL  [initandlisten] 
>

从上面的信息可以看出,已经成功连接上MongoDB了。

MongoDB Shell 示例

查看版本

使用db.version()查看MongoDB版本信息。

> db.version();
3.4.1

创建数据库

使用use DATABASE_NAME创建一个数据库

如果数据库不存在,则创建数据库,否则切换到指定数据库

> use mydb
switched to db mydb

插入&查询

在mydb中创建记录,并查询出来。

> use mydb
switched to db mydb
> db
mydb
> db.paper.insert({"name":"MongoDB in action"})
WriteResult({ "nInserted" : 1 })
> show dbs
admin  0.000GB
local  0.000GB
mydb   0.000GB
> db.paper.findOne()
{
	"_id" : ObjectId("586cab1ecf428c0bf101588c"),"name" : "MongoDB in action"
}
>

至此,MongoDB的简单安装就好了。

原文链接:https://www.f2er.com/centos/379210.html

猜你在找的CentOS相关文章