Linux下快速安装MongoDB

Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。本文介绍了如何快速安装mongodb供大家参考。

一、安装配置mongodb
Step 1: 设置系统环境及确保缺省端口27107可用
###当前环境
# cat /etc/issue
Red Hat Enterprise Linux Server release 6.5 (Santiago)

# vi /etc/selinux/config
SELINUX=disabled

Step 2: 下载安装文件
下载地址: https://www.mongodb.org/downloads. 
或者直接在命令提示符下使用curl命令下载
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz

Step 3: 解压下载的文件
# pwd
/usr/local/src
# tar -xvf mongodb-linux-x86_64-rhel62-3.0.6.gz  ###注,本文直接从网站下载,所以文件为.gz

Step 4: 复制解压文件到运行目录
# mkdir -p /var/lib/mongodb
# cp -R -n /usr/local/src/mongodb-linux-x86_64-rhel62-3.0.6/. /var/lib/mongodb/

Step 5: 设置环境变量
e.g. export PATH=<mongodb-install-directory>/bin:$PATH
# vi ~/.bash_profile 
export PATH=/var/lib/mongodb/bin:$PATH
# source ~/.bash_profile

Step 6: 创建数据目录
# mkdir -p /data/mongodata

二、启动及验证mongodb
###启动mongo
# mongod --dbpath /data/mongodata

###以下内容为启动后输出的相关信息
2015-10-28T10:03:33.100+0800 I JOURNAL  [initandlisten] journal dir=/data/mongodata/journal
2015-10-28T10:03:33.101+0800 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed
2015-10-28T10:03:33.264+0800 I JOURNAL  [initandlisten] preallocateIsFaster=true 2.18
2015-10-28T10:03:33.398+0800 I JOURNAL  [durability] Durability thread started
2015-10-28T10:03:33.398+0800 I JOURNAL  [journal writer] Journal writer thread started
2015-10-28T10:03:33.401+0800 I CONTROL  [initandlisten] MongoDB starting : pid=10191 port=27017 dbpath=/data/mongodata 64-bit host=java_2
2015-10-28T10:03:33.401+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-10-28T10:03:33.401+0800 I CONTROL  [initandlisten] 
2015-10-28T10:03:33.402+0800 I CONTROL  [initandlisten] 
2015-10-28T10:03:33.402+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always‘.
2015-10-28T10:03:33.402+0800 I CONTROL  [initandlisten] **        We suggest setting it to ‘never‘
2015-10-28T10:03:33.402+0800 I CONTROL  [initandlisten] 
2015-10-28T10:03:33.402+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always‘.
2015-10-28T10:03:33.402+0800 I CONTROL  [initandlisten] **        We suggest setting it to ‘never‘
2015-10-28T10:03:33.402+0800 I CONTROL  [initandlisten] 
2015-10-28T10:03:33.402+0800 I CONTROL  [initandlisten] db version v3.0.6
2015-10-28T10:03:33.402+0800 I CONTROL  [initandlisten] git version: 1ef45a23a4c5e3480ac919b28afcba3c615488f2
2015-10-28T10:03:33.402+0800 I CONTROL  [initandlisten] build info: Linux ip-10-67-194-123 2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST 2011 x86_64 BOOST_LIB_VERSION=1_49
2015-10-28T10:03:33.402+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2015-10-28T10:03:33.402+0800 I CONTROL  [initandlisten] options: { storage: { dbPath: "/data/mongodata" } }
2015-10-28T10:03:33.404+0800 I INDEX    [initandlisten] allocating new ns file /data/mongodata/local.ns, filling with zeroes...
2015-10-28T10:03:33.491+0800 I STORAGE  [FileAllocator] allocating new datafile /data/mongodata/local.0, filling with zeroes...
2015-10-28T10:03:33.491+0800 I STORAGE  [FileAllocator] creating directory /data/mongodata/_tmp
2015-10-28T10:03:33.497+0800 I STORAGE  [FileAllocator] done allocating datafile /data/mongodata/local.0, size: 64MB,  took 0.001 secs
2015-10-28T10:03:33.511+0800 I NETWORK  [initandlisten] waiting for connections on port 27017

