当前位置:Gxlcms > 数据库问题 > redhat7.0下mongodb主从搭配

redhat7.0下mongodb主从搭配

时间:2021-07-01 10:21:17 帮助过:5人阅读

1,首先安装mongodb,见之前博文

2,在主节点执行下面的命令

  1. mongod --dbpath=/var/lib/mongo --master --oplogSize 64 --port 27017

看到下面的信息即启动成功

  1. 2015-05-18T09:51:12.111+0800 I STORAGE [initandlisten] ** WARNING: Readahead for /var/lib/mongo is set to 4096KB
  2. 2015-05-18T09:51:12.111+0800 I STORAGE [initandlisten] ** We suggest setting it to 256KB (512 sectors) or less
  3. 2015-05-18T09:51:12.111+0800 I STORAGE [initandlisten] ** http://dochub.mongodb.org/core/readahead
  4. 2015-05-18T09:51:12.112+0800 I JOURNAL [initandlisten] journal dir=/var/lib/mongo/journal
  5. 2015-05-18T09:51:12.112+0800 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
  6. 2015-05-18T09:51:12.162+0800 I JOURNAL [durability] Durability thread started
  7. 2015-05-18T09:51:12.162+0800 I JOURNAL [journal writer] Journal writer thread started
  8. 2015-05-18T09:51:12.162+0800 I CONTROL [initandlisten] MongoDB starting : pid=29245 port=27017 dbpath=/var/lib/mongo master=1 64-bit host=localhost.localdomain
  9. 2015-05-18T09:51:12.162+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
  10. 2015-05-18T09:51:12.162+0800 I CONTROL [initandlisten]
  11. 2015-05-18T09:51:12.162+0800 I CONTROL [initandlisten]
  12. 2015-05-18T09:51:12.162+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
  13. 2015-05-18T09:51:12.162+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
  14. 2015-05-18T09:51:12.162+0800 I CONTROL [initandlisten]
  15. 2015-05-18T09:51:12.162+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
  16. 2015-05-18T09:51:12.162+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
  17. 2015-05-18T09:51:12.162+0800 I CONTROL [initandlisten]
  18. 2015-05-18T09:51:12.162+0800 I CONTROL [initandlisten] db version v3.0.2
  19. 2015-05-18T09:51:12.163+0800 I CONTROL [initandlisten] git version: 6201872043ecbbc0a4cc169b5482dcf385fc464f
  20. 2015-05-18T09:51:12.163+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
  21. 2015-05-18T09:51:12.163+0800 I CONTROL [initandlisten] build info: Linux ip-10-169-109-10 3.10.0-121.el7.x86_64 #1 SMP Tue Apr 8 10:48:19 EDT 2014 x86_64 BOOST_LIB_VERSION=1_49
  22. 2015-05-18T09:51:12.163+0800 I CONTROL [initandlisten] allocator: tcmalloc
  23. 2015-05-18T09:51:12.163+0800 I CONTROL [initandlisten] options: { master: true, net: { port: 27017 }, replication: { oplogSizeMB: 64 }, storage: { dbPath: "/var/lib/mongo" } }

3,从节点执行下面的命令即可

  1. mongod --dbpath=/var/lib/mongo --port=37017--slave


看到下面日志即启动成功

  1. 015-05-18T11:26:21.321+0800 I REPL [replslave] repl: end sync_pullOpLog syncedTo: May 18 11:26:16 55595bd8:1
  2. 2015-05-18T11:26:21.322+0800 I REPL [replslave] repl: syncing from host:192.168.1.1:27017
  3. 2015-05-18T11:26:31.322+0800 I REPL [replslave] repl: applied 1 operations
  4. 2015-05-18T11:26:31.322+0800 I REPL [replslave] repl: end sync_pullOpLog syncedTo: May 18 11:26:26 55595be2:1
  5. 2015-05-18T11:26:31.322+0800 I REPL [replslave] repl: syncing from host:192.168.1.1:27017
  6. 2015-05-18T11:26:41.322+0800 I REPL [replslave] repl: applied 1 operations
  7. 2015-05-18T11:26:41.322+0800 I REPL [replslave] repl: end sync_pullOpLog syncedTo: May 18 11:26:36 55595bec:1
  8. 2015-05-18T11:26:41.322+0800 I REPL [replslave] repl: syncing from host:192.168.1.1:27017
  9. 2015-05-18T11:26:51.322+0800 I REPL [replslave] repl: applied 1 operations
  10. 2015-05-18T11:26:51.322+0800 I REPL [replslave] repl: end sync_pullOpLog syncedTo: May 18 11:26:46 55595bf6:1
  11. 2015-05-18T11:26:51.322+0800 I REPL [replslave] repl: syncing from host:192.168.1.1:27017
  12. 2015-05-18T11:27:01.323+0800 I REPL [replslave] repl: applied 1 operations
  13. 2015-05-18T11:27:01.323+0800 I REPL [replslave] repl: end sync_pullOpLog syncedTo: May 18 11:26:56 55595c00:1
  14. 2015-05-18T11:27:01.323+0800 I REPL [replslave] repl: syncing from host:192.168.1.1:27017
  15. 2015-05-18T11:27:11.323+0800 I REPL [replslave] repl: applied 1 operations
  16. 2015-05-18T11:27:11.323+0800 I REPL [replslave] repl: end sync_pullOpLog syncedTo: May 18 11:27:06 55595c0a:1
  17. 2015-05-18T11:27:11.323+0800 I REPL [replslave] repl: syncing from host:192.168.1.1:27017
  18. 2015-05-18T11:27:21.323+0800 I REPL [replslave] repl: applied 1 operations
  19. 2015-05-18T11:27:21.323+0800 I REPL [replslave] repl: end sync_pullOpLog syncedTo: May 18 11:27:16 55595c14:1
  20. 2015-05-18T11:27:21.323+0800 I REPL [replslave] repl: syncing from host:192.168.1.1:27017
  21. 2015-05-18T11:27:31.323+0800 I REPL [replslave] repl: applied 1 operations
  22. 2015-05-18T11:27:31.323+0800 I REPL [replslave] repl: end sync_pullOpLog syncedTo: May 18 11:27:26 55595c1e:1
  23. 2015-05-18T11:27:31.323+08


4,主库新增一条记录

  1. > use test
  2. switched to db test
  3. > db.blog.save({title:"new article"})
  4. WriteResult({ "nInserted" : 1 })

5,从库进行查询

  1. > db.blog.find()
  2. { "_id" : ObjectId("55595022e26fd5794a58bdd3"), "title" : "new article" }


主从同时都有了这条记录,即配置成功


注意事项:

1,报错如下:

  1. repl: --source 192.168.1.1:27017 != 192.168.1.1:27017 from local.sources collection
原因:第一次执行从库命令的时候,错误的把source写成从库的ip,第二次修改正确的主库ip。由于从库已经在local表里存储了之前的source ip,以后在执行就会报错
解决办法:执行下面命令,删除错误的信息即可

  1. db.sources.remove({'host':'192.168.1.1:27017'})

2,从库命设置同步时机

  1. mongod --dbpath=/var/lib/mongo --port=37017--slave --source=192.168.1.1:27017 autoresync=true



add by joeyon1985


redhat7.0下mongodb主从搭配

标签:

人气教程排行