设为首页 收藏本站
查看: 979|回复: 0

[经验分享] MongoDB在CentOS下的安装、启动和配置

[复制链接]

尚未签到

发表于 2016-5-12 09:21:04 | 显示全部楼层 |阅读模式
  1、安装
  下载链接:http://www.mongodb.org/downloads
  
  下载安装包
  下载版本:rhel70-3.0.0
  下载链接:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.0.0.tgz
  可以在windowns下载后通过ssh工具上传到CentOS或直接用下面命令下载

[iyunv@clinton software]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.0.0.tgz
  下载完后,解压:

[iyunv@clinton software]# tar -zxvf mongodb-linux-x86_64-rhel70-3.0.0.tgz
mongodb-linux-x86_64-rhel70-3.0.0/README
mongodb-linux-x86_64-rhel70-3.0.0/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel70-3.0.0/GNU-AGPL-3.0
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongodump
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongorestore
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongoexport
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongoimport
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongostat
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongotop
mongodb-linux-x86_64-rhel70-3.0.0/bin/bsondump
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongofiles
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongooplog
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongoperf
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongod
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongos
mongodb-linux-x86_64-rhel70-3.0.0/bin/mongo
[iyunv@clinton software]#

  
  一般我们将安装的软件安装在/usr/local目录下,所以将解压后的文件移至/usr/local目录,并重命名,如下

[iyunv@clinton software]# mv mongodb-linux-x86_64-rhel70-3.0.0 /usr/local/mongodb
[iyunv@clinton software]# cd /usr/local/

  
  我们在mongodb目录下新建一个目录data用于存放数据,新建一个目录log用于存放日志,然后在该目录下新建一个日志文件mongodb.log,操作如下

[iyunv@clinton local]# cd mongodb/
[iyunv@clinton mongodb]# ll
总用量 68
drwxr-xr-x. 2 root root  4096 3月  12 15:55 bin
-rw-r--r--. 1 root root 34520 2月  28 01:43 GNU-AGPL-3.0
-rw-r--r--. 1 root root  1359 2月  28 01:43 README
-rw-r--r--. 1 root root 22660 2月  28 01:43 THIRD-PARTY-NOTICES
[iyunv@clinton mongodb]# mkdir data
[iyunv@clinton mongodb]# mkdir log
[iyunv@clinton mongodb]# cd log/
[iyunv@clinton log]# touch mongodb.log

  
  2、启动mongodb
  将目录定位到/usr/local/mongodb

[iyunv@clinton log]# cd /usr/local/mongodb

  
  使用mongod命令建立一个mongodb数据库链接,端口号设置为10001,数据库的路径为/usr/local/mongodb/data,日志路径为/usr/local/mongodb/log/mogodb.log
  注:可用  --help查看命令参数的作用

[iyunv@clinton mongodb]# ./bin/mongod --port 10001 --dbpath data/ --logpath log/mongodb.log
2015-03-12T16:18:33.489+0800 I CONTROL  log file "/usr/local/mongodb/log/mongodb.log" exists; moved to "/usr/local/mongodb/log/mongodb.log.2015-03-12T08-18-33".

  以上方式是在前台启动Mongodb进程,如果Session窗口关闭,Mongodb进程也随之停止,我们的客户端要连接需要另外开启一个终端。不过Mongodb同时还提供了一种后台Daemon方式启动,只需要加上一个"--fork"参数即可,值得注意的是,用到了"--fork"参数就必须启用"--logpath"参数。如下所示:

[iyunv@clinton mongodb]# ./bin/mongod --port 10001 --dbpath data/ --fork
BadValue --fork has to be used with --logpath or --syslog
try './bin/mongod --help' for more information
[iyunv@clinton mongodb]# ./bin/mongod --port 10001 --dbpath data/ --fork --logpath=log/mongodb.log
about to fork child process, waiting until server is ready for connections.
forked process: 6288
child process started successfully, parent exiting
[iyunv@clinton mongodb]# ps -ef|grep mongodb
root      6288     1  1 16:27 ?        00:00:00 ./bin/mongod --port 10001 --dbpath data/ --fork --logpath=log/mongodb.log
root      6304  2292  0 16:27 pts/0    00:00:00 grep --color=auto mongodb
[iyunv@clinton mongodb]#

  
  3、客户端连接
  将目录切换到mongodb目录下,并使用mongo命令来连接该数据库

