Centos安装配置MongoDB数据库实例

系统:centos 5.9
1.安装前准备
这是针对64位系统yum源:

 代码如下 复制代码

echo "[MongoDB]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=0
enabled=1" | sudo tee -a /etc/yum.repos.d/mongodb.repo

这是针对32位系统yum源

 代码如下 复制代码

echo "[MongoDB]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686
gpgcheck=0
enabled=1" | sudo tee -a /etc/yum.repos.d/mongodb.repo

2.安装mongodb

 代码如下 复制代码
yum -y install mongo-10gen mongo-10gen-server

3.配置mongodb
cat /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 #数据库文件保存位置 
 
# 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 = <size> 
 
# Accout token for Mongo monitoring server. 
#mms-token = <token> 
# mongo监控服务器的名称 
# Server name for Mongo monitoring server. 
#mms-name = <server-name> 
# mongo监控服务器的ping 间隔 
# Ping interval for Mongo monitoring server. 
#mms-interval = <seconds> 
 
# 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 <server:port>从属服务器上指定主服务器地址 
  --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 <setname>[/<optionalseedhostlist>] 
                        参数:<名称>[<种子主机列表>] 
 
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 
                        关闭偏着保存大块数据。现在它是默认的,但是会变换 

5.启动mongodb

 代码如下 复制代码
service mongod start
chkconfig mongod on

