1. 首页 > ITPUX技术网 > 正文

源码包安装Mongodb

源码包安装Mongodb

MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似json的bjson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是它支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。
它的特点是高性能、易部署、易使用,存储数据非常方便。主要功能特性有:
面向集合存储,易存储对象类型的数据。
模式自由。
支持动态查询。
支持完全索引,包含内部对象。
支持查询。
支持复制和故障恢复。
使用高效的二进制数据存储,包括大型对象(如视频等)。
自动处理碎片,以支持云计算层次的扩展性
支持RUBY,PYTHON,JAVA,C++,PHP等多种语言。
文件存储格式为BSON(一种JSON的扩展)
可通过网络访问
所谓“面向集合”(Collenction-Orented),意思是数据被分组存储在数据集中,被称为一个集合(Collenction)。每个 集合在数据库中都有一个唯一的标识名,并且可以包含无限数目的文档。集合的概念类似关系型数据库(RDBMS)里的表(table),不同的是它不需要定 义任何模式(schema)。
模式自由(schema-free),意味着对于存储在mongodb数据库中的文件,我们不需要知道它的任何结构定义。如果需要的话,你完全可以把不同结构的文件存储在同一个数据库里。
存储在集合中的文档,被存储为键-值对的形式。键用于唯一标识一个文档,为字符串类型,而值则可以是各中复杂的文件类型。我们称这种存储形式为BSON(Binary Serialized dOcument Format)。

一、关闭SElinux、配置防火墙
[root@localhost ~]# sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/’ /etc/selinux/config
setenforce 0 #使配置立即生效

[root@localhost ~]# vim /etc/sysconfig/iptables #允许27017端口通过防火墙
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 27017 -j ACCEPT
:wq! #保存退出

#重启防火墙使配置生效
/etc/init.d/iptables restart

MongoDB官网下载地址:http://www.mongodb.org/downloads
安装依赖包:
[root@localhost ~]# yum -y install net-snmp* cyrus*

[root@localhost ~]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.3.tgz
tar -zxvf mongodb-linux-x86_64-rhel62-3.4.3.tgz && rm -rf mongodb-linux-x86_64-rhel62-3.4.3.tgz
mv -v mongodb-linux-x86_64-rhel62-3.4.3 /usr/local/mongo
mkdir /usr/local/mongo/data
mkdir /var/run/mongodb
mkdir /var/log/mongodb
groupadd mongod && useradd mongod -r -M -g mongod -s /sbin/nologin
chown -R mongod:mongod /usr/local/mongo /var/run/mongodb /var/log/mongodb
touch /var/log/mongodb/mongod.log
echo “export PATH=\$PATH:/usr/local/mongo/bin” >> /etc/profile && source /etc/profile

配置Mongodb
[root@localhost /]# cat > /etc/mongod.conf < /tmp/mongod.txt
value=$?
[ $value -eq 0 ] && touch $lockfile && echo -e “\033[32m OK \033[0m ]”
cat /tmp/mongod.txt
return $value
fi
}
stop() {
echo -n “Stop Mongodb Server [port $port]……”
killproc -p $pidfile mongod && echo “Mongodb Server [port $port] has stopped”
[ $? -eq 0 ] && rm -rf $lockfile $pidfile
}
restart() {
stop
start
}
case “$1″ in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
reids_status
;;
*)
echo $”Usage: $0 {start|stop|restart|status}”
esac

添加mongod服务:
[root@localhost ~]# chmod +x /etc/init.d/mongod && chkconfig –add mongod
[root@localhost ~]# chkconfig mongod –list
mongodb 0:off 1:off 2:on 3:on 4:on 5:on 6:off

[root@localhost ~]# service mongod restart
[root@localhost ~]# service mongod status

查看mongod启动进程:
[root@localhost ~]# ps -elf | grep -v grep | grep -i mongod
ss -antulp | grep -v grep | grep mongod

连接mongodb
[root@localhost ~]# mongo –port 27017
> use test;
switched to db test
> use admin #进入admin数据库
> db.shutdownServer() #关闭MongoDB数据库
> exit #退出

若数据库出现不能连上,则是一个data目录下的mongod.lock文件的问题,用如下的修复的命令,
[root@localhost ~]# mongod –repair

#手动启动mongod:
[root@localhost ~]# mongod -f /etc/mongod.conf
后台方式启动
[root@localhost ~]# mongod –port 27017 –dbpath=/usr/local/mongo/data –logpath=/var/log/mongodb/mongod.log –logappend –fork

