mongoDB入门系列之配置解释及错误汇总

牛恒博 DevOps视角

作者介绍

牛恒博,一线运维人,擅长为中小企业快速实现运维从无到有,再到自动化。现就职于某医疗大数据公司。

  首先对昨天《入门系列之实战》两处进行排版纠正:


mongod 2.x 配置文件解释

  • replSet=setname
  • oplogSize=1024复制集log的大小。
  • keyFile=/path/to/keyfile: 指定存储身份验证信息的密钥文件的路径。

  • lopath=/var/log/mongodb/mongod.log: log文件路径。

  • logappend=true: 以追加方式写入日志。

  • fork=true: 是否以守护进程方式运行

port=27017: 默认27017

  • dbpath=/var/lib/mongo: 数据库文件位置

  • pidfilepath=/var/run/mongodb/mongod.pid: PID文件的位置

  • bind_ip=127.0.0.1: 配置监听的端口。

  • nojournal=true: Disables write-ahead journaling。

  • cpu=true: 启用定期记录CPU利用率和 I/O 等待

  • noauth=true: 以不安全认证方式运行,默认是不认证的非安全方式

  • auth=true: 以安全认证方式运行。

  • verbose=true: 详细记录输出

  • objcheck=true: 用于开发驱动程序时验证客户端请求

  • quota=true: 启用数据库配额管理

  • diaglog=0: 设置oplog记录等级

    • 0=off (default)
    • 1=W
    • 2=R
    • 3=both
    • 7=W+some reads
  • nohints=true: 忽略查询提示

  • httpinterface=true: 禁用http界面,默认为localhost:28017

  • noscripting=true: 关闭服务器端脚本,这将极大的限制功能

  • notablescan=true: 关闭扫描表,任何查询将会是扫描失败

  • noprealloc=true: 关闭数据文件预分配

  • nssize=<size>: 为新数据库指定.ns文件的大小,单位:MB

  • directoryPerDB: true: 是否一个库一个文件夹

mongod主从配置

复制集模式官方已经不推荐,推荐使用分片,或者分片加复制集

master 服务器写入配置文件

cat >/data/app/mongodb/config/mongo.conf <<EOFlogpath=/data/app/mongodb/logs/mongod.loglogappend=truefork=trueport=27017dbpath=/data/app/mongodb/dbpidfilepath=/data/app/mongodb/mongod.pidbind_ip=192.168.56.12nojournal=trueverbose=truemaster=trueEOF

slave 服务器写入配置

cat >/data/app/mongodb/config/mongo.conf <<EOFlogpath=/data/app/mongodb/logs/mongod.loglogappend=truefork=trueport=27017dbpath=/data/app/mongodb/dbpidfilepath=/data/app/mongodb/mongod.pidbind_ip=192.168.56.13nojournal=trueverbose=trueslave = true  source = 192.168.56.12:27017 EOF

关键参数

  • master=true
  • slave=true
  • source=192.168.56.12:27017 ## 报错汇总

    报错一:第一次连接数据库时报错。

[root@localhost etc]# /application/mongodb/bin/mongoMongoDB shell version: 3.0.4connecting to: testServer has startup warnings: [initandlisten] [initandlisten] ** WARNING: Readahead for /data/db/ is set to 4096KB[initandlisten] **          We suggest setting it to 256KB (512 sectors) or less[initandlisten] **          http://dochub.mongodb.org/core/readahead[initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.[initandlisten] [initandlisten] [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.[initandlisten] **        We suggest setting it to 'never'[initandlisten] [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.[initandlisten] **        We suggest setting it to 'never'[initandlisten] > 

解决办法: 默认:

[root@localhost etc]# cat /sys/kernel/mm/transparent_hugepage/enabled [always] madvise never[root@localhost etc]# cat /sys/kernel/mm/transparent_hugepage/defrag [always] madvise never

更改为:

echo "never" > /sys/kernel/mm/transparent_hugepage/enabledecho "never" > /sys/kernel/mm/transparent_hugepage/defrag

结果:

[root@localhost etc]# cat /sys/kernel/mm/transparent_hugepage/enabled            always madvise [never][root@localhost etc]# cat /sys/kernel/mm/transparent_hugepage/defrag            always madvise [never]

报错二:插入数据是报错:

for ( var i = 1; i < 10; i++ ) db.things.save(n:7,h:i);2015-06-29T22:22:27.689+0800 E QUERY    SyntaxError: Unexpected token :

解决办法:格式错误。

