centos7平台安装MongoDB
Posted 遂越净郝
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos7平台安装MongoDB相关的知识,希望对你有一定的参考价值。
以下演示的是 64 位 centos7上的安装
一、下载MongoDB
1.下载安装包
# curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.4.10.tgz
2.解压 tgz
# tar -zxvf mongodb-linux-x86_64-rhel70-3.4.10.tgz
3.将解压包拷贝到指定目录
# mv mongodb-linux-x86_64-rhel70-3.4.10/ /usr/local/mongodb
#/ /两'/'之间存在空格
二、将MongoDB 的可执行文件增添到 PATH 路径中
# export PATH=<mongodb-install-directory>/bin:$PATH
<mongodb-install-directory> 为你 MongoDB 的安装路径。如本文的 /usr/local/mongodb
即:
# export PATH=/usr/local/mongodb/bin:$PATH
查看PATH
# echo $PATH
/usr/local/mongodb/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
三、创建数据库目录和log目录
1.创建db目录
MongoDB的数据存储在data目录的db目录下,但是这个目录在安装过程不会自动创建,所以你需要手动创建data目录,并在data目录中创建db目录
本文将目录放在 根目录下(/)
即:
/data/db
2.创建log目录以及mongod.log文件
# cd /data/db
# mkdir log
# touch mongod.log
后可参照【MongoDB】mongodb.conf 配置文件
使用logpath来指定
四、命令行中运行 MongoDB 服务
你可以再命令行中执行mongo安装目录中的bin目录执行mongod命令来启动mongdb服务
本文安装在 /usr/local/mongodb
# cd /usr/local/mongodb/mongodb-linux-x86_64-rhel70-3.4.10/bin
注意:如果你的数据库目录不是/data/db,可以通过 --dbpath 来指定。
$ ./mongod
2017-11-07T15:28:32.074+0800 I CONTROL [initandlisten] MongoDB starting : pid=18339 port=27017 dbpath=/data/db 64-bit host=zhurui802
2017-11-07T15:28:32.074+0800 I CONTROL [initandlisten] db version v3.4.10
2017-11-07T15:28:32.074+0800 I CONTROL [initandlisten] git version: 078f28920cb24de0dd479b5ea6c66c644f6326e9
2017-11-07T15:28:32.074+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2017-11-07T15:28:32.074+0800 I CONTROL [initandlisten] allocator: tcmalloc
五、MongoDB后台管理 Shell
1.进入mongodb安装目录下的bin目录
# cd /usr/local/mongodb/mongodb-linux-x86_64-rhel70-3.4.10/bin
2.执行mongo命令文件
# ./mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
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
【问题】
mongodb exception: connect failed
# ./mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
2017-11-07T17:13:57.455+0800 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2017-11-07T17:13:57.456+0800 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:237:13
@(connect):1:6
exception: connect failed
【解决办法】
# sudo ./mongod
2017-11-07T17:16:21.024+0800 I CONTROL [initandlisten]
2017-11-07T17:16:21.025+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 3895 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2017-11-07T17:16:21.025+0800 I CONTROL [initandlisten]
2017-11-07T17:16:21.026+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2017-11-07T17:16:21.027+0800 I NETWORK [thread1] waiting for connections on port 27017
后一直卡在这个界面
不用担心
此时数据库就已经启动
3.新开[注意是新开]一个终端
再次进入mongodb安装目录下的bin目录
# cd /usr/local/mongodb/mongodb-linux-x86_64-rhel70-3.4.10/bin
再启动一次mongodb程序
# ./mongo
【注意事项】运行第二个终端时,第一个mongodb启动窗口切勿关闭
gepage/defrag is 'always'.
2017-11-07T17:16:21.024+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-11-07T17:16:21.024+0800 I CONTROL [initandlisten]
2017-11-07T17:16:21.025+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 3895 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2017-11-07T17:16:21.025+0800 I CONTROL [initandlisten]
>
六、远程登陆MongoDb
1.无密码
2.有密码
以上是关于centos7平台安装MongoDB的主要内容,如果未能解决你的问题,请参考以下文章