[root@localhost ~]# mongod –port 27017 –dbpath=/usr/local/mongo/data –logpath=/var/log/mongodb/mongod.log –logappend
–dbpath 数据库路径(数据文件)
–logpath 日志文件路径
–master 指定为主机器
–slave 指定为从机器
–source 指定主机器的IP地址
–pologSize 指定日志文件大小不超过64M.因为resync是非常操作量大且耗时,最好通过设置一个足够大的oplogSize来避免resync(默认的 oplog大小是空闲磁盘大小的5%)。
–logappend 日志文件末尾添加
–port 启用端口号,如果不指定则默认27017
–fork 在后台运行
–only 指定只复制哪一个数据库
–slavedelay 指从复制检测的时间间隔
–auth 是否需要验证权限登录(用户名和密码)

[root@localhost ~]# mongod -h
Options:
General options:
-h [ –help ] show this usage information
–version show version information
-f [ –config ] arg configuration file specifying additional options
-v [ –verbose ] [=arg(=v)] be more verbose (include multiple times for more verbosity e.g. -vvvvv)
–quiet quieter output #静默模式
–port arg specify port number – 27017 by default
–bind_ip arg comma separated list of ip addresses to listen on – all local ips by default
–ipv6 enable IPv6 support (disabled by default)
–maxConns arg max number of simultaneous connections – 1000000 by default
–logpath arg log file to send write to instead of stdout – has to be a file, not directory #指定日志存放目录
–syslog log to system’s syslog facility instead of file or stdout
–syslogFacility arg syslog facility used for mongodb syslog message
–logappend append to logpath instead of over-writing #指定日志是以追加还是以覆盖的方式写入日志文件
–logRotate arg set the log rotation behavior (rename|reopen)
–timeStampFormat arg Desired format for timestamps in log messages. One of ctime, iso8601-utc or iso8601-local
–pidfilepath arg full path to pidfile (if not set, no pidfile is created)
–keyFile arg private key for cluster authentication
–noauth run without security #无认证模式运行
–setParameter arg Set a configurable parameter
–httpinterface enable http interface
–transitionToAuth For rolling access control upgrade. Attempt to authenticate over outgoing connections and proceed regardless of success. Accept incoming connections with or without authentication.
–clusterAuthMode arg Authentication mode used for cluster authentication. Alternatives are (keyFile|sendKeyFile|sendX509|x509)
–nounixsocket disable listening on unix sockets
–unixSocketPrefix arg alternative directory for UNIX domain sockets (defaults to /tmp)
–filePermissions arg permissions to set on UNIX domain socket file – 0700 by default
–fork fork server process #以创建子进程的方式运行
–networkMessageCompressors arg Comma-separated list of compressors to use for network messages
–auth run with security #认证模式运行
–jsonp allow JSONP access via http (has security implications)
–rest turn on simple rest api
–slowms arg (=100) value of slow for profile and console log
–profile arg 0=off 1=slow, 2=all
–cpu periodically show cpu and iowait utilization #周期性的显示cpu和io的使用情况
–sysinfo print some diagnostic system information
–noIndexBuildRetry don’t retry any index builds that were interrupted by shutdown
–noscripting disable scripting engine #关闭脚本引擎
–notablescan do not allow table scans
–shutdown kill a running server (for init scripts)

Replication options:
–oplogSize arg size to use (in MB) for replication op log. default is 5% of disk space (i.e. large is good) #指定操作日志的大小

Master/slave options (old; use replica sets instead):
–master master mode
–slave slave mode
–source arg when slave: specify master as #当为从时,指定主的地址和端口
–only arg when slave: specify a single database to replicate #当为从时,指定需要从主复制的单一库
–slavedelay arg specify delay (in seconds) to be used when applying master ops to slave
–autoresync automatically resync if slave data is stale #自动同步从的数据

Replica set options:
–replSet arg arg is [/]
–replIndexPrefetch arg specify index prefetching behavior (if secondary) [none|_id_only|all]
–enableMajorityReadConcern enables majority readConcern

Sharding options:
–configsvr declare this is a config db of a cluster; default port 27019; default dir /data/configdb
–shardsvr declare this is a shard db of a cluster; default port 27018

Storage options:
–storageEngine arg what storage engine to use – defaults to wiredTiger if no data files present
–dbpath arg directory for datafiles – defaults to /data/db #指定数据存放目录
–directoryperdb each database will be stored in a separate directory
–noprealloc disable data file preallocation – will often hurt performance #关闭数据库文件大小预分配
–nssize arg (=16) .ns file size (in MB) for new databases #新数据库ns文件的默认大小
–quota limits each database to a certain number of files (8 default) #开始数据库配额的管理
–quotaFiles arg number of files allowed per db, implies –quota #规定每个数据库允许的文件数
–smallfiles use a smaller default file size #使用较小的默认文件大小
–syncdelay arg (=60) seconds between disk syncs (0=never, but not recommended) #系统同步刷新磁盘的时间,默认是60s
–upgrade upgrade db if needed #如果需要就更新数据库
–repair run repair on all dbs #修复所有的数据库
–repairpath arg root directory for repair files – defaults to dbpath
–journal enable journaling
–nojournal disable journaling (journaling is on by default for 64 bit)
–journalOptions arg journal diagnostic options
–journalCommitInterval arg how often to group/batch commit (ms)

