调试代码比新编写代码更困难。因此, 如果你尽自己所能写出了最复杂的代码, 你将没有更大的智慧去调试它。

Linux 安装 MongoDB ?

MongoDB 2017-06-04 浏览量: 2753 字数统计: 624 最后更新: 2017-06-10 19:54

文章目录[显示]

MongoDB这么火的数据库咱怎么能不用用呢,管他 是慢还是 吃内存呢,先装个再说
什么 你说要讲安装? 初级请进来,高手请自行忽略

话不多说,先下载 解压 ,我是本地上传的,所以就不 wget了,直接抄家伙 上解压吧


等等,俺是不是忘记介绍 MongoDB了?
哎呀,介绍过嘛,你看
https://www.tougetu.com/2016/10/mysql-mongodb-select.html
再不济咱还能看维基呢
https://zh.m.wikipedia.org/zh/MongoDB
https://en.m.wikipedia.org/wiki/MongoDB

哎?你废话怎么这么多?(蔫>)
那就开始吧

[[email protected] local]# tar -zxvf /usr/local/mongodb-linux-x86_64-amazon-3.2.6.tgz 
[[email protected] local]# cp -r /usr/local/mongodb-linux-x86_64-amazon-3.2.6 /usr/local/mongodb

[[email protected] local]# cd /usr/local/mongodb
[[email protected] mongodb]# ll
total 100
drwxr-xr-x 2 root root  4096 Jun  3 19:20 bin
-rw-rw-r-- 1  222  500 34520 Apr 28  2016 GNU-AGPL-3.0
-rw-rw-r-- 1  222  500 16726 Apr 28  2016 MPL-2
-rw-rw-r-- 1  222  500  1359 Apr 28  2016 README
-rw-rw-r-- 1  222  500 35910 Apr 28  2016 THIRD-PARTY-NOTICES

先查看一下readme,那里(可能)会有一些说明

[[email protected] mongodb]# cat README 
MongoDB README

Welcome to MongoDB!

COMPONENTS

  bin/mongod - The database process.
  bin/mongos - Sharding controller.
  bin/mongo  - The database shell (uses interactive javascript).

UTILITIES

  bin/mongodump         - MongoDB dump tool - for backups, snapshots, etc..
  bin/mongorestore      - MongoDB restore a dump
  bin/mongoexport       - Export a single collection to test (JSON, CSV)
  bin/mongoimport       - Import from JSON or CSV
  bin/mongofiles        - Utility for putting and getting files from MongoDB GridFS
  bin/mongostat         - Show performance statistics

RUNNING

  For command line options invoke:

    $ ./mongod --help

  To run a single server database:

    $ mkdir /data/db
    $ ./mongod
    $
    $ # The mongo javascript shell connects to localhost and test database by default:
    $ ./mongo 
    > help

DRIVERS

  Client drivers for most programming languages are available at mongodb.org.  Use the 
  shell ("mongo") for administrative tasks.

DOCUMENTATION

  http://www.mongodb.org/

MAIL LISTS AND IRC

  http://dochub.mongodb.org/core/community

32 BIT BUILD NOTES

  MongoDB uses memory mapped files.  If built as a 32 bit executable, you will
  not be able to work with large (multi-gigabyte) databases.  However, 32 bit
  builds work fine with small development databases.

好吧上述没有提到单独开一个log,那咱 自己个 开一个吧

[[email protected] mongodb]# mkdir data
[[email protected] mongodb]# cd data/
[[email protected] data]# mkdir db
[[email protected] data]# mkdir log
[[email protected] mongodb]# ./bin/mongod --dbpath=/usr/local/mongodb/data/db --logpath=/usr/local/mongodb/data/log/mongodb.log
2017-06-03T21:35:01.385+0800 I CONTROL  [main] log file "/usr/local/mongodb/data/log/mongodb.log" exists; moved to ..............................................

[[email protected] mongodb]# ./bin/mongo
MongoDB shell version: 3.2.6
connecting to: test
2017-06-03T21:36:19.966+0800 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2017-06-03T21:36:19.999+0800 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
[email protected]/mongo/shell/mongo.js:229:14
@(connect):1:6

exception: connect failed

哎等等 有问题!!