###停止mongo,直接使用ctrl + c
^C2015-10-28T10:09:21.510+0800 I CONTROL  [signalProcessingThread] got signal 2 (Interrupt), will terminate after current cmd ends
2015-10-28T10:09:21.511+0800 I CONTROL  [signalProcessingThread] now exiting
2015-10-28T10:09:21.511+0800 I NETWORK  [signalProcessingThread] shutdown: going to close listening sockets...
2015-10-28T10:09:21.511+0800 I NETWORK  [signalProcessingThread] closing listening socket: 5
2015-10-28T10:09:21.511+0800 I NETWORK  [signalProcessingThread] closing listening socket: 6
2015-10-28T10:09:21.511+0800 I NETWORK  [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2015-10-28T10:09:21.511+0800 I NETWORK  [signalProcessingThread] shutdown: going to flush diaglog...
2015-10-28T10:09:21.511+0800 I NETWORK  [signalProcessingThread] shutdown: going to close sockets...
2015-10-28T10:09:21.512+0800 I STORAGE  [signalProcessingThread] shutdown: waiting for fs preallocator...
2015-10-28T10:09:21.512+0800 I STORAGE  [signalProcessingThread] shutdown: final commit...
2015-10-28T10:09:21.512+0800 I JOURNAL  [signalProcessingThread] journalCleanup...
2015-10-28T10:09:21.512+0800 I JOURNAL  [signalProcessingThread] removeJournalFiles
2015-10-28T10:09:21.515+0800 I JOURNAL  [signalProcessingThread] Terminating durability thread ...
2015-10-28T10:09:21.615+0800 I JOURNAL  [journal writer] Journal writer thread stopped
2015-10-28T10:09:21.615+0800 I JOURNAL  [durability] Durability thread stopped
2015-10-28T10:09:21.615+0800 I STORAGE  [signalProcessingThread] shutdown: closing all files...
2015-10-28T10:09:21.618+0800 I STORAGE  [signalProcessingThread] closeAllFiles() finished
2015-10-28T10:09:21.618+0800 I STORAGE  [signalProcessingThread] shutdown: removing fs lock...
2015-10-28T10:09:21.618+0800 I CONTROL  [signalProcessingThread] dbexit:  rc: 0

###修复启动过程中的两个警告,关于使用root用户启动mongo的警告先忽略
# echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
# echo "never" > /sys/kernel/mm/transparent_hugepage/defrag

###再次重启,后置于后台进程,
# mongod --dbpath /data/mongodata &

###查看启动后的进程
# ps -ef|grep mongo |grep -v grep
root     11115 27956  0 10:11 pts/2    00:00:00 mongod --dbpath /data/mongodata
# lsof -i:27017
COMMAND   PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
mongod  11115 root    5u  IPv4 50567119      0t0  TCP *:27017 (LISTEN)

###使用mongo连接到mongod
# mongo
MongoDB shell version: 3.0.6
connecting to: test
2015-10-28T10:14:30.685+0800 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:53907 #1 (1 connection now open)
Server has startup warnings: 
2015-10-28T10:11:49.217+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-10-28T10:11:49.217+0800 I CONTROL  [initandlisten] 
> help
        db.help()                    help on db methods
        db.mycoll.help()             help on collection methods
        sh.help()                    sharding helpers
        rs.help()                    replica set helpers
        help admin                   administrative help
        help connect                 connecting to a db help
        help keys                    key shortcuts
        help misc                    misc things to know
        help mr                      mapreduce

show dbs                     show database names
        show collections             show collections in current database
        show users                   show users in current database
        show profile                 show most recent system.profile entries with time >= 1ms
        show logs                    show the accessible logger names
        show log [name]              prints out the last segment of log in memory, ‘global‘ is default
        use <db_name>                set current database
        db.foo.find()                list objects in collection foo
        db.foo.find( { a : 1 } )     list objects in foo where a == 1
        it                           result of the last line evaluated; use to further iterate
        DBQuery.shellBatchSize = x   set default number of items to display on shell
        exit                         quit the mongo shell
> db.getCollection("version");
test.version
> exit
bye

三、mongodb相关工具
###在安装文件下有README,描述了常用的mongodb相关命令行工具
# more /usr/local/mongodb/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.

###获取单个命令用法
# <command> --help
# mongod --help|more
Options:

General options:
  -h [ --help ]               show this usage information
  --version                   show version information

# mongod --version
db version v3.0.6
git version: 1ef45a23a4c5e3480ac919b28afcba3c615488f2

版权声明:本文为博主原创文章,欢迎扩散,扩散请务必注明出处。

时间: 2024-10-06 20:29:47

Linux下快速安装MongoDB的相关文章

如何在Linux下快速安装MapTiler

研究背景:         日前我司接到一个项目需要用到MapTiler,分到我的任务是测试MapTiler在linux下切图速度,这里涉及到到MapTiler在Linux下安装的问题,从  MapTiler公司拿到的在linux30天试用期的是一个rpm文件,单纯的利用rpm –ivh命令安装会发现它依赖了gdal,qtwebkit 等,也就是说你需要先安装gdal,qtwebkit.当你去安装它们的时候你又会发现它们又依赖了其它包,就这样会陷入无止尽的安装,所以写这篇博客的意义是让在不熟悉l

linux下快速安装chrome

linux下安装chrome 1.按下 Ctrl + Alt + t 键盘组合键,启动终端 2.在终端中,输入以下命令: (将下载源加入到系统的源列表.命令的反馈结果如图.如果返回"地址解析错误"等信息,可以百度搜索其他提供 Chrome 下载的源,用其地址替换掉命令中的地址.)[email protected]:~$  sudo wget https://repo.fdzh.org/chrome/google-chrome.list -P /etc/apt/sources.list.

linux下快速安装和卸载open-vswitch的操作方法介绍

一. ovs 从源码编译安装: 安装依赖项: # apt install make # apt install gcc # apt install build-essential # apt install libssl-dev # apt install libcap-ng-dev # apt install python2.7 # apt install python-pip # pip install six # apt install autoconf # apt install aut

【运维】linux下快速安装nodejs

一.安装解压 cd / mkdir node cd nginx wget https://nodejs.org/dist/v12.16.1/node-v12.16.1-linux-x64.tar.xz //https://npm.taobao.org/mirrors/node/v12.16.1/node-v12.16.1-linux-x64.tar.xz tar -xf node-v12.16.1-linux-x64.tar.xz 二.设置环境变量 文件位置 /etc/profile 备份文件

Linux下PHP安装配置MongoDB数据库连接扩展

说明: 操作系统:CentOS 5.X 64位 MongoDB数据库服务器: IP地址:192.168.21.130 Web服务器: IP地址:192.168.21.127 PHP安装路径:/usr/local/php 实现目的: 安装PHP的MongoDB数据库扩展,通过PHP程序连接MongoDB数据库 具体操作: 一.安装PHP的MongoDB数据库扩展(在Web服务器192.168.21.127上操作) 下载地址:http://pecl.php.net/get/mongo-1.5.1.t

linux的ubuntu和centos下快速安装LAMP环境

在linux下安装lamp环境,下面为大家分别介绍在ubuntu和centos下面快速安装LAMP环境的方法. 首先,让我们了解一下什么是LAMP环境(一下摘自百度百科): Linux+Apache+Mysql/MariaDB+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的Web应用程序平台.随着开源潮流的蓬勃发展,开放源代码的LAMP已经与J2EE和.Net商业软件形成三足

MongoDB Linux下的安装和启动(转)

1. 下载MongoDB,此处下载的版本是:mongodb-linux-i686-1.8.1.tgz.tar. http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.8.1.tgz 相关阅读: MongoDB Linux下的安装和启动  http://www.linuxidc.com/Linux/2011-05/36309.htm MongoDB下的高级查询示例  http://www.linuxidc.com/Linux/2011-05/36

Linux下快速搭建php开发环境

php开发环境快速搭建 一.Linux下快速搭建php开发环境 1.安装XAMPP for Linux XAMPP(Apache+MySQL+PHP+PERL)是一个功能强大的建站集成软件包,使用XAMPP可快速搭建PHP开发环境.下载链接:https://www.apachefriends.org/download.html  进入下载界面选择XAMPP for Linux下载 启动终端输入命令  cd /下载xampp保存的路径.我的保存路径是:/home/coderose/下载.更改安装程

Linux下定时切割Mongodb数据库日志并删除指定天数前的日志记录(转)

文章转自:http://www.osyunwei.com/archives/8998.html 说明: 操作系统:CentOS Mongodb安装目录:/usr/local/mongodb Mongodb数据库存放目录:/home/data/mongodb/mongodb_data Mongodb日志存放目录:/home/data/mongodb/mongodb_log 实现目的: 对Mongodb数据库日志按天保存,并且只保留最近7天的日志记录. 具体操作: 使用Mongodb数据库自带的命令