WiredTiger options:
–wiredTigerCacheSizeGB arg maximum amount of memory to allocate for cache; defaults to 1/2 of physical RAM
–wiredTigerStatisticsLogDelaySecs arg (=0) seconds to wait between each write to a statistics file in the dbpath; 0 means do not log statistics
–wiredTigerJournalCompressor arg (=snappy) use a compressor for log records [none|snappy|zlib]
–wiredTigerDirectoryForIndexes Put indexes and data in different directories
–wiredTigerCollectionBlockCompressor arg (=snappy) block compression algorithm for collection data [none|snappy|zlib]
–wiredTigerIndexPrefixCompression arg (=1) use prefix compression on row-store leaf pages

#手动关闭MongoDB
[root@localhost ~]# mongo 127.0.0.1:27017/admin –eval “db.shutdownServer()”
[root@localhost ~]# mongo 127.0.0.1:27017/admin –eval “db.stats()”

[root@localhost ~]# ps -elf | grep -v grep | grep -i mongod
root 19542 1 0 10:43 ? 00:00:07 ./mongod -port 27017 –dbpath=/usr/local/mongodb/data/ –logpath=/var/log/mongodb/mongod.log –logappend –fork
[root@localhost ~]# lsof -i :27017
[root@localhost ~]# ss -antulp | grep -v grep | grep mongod

Mongodb启动命令mongod参数说明
mongod的主要参数有:
基本配置
——————————————————————————–
–quiet # 安静输出
–port arg # 指定服务端口号,默认端口27017
–bind_ip arg # 绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定默认本地所有IP
–logpath arg # 指定MongoDB日志文件,注意是指定文件不是目录
–logappend # 使用追加的方式写日志
–pidfilepath arg #PID File的完整路径,如果没有设置,则没有PID文件
–keyFile arg # 集群的私钥的完整路径,只对于Replica Set架构有效
–unixSocketPrefix arg # UNIX域套接字替代目录,(默认为 /tmp)
–fork # 以守护进程的方式运行MongoDB,创建服务器进程
–auth # 启用验证
–cpu # 定期显示CPU的CPU利用率和iowait
–dbpath arg # 指定数据库路径
–diaglog arg # diaglog选项 0=off 1=W 2=R 3=both 7=W+some reads
–directoryperdb # 设置每个数据库将被保存在一个单独的目录
–journal # 启用日志选项,MongoDB的数据操作将会写入到journal文件夹的文件里
–journalOptions arg # 启用日志诊断选项
–ipv6 # 启用IPv6选项
–jsonp # 允许JSONP形式通过HTTP访问(有安全影响)
–maxConns arg # 最大同时连接数 默认2000
–noauth # 不启用验证
–nohttpinterface # 关闭http接口,默认关闭27018端口访问
–noprealloc # 禁用数据文件预分配(往往影响性能)
–noscripting # 禁用脚本引擎
–notablescan # 不允许表扫描
–nounixsocket # 禁用Unix套接字监听
–nssize arg (=16) # 设置信数据库.ns文件大小(MB)
–objcheck # 在收到客户数据,检查的有效性,
–profile arg # 档案参数 0=off 1=slow, 2=all
–quota # 限制每个数据库的文件数,设置默认为8
–quotaFiles arg # number of files allower per db, requires –quota
–rest # 开启简单的rest API
–repair # 修复所有数据库run repair on all dbs
–repairpath arg # 修复库生成的文件的目录,默认为目录名称dbpath
–slowms arg (=100) # value of slow for profile and console log
–smallfiles # 使用较小的默认文件
–syncdelay arg (=60) # 数据写入磁盘的时间秒数(0=never,不推荐)
–sysinfo # 打印一些诊断系统信息
–upgrade # 如果需要升级数据库

* Replicaton 参数
——————————————————————————–
–fastsync # 从一个dbpath里启用从库复制服务,该dbpath的数据库是主库的快照,可用于快速启用同步
–autoresync # 如果从库与主库同步数据差得多,自动重新同步,
–oplogSize arg # 设置oplog的大小(MB)

* 主/从参数
——————————————————————————–
–master # 主库模式
–slave # 从库模式
–source arg # 从库 端口号
–only arg # 指定单一的数据库复制
–slavedelay arg # 设置从库同步主库的延迟时间

* Replica set(副本集)选项:
——————————————————————————–
–replSet arg # 设置副本集名称

* Sharding(分片)选项
——————————————————————————–
–configsvr # 声明这是一个集群的config服务,默认端口27019,默认目录/data/configdb
–shardsvr # 声明这是一个集群的分片,默认端口27018
–noMoveParanoia # 关闭偏执为moveChunk数据保存

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息