发现忘记加fork参数 后台Daemon 启动不了,并且用fork的时候一定要加logpath

[[email protected] mongodb]# ./bin/mongod --dbpath=/usr/local/mongodb/data/db --logpath=/usr/local/mongodb/data/log/mongodb.log --fork
about to fork child process, waiting until server is ready for connections.
forked process: 23431
child process started successfully, parent exiting



[[email protected] mongodb]# ./bin/mongo
MongoDB shell version: 3.2.6
connecting to: test
Server has startup warnings: 
2017-06-03T21:36:34.475+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-06-03T21:36:34.475+0800 I CONTROL  [initandlisten] 
2017-06-03T21:36:34.476+0800 I CONTROL  [initandlisten] 
2017-06-03T21:36:34.476+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-06-03T21:36:34.476+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-06-03T21:36:34.476+0800 I CONTROL  [initandlisten] 
2017-06-03T21:36:34.476+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-06-03T21:36:34.476+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-06-03T21:36:34.476+0800 I CONTROL  [initandlisten] 
2017-06-03T21:36:34.476+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 3888 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2017-06-03T21:36:34.476+0800 I CONTROL  [initandlisten] 
> show dbs;
WRITE_ME  0.000GB
local     0.000GB
> use local
switched to db local
> show collections;
startup_log

WC 够简单,齐活!!!


但是我老这么启mongod 是不是有些麻烦 ,玩意dbpath打错了,岂不是又看到该死的报错信息,于是咱还是用最常用的 根据 配置文件启动吧,简单大气上档次(好吧,并没有上到哪里去),开始当然还是先搞简单的配置了

Come on!

[[email protected] mongodb]# mkdir etc
[[email protected] mongodb]# cd etc/
[[email protected] etc]# vi mongodb.cnf

#!/bin/bash
dbpath=/usr/local/mongodb/data/db
logpath=/usr/local/mongodb/data/log/mongodb.log
bind_ip=127.0.0.1
port=27017
#auth=true
fork=true

logappend=true
journal=true
#jsonp=


[[email protected] mongodb]# ./bin/mongod -f etc/mongodb.cnf 
Error parsing INI config file: the options configuration file contains an invalid line '~'
try './bin/mongod --help' for more information

什么???又报错了???

俺 就说没有不踩坑的时候嘛
啥玩意儿???配置文件不知道怎么的加了个~,好吧 改正之后接着来

[[email protected] mongodb]# ./bin/mongod -f etc/mongodb.cnf 
about to fork child process, waiting until server is ready for connections.
forked process: 23814
ERROR: child process failed, exited with error number 48

啥????咋又出错了!!!48 这个错误真讨厌,恨死它

好吧,出现上述 代号 48 的错误可能有很多原因,比如说 mongodb 服务在不正常关闭的情况下,mongod 被锁,因为俺 之前 就遇到过这种情况 是前一次无故死机(因为当时用的Windows 所以只能用Xshell这种终端模拟软件来来远程连接 服务器,一不小心掉线了,莫名其妙的就挂了)造成的,解决的方法 基本上都是先杀死进程,然后再启动

# 查看mongo进程,好吧居然有一个

[[email protected] etc]# ps axu|grep mongo
root     23431  0.3  7.2 372576 73768 ?        Sl   21:36   0:01 ./bin/mongod --dbpath=/usr/local/mongodb/data/db --logpath=/usr/local/mongodb/data/log/mongodb.log --fork
root     23561  0.0  0.1 126268  1680 pts/3    S+   21:43   0:00 vi mongodb.conf
root     23569  0.0  0.0 112648   956 pts/2    S+   21:47   0:00 grep --color=auto mongo

# 差掉mongod进程
[[email protected] mongodb]# ./bin/mongod  --shutdown
There doesn't seem to be a server running with dbpath: /data/db

啥????!!!!咋又出问题了!

好吧 需要 指定db目录才能关闭mongod 进程
或者直接kill掉 mongo 进程id

[[email protected] mongodb]# ./bin/mongod --dbpath=/usr/local/mongodb/data/db --shutdown
killing process with pid: XXXX