[iyunv@clinton mongodb]# pwd
/usr/local/mongodb
[iyunv@clinton mongodb]# ./bin/mongo localhost:10001
MongoDB shell version: 3.0.0
connecting to: localhost:10001/test
Server has startup warnings:
2015-03-12T16:27:20.453+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-03-12T16:27:20.453+0800 I CONTROL  [initandlisten]
2015-03-12T16:27:20.453+0800 I CONTROL  [initandlisten]
2015-03-12T16:27:20.453+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-03-12T16:27:20.453+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-03-12T16:27:20.453+0800 I CONTROL  [initandlisten]
2015-03-12T16:27:20.453+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-03-12T16:27:20.453+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-03-12T16:27:20.453+0800 I CONTROL  [initandlisten]
>

  
  往数据库中插入值

> db.foo.save({a:1})
WriteResult({ "nInserted" : 1 })
>

  从数据库中查询

> db.foo.find()
{ "_id" : ObjectId("55014b84b66287928352f381"), "a" : 1 }
>
  
  通过浏览器访问
  在浏览器地址栏输入: http://localhost:10001/ 然后回车访问 (localhost可以换成具体的服务器IP)
  可以看到如下提示:It looks like you are trying to access MongoDB over HTTP on the native driver port.
  不知道什么原因,google了一下,发现在官网有如下解释:
  
  --httpinterface

New in version 2.6.



Enables the HTTP interface. Enabling the interface can increase network exposure.
Leave the HTTP interface disabled for production deployments. If you do enable this interface, you should only allow trusted clients to access this port. See Firewalls.
  
  
  应该是自2.6版本以后默认就不开启HTTP接口访问,于是重启加上--httpinterface启动

[iyunv@clinton mongodb]# ./bin/mongod --port 10001 --dbpath data/ --fork --logpath=log/mongodb.log --httpinterface
about to fork child process, waiting until server is ready for connections.
forked process: 7760
child process started successfully, parent exiting

  
  再在地址栏中输入:http://localhost:10001/ 回车
  发现仍然有上面的提示:It looks like you are trying to access MongoDB over HTTP on the native driver port.
  
  之前的版本直接访问10001端口会提示:You are trying to access MongoDB on the native driver port. For http diagnostic access, add 1000 to the port number
  
  于是把端口加上1000,http://localhost:11001/,回车后,就能够访问到Monodb的服务端web页面
  
  在CentOS中
DSC0000.png
 
  在windowns中
DSC0001.png
 
  
  4、通过配置文件来配置Mongodb
  创建mongodb的配置文件conf/mongodb.conf
  

[iyunv@clinton mongodb]# pwd
/usr/local/mongodb
[iyunv@clinton mongodb]# mkdir conf
[iyunv@clinton mongodb]# cd conf
[iyunv@clinton conf]# vim mongodb.conf

  
  在mongodb.conf中加入下面内容:
  

port=10001
dbpath=/usr/local/mongodb/data/
logpath=/usr/local/mongodb/log/mongodb.log
logappend=true
fork=true

  
  解释说明:
  port=10001【代表端口号,如果不指定则默认为 27017 】
  dbpath=data/ 【数据库路径】
  logpath=log/mongodb.log 【日志路径】
  logappend=true 【日志文件自动累加,而不是覆盖】
  fork=true 【后台执行方式启动】
  
  启动mongodb服务

[iyunv@clinton mongodb]# ./bin/mongod -f conf/mongodb.conf --httpinterface
about to fork child process, waiting until server is ready for connections.
forked process: 8421
child process started successfully, parent exiting

  
  然后访问方式和上面一样
  
  停掉服务
  关闭mongodb可以用如下方式

[iyunv@clinton mongodb]# ps -ef|grep mongodb
root      8421     1  0 17:35 ?        00:00:01 ./bin/mongod -f conf/mongodb.conf --httpinterface
root      8459  2292  0 17:38 pts/0    00:00:00 grep --color=auto mongodb
[iyunv@clinton mongodb]# kill -9 8421
[iyunv@clinton mongodb]#

  
  
  
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-215872-1-1.html 上篇帖子: centos环境下安装apache + php + svn + svnadmin 下篇帖子: linux 进程组 作业 会话
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表