前言:
现时小伙伴们对“apache24配置mongodb”大约比较注意,各位老铁们都需要了解一些“apache24配置mongodb”的相关文章。那么小编在网络上汇集了一些对于“apache24配置mongodb””的相关内容,希望看官们能喜欢,我们一起来了解一下吧!配置yum源3.2 或3.4
vi /etc/yum.repos.d/mongodb-org-3.4.repo[mongodb-org-3.4] name=MongoDB Repository baseurl= gpgcheck=1 enabled=1 gpgkey=从阿里源下载vi /etc/yum.repos.d/mongodb-org-3.2.0.repo[mongodb-org]name=MongoDB Repositorybaseurl=[MongoDB]name=MongoDB Repositorybaseurl=wget下载
wget安装
rpm -ivh mongodb-org-3.2.20-1.el7.x86_64.rpm mongodb-org-server-3.2.20-1.el7.x86_64.rpm mongodb-org-tools-3.2.20-1.el7.x86_64.rpmmongodb-org-mongos-3.2.20-1.el7.x86_64.rpm mongodb-org-shell-3.2.20-1.el7.x86_64.rpm安装前yum update
安装mongodbyum -y install mongodb-org mongodb-org-server创建文件夹和权限
mkdir -pv /app/mongodb/{logs,data}chown mongod:mongod /app/mongodb/*修改配置在vi /etc/mongod.conf
systemLog: quiet: false path: /app/mongodb/logs/mongod.log #path: /var/log/mongodb/mongod.log logAppend: false destination: fileprocessManagement: fork: true #pidFilePath: /app/mongodb/logs/mongodb.pid pidFilePath: /var/run/mongodb/mongod.pidnet: bindIp: 127.0.0.1 port: 27099 maxIncomingConnections: 65536 wireObjectCheck: true ipv6: false storage: dbPath: /app/mongodb/data indexBuildRetry: true journal: enabled: true directoryPerDB: false engine: mmapv1 syncPeriodSecs: 60 mmapv1: quota: enforced: false maxFilesPerDB: 8 smallFiles: true journal: commitIntervalMs: 100 wiredTiger: engineConfig: cacheSizeGB: 8 journalCompressor: snappy directoryForIndexes: false collectionConfig: blockCompressor: snappy indexConfig: prefixCompression: trueoperationProfiling: slowOpThresholdMs: 100 mode: offmongodb 3.2 用户权限管理配置
用户权限设置1、进入mongodb的shell : mongo2、切换数据库: use admin从3.0 版本起,默认只有 local 库,没有admin 库,需要我们自己来创建。3、添加用户,指定用户的角色和数据库:
db.createUser(
{ user: "admin",
customData:{description:"superuser"},
pwd: "admin",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
user字段,为新用户的名字;pwd字段,用户的密码;cusomData字段,为任意内容,例如可以为用户全名介绍;roles字段,指定用户的角色,可以用一个空数组给新用户设定空角色。在roles字段,可以指定内置角色和用户定义的角色。
4、查看创建的用户 : show users 或 db.system.users.find()5、启用用户权限:修改配置文件,增加配置:security: authorization: enabled重新启动mongodb
6、用户验证使用:启用用户验证后,再次登录mongo shell ,执行 show dbs 等命令会提示“没有权限”。此时,需要用户验证登录。db.auth("admin","admin")
###创建普通用户use foo;db.createUser({user: 'foo', pwd: 'bar', roles: [{role: 'readWrite', db: 'foo'}]})db.auth('foo', 'bar')
其他内建的角色数据库用户角色:read、readWrite;数据库管理角色:dbAdmin、dbOwner、userAdmin;集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;备份恢复角色:backup、restore;所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase超级用户角色:root// 这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、userAdminAnyDatabase)内部角色:__system配置文件示例
#此处为配置文件可配置的内容#Mongod config file #MongoDB configuration files use the YAML format.#The following example configuration file contains several mongod settings.#########Example Start#########systemLog:# destination: file# path: "/var/log/mongodb/mongodb.log"# logAppend: true#storage:# journal:# enabled: true#processManagement:# fork: true#net:# bindIp: 127.0.0.1# port: 27017#setParameter:# enableLocalhostAuthBypass: false#########Example End#################Core OptionssystemLog:# verbosity: 0 #Default: 0; 1 to 5 increases the verbosity level to include Debug messages.# quiet: <boolean># traceAllException: <boolean># syslogFacility: user path: "/usr/local/mongodb/log/mongod.log" logAppend: true# logRotate: <string> #rename or reopen destination: file# timeStampFormat: iso8601-local# component:# accessControl:# verbosity: 0# command:# verbosity: 0# # COMMENT additional component verbosity settings omitted for brevity# storage:# verbosity: 0# journal:# verbosity: <int># write:# verbosity: 0##########ProcessManagement OptionsprocessManagement: fork: true pidFilePath: "/usr/local/mongodb/log/mongod.pid"###########Net Optionsnet: port: 27017# bindIp: <string> #Default All interfaces.# maxIncomingConnections: 65536# wireObjectCheck: true# ipv6: false# unixDomainSocket:# enabled: true# pathPrefix: "/tmp"# filePermissions: 0700# http:# enabled: false# JSONPEnabled: false# RESTInterfaceEnabled: false# ssl:# sslOnNormalPorts: <boolean> # deprecated since 2.6# mode: <string># PEMKeyFile: <string># PEMKeyPassword: <string># clusterFile: <string># clusterPassword: <string># CAFile: <string># CRLFile: <string># allowConnectionsWithoutCertificates: <boolean># allowInvalidCertificates: <boolean># allowInvalidHostnames: false# FIPSMode: <boolean>##########security Options#security:# keyFile: <string># clusterAuthMode: keyFile# authorization: disable# javascriptEnabled: true########security.sasl Options# sasl:# hostName: <string># serviceName: <string># saslauthdSocketPath: <string>###########setParameter OptionsetParameter: enableLocalhostAuthBypass: false# <parameter1>: <value1># <parameter2>: <value2>###########storage Optionsstorage: dbPath: "/data/db"# indexBuildRetry: true# repairPath: "/data/db/_tmp"# journal:# enabled: true# directoryPerDB: false# syncPeriodSecs: 60 engine: "mmapv1" #Valid options include mmapv1 and wiredTiger.#########storage.mmapv1 Options# mmapv1:# preallocDataFiles: true# nsSize: 16# quota:# enforced: false# maxFilesPerDB: 8# smallFiles: false# journal:# debugFlags: <int># commitIntervalMs: 100 # 100 or 30#########storage.wiredTiger Options# wiredTiger:# engineConfig:# cacheSizeGB: <number> #Default: the maximum of half of physical RAM or 1 gigabyte# statisticsLogDelaySecs: 0# journalCompressor: "snappy"# directoryForIndexes: false# collectionConfig:# blockCompressor: "snappy"# indexConfig:# prefixCompression: true############operationProfiling Options#operationProfiling:# slowOpThresholdMs: 100# mode: "off"############replication Options#replication:# oplogSizeMB: <int># replSetName: <string># secondaryIndexPrefetch: all############sharding Options#sharding:# clusterRole: <string> #configsvr or shardsvr# archiveMovedChunks: True###########auditLog Options#auditLog:# destination: <string> #syslog/console/file# format: <string> #JSON/BSON# path: <string># filter: <string>###########snmp Options#snmp:# subagent: <boolean># master: <boolean>##########mongos-only Options#replication:# localPingThresholdMs: 15##sharding:# autoSplit: true# configDB: <string># chunkSize: 64##########Windows Service Options#processManagement:# windowsService:# serviceName: <string># displayName: <string># description: <string># serviceUser: <string># servicePassword: <string>修改启动服务
[Unit]Description=High-performance, schema-free document-oriented databaseAfter=network.targetDocumentation=[Service]User=mongodGroup=mongodEnvironment="OPTIONS=-f /etc/mongod.conf"ExecStart=/usr/bin/mongod $OPTIONSExecStartPre=/usr/bin/mkdir -p /var/run/mongodbExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodbExecStartPre=/usr/bin/chmod 0755 /var/run/mongodbPermissionsStartOnly=true#PIDFile=/app/mongodb/logs/mongod.pidPIDFile=/var/run/mongodb/mongod.pidType=forking# file sizeLimitFSIZE=infinity# cpu timeLimitCPU=infinity# virtual memory sizeLimitAS=infinity# open filesLimitNOFILE=64000# processes/threadsLimitNPROC=64000# locked memoryLimitMEMLOCK=infinity# total threads (user+kernel)TasksMax=infinityTasksAccounting=false# Recommended limits for for mongod as specified in# [Install]WantedBy=multi-user.target建立链接
ln -s /usr/lib/systemd/system/mongod.service /etc/systemd/system/multi-user.target.wants/重新加载systemctl
systemctl daemon-reload启动服务
sudo chkconfig mongod on sudo service mongod start sudo service mongod stopsudo service mongod restart netstat -tunpl | grep 27017使用说明
###连接mongodbmongo查看数据show dbs切换数据库,没有创建use new查看collectionshow collectionsshow tables查看当前数据库状态db.stats()插入数据db.user.insert({"username":"test1","age":12,"sex":"m"})查找数据,查找集合中所有数据 db.user.find()指定条件查找数据db.user.find({age: {$gt: 10}});db.user.find({sex: /m/});统计数据db.user.count()指定条件统计数据db.user.count({sex: "m"});删除记录db.user.remove({age: 12});删除collectiondb.user.drop()复制数据库db.copyDatabase("new2", "new3", "127.0.0.1"); 删除当前数据库 db.dropDatabase();释放空间db.repairDatabase()停止mongodbuse admindb.shutdownServer()帮助文件helpdb.help()备份数据库
mongodump -h 127.0.0.1:27017 -o /data/tmp/mongodb/还原数据库
mongorestore -h 127.0.0.1:27017 -d 数据库 /app/mongodb/tmpdata/还原数据库 --drop同步数据实例mongosync
mongosync 用户手册 1.6.0简介mongosync 是 mongodb 数据同步工具,目前已支持绝大多种数据同步。许可证According to MongoDB tools licenses.We use apache license right now :举例source: 10.0.4.91:27017target: 10.0.4.91:27020全量同步mongosync -h 10.0.4.91:27017 -u admin -p 123 --to 10.0.4.91:27020 --tu admin --tp456增量同步mongosync -h 10.0.4.91:27017 -u admin -p 123 --to 10.0.4.91:27020 --tu admin --tp456 --oplog -s 1369406664,1全量和增量同步,实时同步mongosync -h 10.0.4.91:27017 -u admin -p 123 --to 10.0.4.91:27020 --tu admin --tp456 --oplog 按时间段同步mongosync -h 10.0.4.91:27017 -u admin -p卸载mongodb
停止服务sudo service mongod stop移除包sudo yum erase $(rpm -qa | grep mongodb-org)移除数据和日志文件sudo rm -r /var/log/mongodb sudo rm -r /var/lib/mongo相关说明
processManagement.fork: true 以daemon模式启动mongodnet.bindIp: 指定IP,逗号分隔,注释掉的情况下,服务启动在0.0.0.0systemLog.quiet: true 日志输出使用quiet模式,只输出criticalstorage.dbPath: 指定数据的存放位置replication.replSetName: 副本集的名字security.authorization: enabled 使用认证security.keyFile: 使用keyFileverbosity: 日志开启debug模式,0-5operationProfiling.slowOpThresholdMs: slowlog设定,默认100net.http.RESTInterfaceEnabled: rest接口是否有效net.maxIncomingConnections: 最大连接数,默认65536,不能超过系统设置,# ulimit -n环境
操作系统: Centos 7
mongodb备份shell脚本
#!/bin/sh# dump 命令执行路径,根据mongodb安装路径而定DUMP=/usr/bin/mongodump# 临时备份路径OUT_DIR=/data/backup/mongod_bak/mongod_bak_now# 压缩后的备份存放路径TAR_DIR=/app/backup/mongod_bak/mongod_bak_list# 当前系统时间DATE=`date +%Y-%m-%d`# 数据库账号#DB_USER=admin# 数据库密码#DB_PASS=Dinw^0#(77E)wt[V# 代表删除7天前的备份,即只保留近 7 天的备份DAYS=7# 最终保存的数据库备份文件TAR_BAK="mongod_bak_$DATE.tar.gz"cd $OUT_DIRrm -rf $OUT_DIR/*mkdir -p $OUT_DIR/$DATE$DUMP -h 127.0.0.1:27017 -d 数据库 -o $OUT_DIR/$DATE# 压缩格式为 .tar.gz 格式tar -zcvf $TAR_DIR/$TAR_BAK $OUT_DIR/$DATE# 删除 15 天前的备份文件find $TAR_DIR/ -mtime +$DAYS -deleteexit二、创建对应的备份目录
mkdir -p /appapp/backup/mongodb_bak/mongodb_bak_nowmkdir -p /app/backup/mongodb_bak/mongodb_bak_list三、修改文件属性,使其可执行
chmod +x MongoDB_bak.sh四、添加到计划任务
cron服务是Linux的内置服务,但它不会开机自动启动。可以用以下命令启动和停止服务:
/sbin/service crond start/sbin/service crond stop/sbin/service crond restart/sbin/service crond reload
以上1-4行分别为启动、停止、重启服务和重新加载配置。
要把cron设为在开机的时候自动启动,在 /etc/rc.d/rc.local 脚本中加入 /sbin/service crond start 即可
查看当前用户的crontab,输入 crontab -l;
编辑crontab,输入 crontab -e;
删除crontab,输入 crontab -r
1 进入编辑界面
crontab -e
2 添加任务
30 18 * * * /home/crontab/mongobk.sh
基本格式 :
* command分 时 日 月 周 命令第1列表示分钟1~59 每分钟用或者 /1表示第2列表示小时1~23(0表示0点)第3列表示日期1~31第4列表示月份1~12第5列标识号星期0~6(0表示星期天)
3 保存后退出,启动服务
service crond start
4 设置开机自启动
chkconfig crond on
标签: #apache24配置mongodb