# 查看是否关了(确实关了)
[[email protected] mongodb]# ps axu|grep mongo
root     23561  0.0  0.1 126268  1680 pts/3    S+   21:43   0:00 vi mongodb.conf
root     23667  0.0  0.0 112648   956 pts/2    S+   21:57   0:00 grep --color=auto mongo

再来!!重新配置文件 启动

[[email protected] mongodb]# ./bin/mongod -f etc/mongodb.cnf 
about to fork child process, waiting until server is ready for connections.
forked process: 23820
child process started successfully, parent exiting

[[email protected] mongodb]# ps axu|grep mongo
root     23561  0.0  0.1 126268  1680 pts/3    S+   21:43   0:00 vi mongodb.conf
root     23820  0.7  3.4 370516 34572 ?        Sl   22:17   0:00 ./bin/mongod -f etc/mongodb.cnf
root     23838  0.0  0.0 112648   952 pts/2    S+   22:18   0:00 grep --color=auto mongo

ok成功,终于好了


为了方便,咱们可以自己写一启动关闭的小脚本(确实挺小的 蛤)

[[email protected] ~]# vi mongodb_service


#!/bin/bash

config=$1
action=$2

case "$action" in 
    'start')
        /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/"$config".cnf;;
    'stop')
        /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/"$config".cnf --shutdown;;
    'restart')
        /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/"$config".cnf --shutdown
        /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/"$config".cnf;;
esac

好像有点low,要不改成函数呢?

[[email protected] ~]# vi mongodb_service


#!/bin/bash

config=$1
action=$2
start() {
    /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/"$config".cnf
}
 
stop() {
    /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/"$config".cnf --shutdown
}
 
case "$action" in
    start)
        start;;
    stop)
        stop;;
    restart)
        stop
        start;;
esac

啥玩意儿 ???你说更low了???,好吧确实是!!水平有限啊 !(大哭),算了就这样吧那

赋个755权限,然后 试试好不好用

[[email protected] ~]# chmod 755 mongodb_service 

[[email protected] ~]# ./mongodb_service2 mongodb stop
killing process with pid: 23943
[[email protected] ~]# ./mongodb_service2 mongodb start
about to fork child process, waiting until server is ready for connections.
forked process: 23966
child process started successfully, parent exiting
[[email protected] ~]# ./mongodb_service2 mongodb restart
killing process with pid: 23966
about to fork child process, waiting until server is ready for connections.
forked process: 23990
child process started successfully, parent exiting

瘟斗士7启动居然必须 mongod --dbpath "C:Program FilesMongoDBServer3.4bindatadb",不加上"" 居然会报错 多奇葩


小蜗牛 说:
Freedom is the source from which all meaning and all values spring .


文章版权归 原文作者所有丨本站默认采用CC-BY-NC-SA 4.0协议进行授权| 转载必须包含本声明,并以超链接形式注明原文作者和本文原始地址: https://www.tougetu.com/2017/06/mongodb-install.html

8 条评论

  1. ホロ
    1. 小土豆

      Mongo官方维护了一个apt源,真好?(其实MySQL也有啊)

      1. 小蜗牛

        不过 某人还是喜欢自己折腾嘛( 跑(๑•̀ㅁ•́ฅ) )???

        1. 小土豆

          那咱啥时候来build from source呀!

          1. 小蜗牛

            好呀,其实也差不多的啦

        2. 小土豆

          从源码编译要考虑好多问题呢,可以选择是否编译某些组件,还可以定义安装位置,解决各种依赖,超好玩?

    2. 小蜗牛

      带走??

  2. 小土豆

    (不知道怎么突然看到这篇 不要介意)

    其实你要是知道systemd或者supervisor的话,就更好了,这俩更方便更易用……
    这俩都可以监管服务,挂了会帮你重启,还能处理依赖,还有日志。重点是,他俩都能用“控制 命令 服务”的模式管理服务,比如说:

    systemctl start mongodb.service
    supervisorctl restart mongodb.service

    supervisorctl的交互式更方便。

    BTW,MongoDB支持fork模式启动还好,万一下次遇见一个只能以simple模式启动的,那还得考虑nohupsetsid什么的,更麻烦了。

    近期工作感想。

添加新评论

代码 Pastebin Gist 加粗 删除线 斜体 链接 签到