linux下安装配置mongodb
2015-06-25 17:54
597 查看
一:下载
下载地址:https://www.mongodb.org/
二:安装
2.1 解压
[root@ser6-51 download]# ls -ltr total 71552 -rw-r--r-- 1 root root 32979820 Jun 15 10:01 mysql-5.6.20.tar.gz drwxr-xr-x 4 root root 4096 Jun 16 13:39 mysql -rw-r--r-- 1 root root 40280527 Jun 25 11:54 mongodb-linux-x86_64-3.0.4.tgz [root@ser6-51 download]# tar -xvf mongodb-linux-x86_64-3.0.4.tgz mongodb-linux-x86_64-3.0.4/README mongodb-linux-x86_64-3.0.4/THIRD-PARTY-NOTICES mongodb-linux-x86_64-3.0.4/GNU-AGPL-3.0 mongodb-linux-x86_64-3.0.4/bin/mongodump mongodb-linux-x86_64-3.0.4/bin/mongorestore mongodb-linux-x86_64-3.0.4/bin/mongoexport mongodb-linux-x86_64-3.0.4/bin/mongoimport mongodb-linux-x86_64-3.0.4/bin/mongostat mongodb-linux-x86_64-3.0.4/bin/mongotop mongodb-linux-x86_64-3.0.4/bin/bsondump mongodb-linux-x86_64-3.0.4/bin/mongofiles mongodb-linux-x86_64-3.0.4/bin/mongooplog mongodb-linux-x86_64-3.0.4/bin/mongoperf mongodb-linux-x86_64-3.0.4/bin/mongod mongodb-linux-x86_64-3.0.4/bin/mongos mongodb-linux-x86_64-3.0.4/bin/mongo [root@ser6-51 download]# ls -ltr total 71556 -rw-r--r-- 1 root root 32979820 Jun 15 10:01 mysql-5.6.20.tar.gz drwxr-xr-x 4 root root 4096 Jun 16 13:39 mysql -rw-r--r-- 1 root root 40280527 Jun 25 11:54 mongodb-linux-x86_64-3.0.4.tgz drwxr-xr-x 3 root root 4096 Jun 25 11:54 mongodb-linux-x86_64-3.0.4
#为了方便管理,把安装文件移动到/usr/local下
[root@ser6-51 download]# mv mongodb-linux-x86_64-3.0.4 /usr/local/mongodb
2.2 创建相关目录
为mongodb创建数据库存放的位置和日志文件[root@ser6-51 mongodb]# cd /usr/local/mongodb
[root@ser6-51 mongodb]# mkdir data
[root@ser6-51 mongodb]# touch logs
[root@ser6-51 mongodb]# ls -ltr
total 72
-rw-r--r-- 1 1046 1046 22660 Jun 15 23:51 THIRD-PARTY-NOTICES
-rw-r--r-- 1 1046 1046 1359 Jun 15 23:51 README
-rw-r--r-- 1 1046 1046 34520 Jun 15 23:51 GNU-AGPL-3.0
drwxr-xr-x 2 root root 4096 Jun 25 11:54 bin
drwxr-xr-x 2 root root 4096 Jun 25 12:00 data
-rw-r--r-- 1 root root 0 Jun 25 12:00 logs
2.3 创建Linux用户
[root@ser6-51 init.d]# groupadd mongodb[root@ser6-51 init.d]# useradd -g mongodb mongodb
[root@ser6-51 init.d]# chown -R mongodb:mongodb /usr/local/mongodb
2.4 配置PATH
#mongodb用户:在PATH末尾添加Mongodb 安装文件/bin路径:
[root@ser6-51 init.d]# su - mongodb
[mongodb@ser6-51 ~]$ vi .bash_profile
PATH=$PATH:$HOME/bin:/usr/local/mongodb/bin
[mongodb@ser6-51 ~]$ source .bash_profile
这样就可以不用进入到/usr/local/mongodb/bin路径下使用mongo命令了,直接输入mongo命令即可。
#root用户省略
三:启动mongodb
#验证是否安装成功[mongodb@ser6-51 ~]$ mongod --dbpath=/usr/local/mongodb/data --fork --logpath=/usr/local/mongodb/logs about to fork child process, waiting until server is ready for connections. forked process: 12135 child process started successfully, parent exiting [root@ser6-51 bin]# mongo MongoDB shell version: 3.0.4 connecting to: test Server has startup warnings: 2015-06-25T12:02:26.238+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2015-06-25T12:02:26.238+0800 I CONTROL [initandlisten] 2015-06-25T12:02:26.247+0800 I CONTROL [initandlisten] 2015-06-25T12:02:26.247+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2015-06-25T12:02:26.247+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2015-06-25T12:02:26.247+0800 I CONTROL [initandlisten] 2015-06-25T12:02:26.247+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2015-06-25T12:02:26.247+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2015-06-25T12:02:26.247+0800 I CONTROL [initandlisten] > show dbs; local 0.078GB
可以看到有Local数据库,说明已安装成功。
四:建配置文件
[mongodb@ser6-51 ~]$ cd /usr/local/mongodb[mongodb@ser6-51 mongodb]$ vi mongod.cnf
添加:
logpath=/usr/local/mongodb/logs logappend = true #fork and run in background fork = true #port = 27017 dbpath=/usr/local/mongodb/data #location of pidfile pidfilepath=/usr/local/mongodb/mongod.pid
除了以mongod --dbpath=/usr/local/mongodb/data --fork --logpath=/usr/local/mongodb/logs这种方式启动数据库外,还可以通过配置文件启动数据库:
之前打开数据库了,所以先关库:
可以使用db.shutdownServer()命令
> use admin switched to db admin > db.shutdownServer() 2015-06-25T15:15:57.839+0800 I NETWORK DBClientCursor::init call() failed server should be down... 2015-06-25T15:15:57.841+0800 I NETWORK trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed 2015-06-25T15:15:57.842+0800 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:104 Connection reset by peer 2015-06-25T15:15:57.842+0800 I NETWORK reconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
#通过配置文件连接数据库
[mongodb@ser6-51 mongodb]$ mongod --config /usr/local/mongodb/mongod.cnf
about to fork child process, waiting until server is ready for connections.
forked process: 2059
child process started successfully, parent exiting
五:设置开机启动
[mongodb@ser6-51 mongodb]$ su -Password:
[root@ser6-51 ~]# vi /etc/rc.local
添加:
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongod.cnf
#重启机器,检测是否生效
[root@ser6-51 ~]# reboot
--过一小会儿--
[root@ser6-51 ~]# ps -ef | grep mongod
root 1828 1 0 17:32 ? 00:00:00 /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data --fork --logpath=/usr/local/mongodb/logs
root 1900 1624 0 17:33 pts/1 00:00:00 grep mongod
看到数据库已经起来了。
六:怎样使用以service方式启动数据库
前面启动数据库的方式略显繁琐,所以想用service mongodb start方式启动数据库。默认service mongodb status会报错(由于该服务没有加入到service中):
[root@ser6-51 bin]# service mongodb status
mongodb: unrecognized service
需要进行如下配置:
[root@ser6-51 ~]#vi /etc/init.d/mongod
添加:
#!/bin/sh CONFIG=/usr/local/mongodb/mongod.cnf PROGRAM=/usr/local/mongodb/bin/mongod MONGOPID=`ps -ef | grep 'mongod --config' | grep -v grep | awk '{print $2}'` test -x $PROGRAM || exit 0 case "$1" in start) echo "Starting MongoDB Server..." $PROGRAM --config $CONFIG & ;; stop) echo "Stopping MongoDB Server..." if [ ! -z "$MONGOPID" ]; then kill -15 $MONGOPID fi ;; status) if [ -z "$MONGOPID" ]; then echo "MongoDB is not running!" else echo "MongoDB is running!("$MONGOPID")" fi ;; restart) echo "Shutting down MongoDB Server..." if [ ! -z "$MONGOPID" ]; then kill -15 $MONGOPID fi echo "Starting MongoDB..." $PROGRAM --config $CONFIG & ;; *) log_success_msg "Usage:/etc/init.d/mongod {start|stop|status|restart}" exit 1 esac exit 0
#为该文件赋予执行权限
[root@ser6-51 ~]# ls -l /etc/init.d/mongod
-rw-r--r--. 1 root root 529 Jun 25 17:33 /etc/init.d/mongod
[root@ser6-51 ~]# chmod 744 /etc/init.d/mongod
[root@ser6-51 ~]# ls -l /etc/init.d/mongod
-rwxr--r--. 1 root root 529 Jun 25 17:33 /etc/init.d/mongod
测试效果:
[root@ser6-51 mongodb]# service mongod stop
Stopping MongoDB Server
Terminated
Terminated
[root@ser6-51 mongodb]# service mongod start
Starting MongoDB Server
[root@ser6-51 mongodb]# about to fork child process, waiting until server is ready for connections.
forked process: 3632
child process started successfully, parent exiting
--开启用户认证,请参考MongoDB用户权限操作
--对应的shell安装脚本,请参考:mongodb
3.0.4 shell安装脚本
相关文章推荐
- MongoDB源码概述——内存管理和存储引擎
- MongoDB源码概述——内存管理和存储引擎
- MongoDB入门篇--增删改查
- mongodb3.1.3安全认证
- Mongodb知识及使用总结(一)
- 8天学通MongoDB——第一天 基础入门
- 利用MongoDB存储scrapy的数据
- mongodb 学习笔记 04 -- 游标、索引
- mongodb维护常用命令
- 【Monkey Run】2015博客keep doing outline
- 用mongodb存储日志
- 10-【MongoDB入门教程】 Journaling日志机制
- MongoDB 在mac上安装
- 为首次部署MongoDB做好准备:容量计划和监控
- mongodb性能优化
- MongoDB常用操作命令大全
- 转:mongodb的安装及主从复制
- MongoDB使用小结:一些常用操作分享
- 2-mongoDB命令
- MongoDB学习笔记~管道中的分组实现group+distinct