mongodb集群搭建过程记录

mongodb集群搭建花费比较长的时间,在此记录下过程,方便以后使用
一 软件环境
系统:ubuntu 18.04,mongodb 社区版4.2 https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
二 准备工作
在第一台电脑上的配置:
“sudo vim /etc/mongod.conf ”(无vim请自行配置)将127.0.0.1修改为0.0.0.0,重启“sudo service mongod restart”,绑定“ip sudo mongod --bind_ip localhost,192.168.3.17”,输出的结果如下:
dow@dow-ubuntu:/etc$ sudo mongod --bind_ip localhost,192.168.3.17
2019-10-15T15:01:49.108+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-10-15T15:01:49.110+0800 I CONTROL [initandlisten] MongoDB starting : pid=2331 port=27017 dbpath=/data/db 64-bit host=dow-ubuntu
2019-10-15T15:01:49.110+0800 I CONTROL [initandlisten] db version v4.2.0
2019-10-15T15:01:49.110+0800 I CONTROL [initandlisten] git version: a4b751dcf51dd249c5865812b390cfd1c0129c30
2019-10-15T15:01:49.110+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.1.1 11 Sep 2018
2019-10-15T15:01:49.110+0800 I CONTROL [initandlisten] allocator: tcmalloc
2019-10-15T15:01:49.110+0800 I CONTROL [initandlisten] modules: none
2019-10-15T15:01:49.110+0800 I CONTROL [initandlisten] build environment:
2019-10-15T15:01:49.110+0800 I CONTROL [initandlisten] distmod: ubuntu1804
2019-10-15T15:01:49.110+0800 I CONTROL [initandlisten] distarch: x86_64
2019-10-15T15:01:49.110+0800 I CONTROL [initandlisten] target_arch: x86_64
2019-10-15T15:01:49.110+0800 I CONTROL [initandlisten] options: { net: { bindIp: "localhost,192.168.3.17" } }
2019-10-15T15:01:49.112+0800 I STORAGE [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2019-10-15T15:01:49.112+0800 I STORAGE [initandlisten]
2019-10-15T15:01:49.112+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-10-15T15:01:49.112+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-10-15T15:01:49.112+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=3370M,cache_overflow=(file_max=0M),session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress],
2019-10-15T15:01:49.599+0800 I STORAGE [initandlisten] WiredTiger message [1571122909:599292][2331:0x7fa1ea155b00], txn-recover: Recovering log 2 through 3
2019-10-15T15:01:49.648+0800 I STORAGE [initandlisten] WiredTiger message [1571122909:648302][2331:0x7fa1ea155b00], txn-recover: Recovering log 3 through 3
2019-10-15T15:01:49.690+0800 I STORAGE [initandlisten] WiredTiger message [1571122909:690912][2331:0x7fa1ea155b00], txn-recover: Main recovery loop: starting at 2/24832 to 3/256
2019-10-15T15:01:49.752+0800 I STORAGE [initandlisten] WiredTiger message [1571122909:752130][2331:0x7fa1ea155b00], txn-recover: Recovering log 2 through 3
2019-10-15T15:01:49.792+0800 I STORAGE [initandlisten] WiredTiger message [1571122909:792466][2331:0x7fa1ea155b00], txn-recover: Recovering log 3 through 3
2019-10-15T15:01:49.824+0800 I STORAGE [initandlisten] WiredTiger message [1571122909:824030][2331:0x7fa1ea155b00], txn-recover: Set global recovery timestamp: (0,0)
2019-10-15T15:01:49.850+0800 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2019-10-15T15:01:49.876+0800 I STORAGE [initandlisten] Timestamp monitor starting
2019-10-15T15:01:49.880+0800 I CONTROL [initandlisten]
2019-10-15T15:01:49.880+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-10-15T15:01:49.880+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-10-15T15:01:49.880+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-10-15T15:01:49.880+0800 I CONTROL [initandlisten]
2019-10-15T15:01:49.924+0800 I SHARDING [initandlisten] Marking collection local.system.replset as collection version:
2019-10-15T15:01:49.926+0800 I STORAGE [initandlisten] Flow Control is enabled on this deployment.
2019-10-15T15:01:49.927+0800 I SHARDING [initandlisten] Marking collection admin.system.roles as collection version:
2019-10-15T15:01:49.927+0800 I SHARDING [initandlisten] Marking collection admin.system.version as collection version:
2019-10-15T15:01:49.929+0800 I SHARDING [initandlisten] Marking collection local.startup_log as collection version:
2019-10-15T15:01:49.929+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2019-10-15T15:01:49.930+0800 I SHARDING [LogicalSessionCacheRefresh] Marking collection config.system.sessions as collection version:
2019-10-15T15:01:49.930+0800 I NETWORK [initandlisten] Listening on /tmp/mongodb-27017.sock
2019-10-15T15:01:49.930+0800 I NETWORK [initandlisten] Listening on 127.0.0.1
2019-10-15T15:01:49.930+0800 I NETWORK [initandlisten] Listening on 192.168.3.17
2019-10-15T15:01:49.930+0800 I NETWORK [initandlisten] waiting for connections on port 27017
2019-10-15T15:01:49.930+0800 I SHARDING [LogicalSessionCacheReap] Marking collection config.transactions as collection version:
2019-10-15T15:01:58.232+0800 I NETWORK [listener] connection accepted from 192.168.3.16:42538 #1 (1 connection now open)
2019-10-15T15:01:58.232+0800 I NETWORK [conn1] received client metadata from 192.168.3.16:42538 conn1: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "4.2.0" }, os: { type: "Linux", name: "Ubuntu", architecture: "x86_64", version: "18.04" } }
2019-10-15T15:02:01.302+0800 I NETWORK [conn1] end connection 192.168.3.16:42538 (0 connections now open)
^C2019-10-15T15:02:03.220+0800 I CONTROL [signalProcessingThread] got signal 2 (Interrupt), will terminate after current cmd ends
2019-10-15T15:02:03.221+0800 I NETWORK [signalProcessingThread] shutdown: going to close listening sockets...
2019-10-15T15:02:03.221+0800 I NETWORK [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2019-10-15T15:02:03.221+0800 I - [signalProcessingThread] Stopping further Flow Control ticket acquisitions.
2019-10-15T15:02:03.222+0800 I CONTROL [signalProcessingThread] Shutting down free monitoring
2019-10-15T15:02:03.222+0800 I FTDC [signalProcessingThread] Shutting down full-time diagnostic data capture
2019-10-15T15:02:03.227+0800 I STORAGE [signalProcessingThread] Deregistering all the collections
2019-10-15T15:02:03.227+0800 I STORAGE [signalProcessingThread] Timestamp monitor shutting down
2019-10-15T15:02:03.227+0800 I STORAGE [signalProcessingThread] WiredTigerKVEngine shutting down
2019-10-15T15:02:03.233+0800 I STORAGE [signalProcessingThread] Shutting down session sweeper thread
2019-10-15T15:02:03.233+0800 I STORAGE [signalProcessingThread] Finished shutting down session sweeper thread
2019-10-15T15:02:03.233+0800 I STORAGE [signalProcessingThread] Shutting down journal flusher thread
2019-10-15T15:02:03.302+0800 I STORAGE [signalProcessingThread] Finished shutting down journal flusher thread
2019-10-15T15:02:03.302+0800 I STORAGE [signalProcessingThread] Shutting down checkpoint thread
2019-10-15T15:02:03.302+0800 I STORAGE [signalProcessingThread] Finished shutting down checkpoint thread
2019-10-15T15:02:03.335+0800 I STORAGE [signalProcessingThread] shutdown: removing fs lock...
2019-10-15T15:02:03.335+0800 I CONTROL [signalProcessingThread] now exiting
2019-10-15T15:02:03.335+0800 I CONTROL [signalProcessingThread] shutting down with code:0

三 再配置复制集(service mongod 未启动时才能配置副本集成功)
sudo mongod --replSet pointeye -f mongod.conf --fork

关于fork的解释:
fork: 以守护进程的方式运行MongoDB。指定日志输出路径,而不是输出到命令行。如果对文件夹有写权限的话,系统会在文件不存在时创建它。它会将已有文件覆盖掉,清除所有原来的日志记录。如果想保留原来的日志,还需要使用--logappend选项。如:
/> mongod --logpath=D:\logdata\mylog.log
all output going to: D:\logdata\mylog.log
需要说明的是,对于上例,logdata目录必须被提前手工创建,否则mongod将执行启动失败。
(对mongod参数的解释请参见文献3)

    要注意的是,mongod服务应该要未启动时配置,否则会报错:
    “ERROR:child process failed,exited with error number 48”

    如果连sudo 都不加,就会出现:
    “ERROR:child process failed,exited with error number 1”

   这个错误的原因,可以在终端去掉--fork,就会出现错误提示。“/etc/mongo.conf”里配置的日志文件是"/var/log/mongodb/mongd.log",


实际上是一般用户无法访问系统目录下的日志文件。所以必须要加上"sudo"

四 重复二和三,以配置另外两台电脑(192.168.3.16,192.168.3.27)
五 进入一台电脑的mongodb shell ,并创建副本集配置文档 “config = {"_id":"pointeye","members":[{"_id":0,"host":"192.168.3.17:27017"},{"_id":1,"host":"192.168.3.16:27017"},{"_id":2,"host":"192.168.3.27:27017"}]}”


六 将config发送给其中一台副本集成员,使用config对象对副本集进行初始化:

   192.168.3.17会解析这个配置对象,然后向其他成员发送消息,提醒它们使用新的配置。所有成员配置完成之后,它们会自动选出一个主节点,然后就可以正常地处理读写请求了

七 测试数据读写
插入post,并读取(请注意观察primary和secondary之间的变化)

参考文献:

  1. Kristina Chodorow,《MongoDB权威指南/第二版》,人民邮电出版社
  2. https://docs.mongodb.com/manual/tutorial/deploy-replica-set/
  3. https://www.cnblogs.com/sunhan/p/3646302.html
posted @ 2019-10-15 15:36  DowTowne  阅读(1048)  评论(0编辑  收藏  举报