今天看到了MongoDB,熟悉了一下,便动手开始安装,在安装过程中遇到了一系列的问题,在此分享给大家,一来为自己做一个记录,二来方便大家遇到相同的问题时能够顺利解决。废话少说,直接进入正题吧。
本人的实验环境为CentOS6.5 ,64位系统,CentOS下,我们一般都会通过yum来安装相关软件。
目录
MongoDB v2.4.x和v2.6.x软件包对比
MongoDB v2.4.x
MongoDB v2.4.x版的软件仓库有两个包:
1)mongo-10gen-server
此包里面有最新版的mongod和mongos守护程序以及相关的配置和初始化脚本。
2)mongo-10gen
此包里面有最新版的所有MongoDB工具。这些工具方便你管理MongoDB系统。
MongoDB v2.6.x
但在MongoDB v2.6.0版的软件仓库一共有五个包:
1)mongodb-org
此包是元数据包,它可以实现自动安装下面的4个组件包。
2)mongodb-org-server
此包里面有mongod守护程序,以及相关的配置和初始化脚本。
3)mongodb-org-mongos
此包里面有mongos守护程序。
4)mongodb-org-shell
此包里面有mongo shell环境。
5)mongodb-org-tools
此包里面有以下的MongoDB工具:mongoimport、bsondump、mongodump、mongoexport、mongofiles、mongoimport、mongooplog、mongoperf、mongorestore、mongostat以及mongotop。
控制脚本:mongodb-org包里面有各种控制脚本,包括初始化脚本/etc/rc.d/init.d/mongod
使用/etc/mongod.conf配置文件来对MongoDB进行配置。
MongoDB 2.6.0版不再有mongos的控制脚本。mongos进程只用于分片的场景。你可以使用mongod初始化脚本来驱动mongos控制脚本。
准备工作
运行yum命令查看MongoDB的包信息 [root@localhost~]# yum info mongodb-org
(提示没有相关匹配的信息,) 说明你的centos系统中的yum源不包含MongoDB的相关资源,所以要在使用yum命令安装MongoDB前需要增加yum源,也就是在 /etc/yum.repos.d/目录中增加 *.repo yum源配置文件
创建mongodb.repo文件
在/etc/yum.repos.d/目录下创建文件mongodb.repo,它包含MongoDB仓库的配置信息,64位的操作系统内容如下:
[mongodb] name=MongoDB Repository baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/ gpgcheck=0 enabled=1
注意32位的操作系统配置如下:
[mongodb] name=MongoDB Repository baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686/ gpgcheck=0 enabled=1
如果你不知道自己的操作系统是多少位的,使用如下命令:
[root@h3 yum.repos.d]# getconf LONG_BIT 32
yum的更新原则应该是会将本地的repomod.xml文件的修改日期与yum服务器的更新文件repomd.xml 作比较,然后从其中选择一份最新的文件,所以我们需要运行如下命令清除一下:
[root@localhost ~]# yum clean all
做好yum源的配置后,如果配置正确执行下面的命令便可以查询MongoDB相关的信息:
查看mongoDB的服务器包的信息
[root@localhost ~]# yum info mongodb-org
关闭SElinux
vi /etc/selinux/config #SELINUX=enforcing #注释掉 #SELINUXTYPE=targeted #注释掉 SELINUX=disabled #增加 :wq! #保存退出 reboot 重启系统使配置生效 或 setenforce 0 #使配置立即生效
配置防火墙
vi /etc/sysconfig/iptables #编辑 -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT #允许27017端口通过防火墙 :wq! #保存退出 /etc/init.d/iptables restart #重启防火墙使配置生效
启动MongoDB
service mongod start
系统更新
yum update
安装MongoDB
安装MongoDB的服务器端和客户端工具
接下来,那么,接下来就是见证奇迹的时候了,在这里,我们直接yum insall mongodb-org ,在这里稍作赘述,因为mongo分为客户端和服务端,而服务端依赖于客户端,因此我们直接安装 mongodb-org就行了。
[root@localhost ~]# yum install -y mongodb-org
在启动的时候,可能会遇到以下问题
service mongod start Starting mongod: about to fork child process, waiting until server is ready for connections. forked process: 31346 all output going to: /var/log/mongo/mongod.log ERROR: child process failed, exited with error number 100
我们可以先查看日志,看发生了什么
more /var/log/mongo/mongod.log 2014-09-25T16:43:04.845+0800 [initandlisten] ERROR: Insufficient free space for journal files 2014-09-25T16:43:04.845+0800 [initandlisten] Please make at least 3379MB available in /var/lib/mongo/journal or use --smallfiles
由上可知,journal文件的存储空间不够,我们可以选择关闭journal
vim /etc/mongod.conf # Disables write-ahead journaling nojournal = true nojournal = true
然后就可以正常启动了
以后有更新了,停掉mongodb,执行yum update mongodb-org 即可。
验证MongoDB
可以通过检查日志文件/var/log/mongodb/mongod.log的内容来判断mongod进程是否正常运行。
也可以执行命令:
$ sudo chkconfig mongod on 要停止MongoDB,执行: $ sudo service mongod stop 要重启MongoDB,执行: $ sudo service mongod restart
测试MongoDB
# mongo
MongoDB shell version: 2.0.5
connecting to: test
> db.test.save( { a: 1})
> db.test.find()
{ "_id" :
ObjectId("4fb83ca7698712e93dad12a7"), "a" : 1 }
加入开机启动
# chkconfig mongod on # chkconfig --list | grep mongod mongod 0:off 1:off 2:on 3:on 4:on 5:on 6:off
mongodb说明
配置文件为:/etc/mongod.conf
初始化脚本为:/etc/rc.d/init.d/mongod
数据存储路径:/var/lib/mongo/
日志文件存储路径:/var/log/mongo/
守护进程运行用户为:mongod
以上均为默认值
服务器配置
/etc/mongod.conf # mongo.conf #where to log logpath=/var/log/mongo/mongod.log logappend=true #以追加方式写入日志 # fork and run in background fork = true #port = 27017 #端口 dbpath=/var/lib/mongo #数据库文件保存位置 directoryperdb=true # Enables periodic logging of CPU utilization and I/O wait #启用定期记录CPU利用率和 I/O 等待 #cpu = true # Turn on/off security. Off is currently the default # 是否以安全认证方式运行,默认是不认证的非安全方式 #noauth = true #auth = true # Verbose logging output. # 详细记录输出 #verbose = true # Inspect all client data for validity on receipt (useful for # developing drivers)用于开发驱动程序时的检查客户端接收数据的有效性 #objcheck = true # Enable db quota management 启用数据库配额管理,默认每个db可以有8个文件,可以用quotaFiles参数设置 #quota = true # 设置oplog记录等级 # Set oplogging level where n is # 0=off (default) # 1=W # 2=R # 3=both # 7=W+some reads #oplog = 0 # Diagnostic/debugging option 动态调试项 #nocursors = true # Ignore query hints 忽略查询提示 #nohints = true # 禁用http界面,默认为localhost:28017 # Disable the HTTP interface (Defaults to localhost:27018).这个端口号写的是错的 #nohttpinterface = true # 关闭服务器端脚本,这将极大的限制功能 # Turns off server-side scripting. This will result in greatly limited # functionality #noscripting = true # 关闭扫描表,任何查询将会是扫描失败 # Turns off table scans. Any query that would do a table scan fails. #notablescan = true # 关闭数据文件预分配 # Disable data file preallocation. #noprealloc = true # 为新数据库指定.ns文件的大小,单位:MB # Specify .ns file size for new databases. # nssize =# Accout token for Mongo monitoring server. #mms-token = # mongo监控服务器的名称 # Server name for Mongo monitoring server. #mms-name = # mongo监控服务器的ping 间隔 # Ping interval for Mongo monitoring server. #mms-interval = # Replication Options 复制选项 # in replicated mongo databases, specify here whether this is a slave or master 在复制中,指定当前是从属关系 #slave = true #source = master.example.com # Slave only: specify a single database to replicate #only = master.example.com # or #master = true #source = slave.example.com
以上是默认的配置文件中的一些参数,更多参数可以用 mongod -h 命令来查看
[root@test ~]# mongod -h
Allowed options:
General options:
-h [ --help ] show this usage information
--version show version information
-f [ --config ] arg configuration file specifying additional options 指定启动配置文件路径
-v [ --verbose ] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--quiet quieter output
--port arg specify port number 端口
--bind_ip arg comma separated list of ip addresses to listen on -
all local ips by default 绑定ip,可以多个
--maxConns arg max number of simultaneous connections 最大并发连接数
--logpath arg log file to send write to instead of stdout - has to
be a file, not directory 日志文件路径
--logappend append to logpath instead of over-writing 日志写入方式
--pidfilepath arg full path to pidfile (if not set, no pidfile is
created) pid文件路径
--keyFile arg private key for cluster authentication (only for
replica sets)集群认证私钥,仅适用于副本集
--unixSocketPrefix arg alternative directory for UNIX domain sockets
(defaults to /tmp)替代目录
--fork fork server process
--auth run with security 使用认证方式运行
--cpu periodically show cpu and iowait utilization 定期显示的CPU和IO等待利用率
--dbpath arg directory for datafiles 数据库文件路径
--diaglog arg 0=off 1=W 2=R 3=both 7=W+some reads oplog记录等级
--directoryperdb each database will be stored in a separate directory
每个数据库存储到单独目录
--journal enable journaling 记录日志,建议开启,在异常宕机时可以恢复一些数据
--journalOptions arg journal diagnostic options
--ipv6 enable IPv6 support (disabled by default)
--jsonp allow JSONP access via http (has security
implications)允许JSONP通过http访问,该方式存在安全隐患
--noauth run without security 不带安全认证的方式
--nohttpinterface disable http interface 禁用http接口
--noprealloc disable data file preallocation - will often hurt
performance 禁用数据文件的预分配,往往会损害性能
--noscripting disable scripting engine 禁用脚本引擎
--notablescan do not allow table scans 不允许表扫描
--nounixsocket disable listening on unix sockets禁止unix sockets监听
--nssize arg (=16) .ns file size (in MB) for new databases 为新数据设置.ns文件的大小
--objcheck inspect client data for validity on receipt 检查在收到客户端的数据的有效性
--profile arg 0=off 1=slow, 2=all
--quota limits each database to a certain number of files (8
default)启用数据库配额管理,默认每个db可以有8个文件,可以用quotaFiles参数设置
--quotaFiles arg number of files allower per db, requires --quota
--rest turn on simple rest api 开启rest api
--repair run repair on all dbs 修复所有数据库
--repairpath arg root directory for repair files - defaults to dbpath修复文件的根目录,默
认为dbpath指定的目录
--slowms arg (=100) value of slow for profile and console log
--smallfiles use a smaller default file size
--syncdelay arg (=60) seconds between disk syncs (0=never, but not
recommended)与硬盘同步数据的时间,默认60秒,0表示不同步到硬盘(不建议)
--sysinfo print some diagnostic system information打印一些诊断系统信息
--upgrade upgrade db if needed 如果必要,将数据库文件升级到新的格式
(< =1.0到1.1+升级时所需的)
Replication options: 复制选项
--fastsync indicate that this instance is starting from a dbpath
snapshot of the repl peer 从一个dbpath快照开始同步
--autoresync automatically resync if slave data is stale 自动同步,如果从机的数据不是新的
自动同步
--oplogSize arg size limit (in MB) for op log oplog的大小
Master/slave options: 主/从配置选项
--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 指定从主服务器上同步数据的时间间隔 单位秒
Replica set options: 副本集选项
--replSet arg arg is [/]
参数:< 名称>[< 种子主机列表>]
Sharding options: 分片设置选项
--configsvr declare this is a config db of a cluster; default port
27019; default dir /data/configdb 声明这是一个集群的配置数据库,
默认的端口是27019 默认的路径是/data/configdb
--shardsvr declare this is a shard db of a cluster; default port
27018 声明这是集群的一个分片数据库,默认端口为27018
--noMoveParanoia turn off paranoid saving of data for moveChunk. this
is on by default for now, but default will switch
关闭偏着保存大块数据。现在它是默认的,但是会变换
参考资料:
Install MongoDB on Red Hat Enterprise, CentOS, Fedora, or Amazon Linux(官方文档):http://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat-centos-or-fedora-linux/#install-the-mongodb-packages-and-associated-tools
CentOS下使用yum安装MongoDB及相关配置:http://www.itpub.net/thread-1778533-1-1.html
CentOS 6.5安装MongoDB 2.6:http://blog.csdn.net/chszs/article/details/23392487
0 条评论。