如果启动显示成功后,可以在命令行输入:
mongo
测试和操作数据库.

 构造查询数据。
  

 代码如下 复制代码

  > db.test.findOne()
    {
         "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"),
         "name" : "stephen",
         "age" : 35,
         "genda" : "male",
         "email" : "stephen@hotmail.com"
    }
 
    --多条件查询。下面的示例等同于SQL语句的where name = "stephen" and age = 35
    > db.test.find({"name":"stephen","age":35})
    { "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }
 
    --返回指定的文档键值对。下面的示例将只是返回name和age键值对。
    > db.test.find({}, {"name":1,"age":1})
   { "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35 }

 

    --指定不返回的文档键值对。下面的示例将返回除name之外的所有键值对。
    > db.test.find({}, {"name":0})
    { "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }
 

2.  查询条件:
 

 代码如下 复制代码
   MongoDB提供了一组比较操作符:$lt/$lte/$gt/$gte/$ne,依次等价于</<=/>/>=/!=。
    --下面的示例返回符合条件age >= 18 && age <= 40的文档。
    > db.test.find({"age":{"$gte":18, "$lte":40}})
    { "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "stephen@hotmail.com" }
 
    --下面的示例返回条件符合name != "stephen1"
    > db.test.find({"name":{"$ne":"stephen1"}})
    { "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "stephen@hotmail.com" }
 
    --$in等同于SQL中的in,下面的示例等同于SQL中的in ("stephen","stephen1")
    > db.test.find({"name":{"$in":["stephen","stephen1"]}})
    { "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "stephen@hotmail.com" } 
 
    --和SQL不同的是,MongoDB的in list中的数据可以是不同类型。这种情况可用于不同类型的别名场景。
    > db.test.find({"name":{"$in":["stephen",123]}})
    { "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "stephen@hotmail.com" }
 
    --$nin等同于SQL中的not in,同时也是$in的取反。如:
    > db.test.find({"name":{"$nin":["stephen2","stephen1"]}})
    { "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "stephen@hotmail.com" }
 
    --$or等同于SQL中的or,$or所针对的条件被放到一个数组中,每个数组元素表示or的一个条件。
    --下面的示例等同于name = "stephen1" or age = 35
    > db.test.find({"$or": [{"name":"stephen1"}, {"age":35}]})
    { "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "stephen@hotmail.com" }
 
    --下面的示例演示了如何混合使用$or和$in。
    > db.test.find({"$or": [{"name":{"$in":["stephen","stephen1"]}}, {"age":36}]})
    { "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "stephen@hotmail.com" }
 
    --$not表示取反,等同于SQL中的not。
    > db.test.find({"name": {"$not": {"$in":["stephen2","stephen1"]}}})
    { "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "stephen@hotmail.com" }

注意:默认mongodb的数据目录对应的是/data/db下面。日志目录对于到/data/logs/mongodb.log,如果是目录结构有调整需要重新指定配置的路径

时间: 2024-09-17 03:05:20

Centos安装配置MongoDB数据库实例的相关文章

Linux下安装配置MongoDB数据库图解

说明: 操作系统:CentOS 5.X 64位 IP地址:192.168.21.130 实现目的: 安装配置MongoDB数据库 具体操作: 一.关闭SElinux.配置防火墙 1.vi /etc/selinux/config #SELINUX=enforcing #注释掉 #SELINUXTYPE=targeted #注释掉 SELINUX=disabled #增加 :wq!  #保存退出 setenforce 0 #使配置立即生效 2.vi /etc/sysconfig/iptables  

新手学Linux(四)----Linux下安装配置MongoDB数据库

一安装步骤     1下载及解压     2创建数据库文件夹与日志文件夹     3启动MongoDB 二设置MongoDB数据库     第一步     第二步     第三步     最近在学习研究linux,今天就教教大家怎么在linux上安装配置MongoDB数据库 一.安装步骤     1.下载及解压     MongoDB的下载地址:mongodb-linux-x86_64-rhel62-3.0.7     下载完成后用WinSCP复制到/usr/local/下,然后进行解压,解压命

CentOS 安装配置 Mysql 数据库。

centos 安装 Mysql 数据库.   安装mysql 命令和mysql服务器.yum install mysql mysql-server     然后设置 mysql 编码     vi /etc/my.cnf 在[mysqld]下添加 default-character-set = utf8   在末尾添加 [mysql] default-character-set = utf8   #然后开始启动mysql   service mysqld start   #设置MySQL服务随系

Linux下安装配置MongoDB 3.0版本数据库教程

说明: 操作系统:CentOS 5.X 64位 IP地址:192.168.21.128 实现目的: 安装配置MongoDB数据库 具体操作: 一.关闭SElinux.配置防火墙 1.vi /etc/selinux/config #SELINUX=enforcing #注释掉 #SELINUXTYPE=targeted #注释掉 SELINUX=disabled #增加 :wq! #保存退出 setenforce 0 #使配置立即生效 2.vi /etc/sysconfig/iptables #编

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

yii2框架 操作 mongodb 数据库实例教程

一:安装yii2  关于安装yii2我们就不??铝耍?故抢??omposer安装下载,我们可以安装basic 模版 composer global require "fxp/composer-asset-plugin:~1.0.3" composer create-project --prefer-dist yiisoft/yii2-app-basic basic 或者是advanced composer global require "fxp/composer-asset-

Lua 操作 MongoDB 数据库实例

这篇文章主要介绍了Lua 操作 MongoDB 数据库实例,本文给出了修改后的lua-mongo API和具体的操作MongoDB 数据库代码,需要的朋友可以参考下     最近有个工作是使用Nginx + Lua实现一个操作MongoDB数据库的API,主要实现其count和query功能.之前没有写过Lua,于是也就勉强着上手,在cloudwu的 lua-mongo 的基础上实现了操作MongoDB的API. cloudwu的lua-mongo驱动实现了连接Mongo,进行find和find

CentOS系统上安装配置Oracle数据库的详细教程_oracle

一.基本配置   1.硬盘剩余空间:10G以上     虽然Oracle 10g的安装文件只有800多MB,但安装后的oracle+oraInventory目录会用差不多4G空间,再加上安装时Oracle生成的临时文件(/tmp目录需要至少400MB). 2.内存1.5G以上     官方文档说512M内存也可以安装.为了避免不必要的麻烦,请把内存加多一些. 3.Swap交换区2G 二.CenOS6 上安装Oracle 10g 1.准备安装在安装Oracle之前执行以下命令: yum insta

linux是安装配置 mongodb 副本集步骤

第一步.安装 mongodb 至每台服务器 准备 3 台以上mongodb服务器,并在每台上先做以下操作 1.下载mongodb,解压并移动到 /usr/local/mongodb 目录 (若未安装wget,请先 yum install wget 或者 apt-get install wget) 可以在局域网内某一台web服务器上下载后,再去从此服务器下载,会快些. 如:wget -c http://10.0.0.123/mongodb-linux-x86_64-2.6.5.tgz wget -