for ( var i = 1; i < 10; i++ ) db.things.save( { n:7,h:i } );WriteResult({ "nInserted" : 1 })

报错三:初始化复制集报错:

> rs.initiate(config_rs1);{        "ok" : 0,        "errmsg" : "replSetInitiate quorum check failed because not all proposed set members responded affirmatively: localhost:28012 failed with not running with --replSet",        "code" : 74}> exit

解决办法:

/application/mongodb/bin/mongod --replSet rs1 --keyFile /data/key/r2 --fork --port 28012 --dbpath /data/data/r2 --logpath=/data/log/r2.log --logappend

报错四:在从节点上删除另一个从节点报错:

rs1:SECONDARY> rs.remove("localhost:28012"){        "ok" : 0,        "errmsg" : "replSetReconfig should only be run on PRIMARY, but my state is SECONDARY; use the \"force\" argument to override",        "code" : 10107}rs1:SECONDARY> 

解决办法:从节点没有权限去更改,rs的配置。

报错五:关闭一个节点在启动复制集时,不能正常启动:

[initandlisten] journal dir=/data/data/r0/journal[initandlisten] recover : no journal files present, no recovery needed [initandlisten] [initandlisten] Insufficient free space for journal files[initandlisten] Please make at least 3379MB available in /data/data/r0/journal or use --smallfilesinitandlisten] initandlisten] exception in initAndListen: 15926 Insufficient free space for journals, terminating[initandlisten] ReplicationCoordinatorImpl::shutdown() called before startReplication() finished.  Shutting down without cleaning up the replication system[initandlisten] now exiting[initandlisten] shutdown: go

解决办法:(原因是因为硬盘空间不够了)

/application/mongodb/bin/mongod --config /application/mongodb/etc/mongodb.conf.r0 --smallfiles

报错六:配置分片时,在路由端添加后端真实服务器提示权限错误:

mongos> db.runCommand({addshard:"localhost:20000"}){        "ok" : 0,        "errmsg" : "failed listing localhost:20000's databases:{ ok: 0.0, errmsg: \"not authorized on admin to execute command { listDatabases: 1 }\", code: 13 }"}

解决办法:开启真实服务器的时候不要加auth选项。

报错七:配置route来管理config服务器的时候,报错:

 error upgrading config database to v6 :: caused by :: Distributed ClockSkewed clock skew of the cluster 192.168.2.145:30000,192.168.2.149:30000,192.168.2.147:30000 is too far out of bounds to allow distributed locking.

解决办法:三台服务器的时间不一样。将服务器时间设置相同。问题解决。

报错八:配置完复制集后,登陆数据库操作,即使在主数据库操作也提示需要认证。

解决办法: 如果 MongoDB 服务器启动时使用了 --auth 或 --keyFile 参数,你就必须在进行任何操作前进行认证。 你可以在连接时进行认证。方法是在链接字符串中指定用户名密码,或者在 MongoClient::__construct() 构造函数中指定 "username" 和 "password"。

因为在配置集群时,使用了keyfile的原因,所以必须使用认证登录。 而不是和 db.auth() 命令有关系。

报错八:IP配置错误

配置--conigdb的时候ip地址不能填localhost或127.0.0.1否则添加分片时会返回如下错误信息:

{           "ok" : 0,           "errmsg" : "can't use localhost as a shard since all shards need to communicate. either use all shards and configdbs in localhost or all in actual IPs  host: 192.168.71.43:27017 isLocalHost:0"}
©著作权归作者所有:来自51CTO博客作者mob604756e75222的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. linux初始化配置
  2. JDK安装及注意事项
  3. httpd-2.4.46 (apache) 基于网络源码包安装
  4. Nginx动静分离 - 配置笔记
  5. Prometheus+Alertmanager配置邮件报警
  6. Playbook分发Nginx配置文件
  7. 实战--Playbook批量部署zabbix-agent
  8. Redis配置文件详细总结
  9. 实战--Playbook批量更改服务器主机名

随机推荐

  1. android之实现ProgressBar进度条组件
  2. Android 隐藏ActionBar
  3. Android 使用WebView控件展示SVG图
  4. 各种控件属性
  5. Android两种序列化方式详解(一):Serializabl
  6. 判断Android的WIFI与GPS状态,并引导用户前
  7. android 键盘 搜索键
  8. android 性能优化
  9. AgentWeb WebView 与 Android交互 JS调用
  10. Android如何避免输入法弹出时遮挡住按钮