草庐IT

node.js - 连接到副本集时的“MongoError: no mongos proxy available”

coder 2023-11-03 原文

我正在遵循本教程(https://github.com/drginm/docker-boilerplates/tree/master/mongodb-replicaset),以便获得三个实例的mongodb副本集,以在docker-compose中工作。

这是我到目前为止已采取的步骤:

1)我已经将setupmongo-rs0-1文件夹复制到了我的根目录中。

2)我已经将三个mongo实例和安装实例添加到我的docker-compose文件中。现在看起来像这样:

version: '3'

services:
  mongo-rs0-1:
    image: "mongo-start"
    build: ./mongo-rs0-1
    ports:
      - "27017:27017"
    volumes:
      - ./mongo-rs0-1/data:/data/db
    networks:
      - app-network
    depends_on:
      - "mongo-rs0-2"
      - "mongo-rs0-3"
  mongo-rs0-2:
    image: "mongo"
    command: --replSet rs0 --smallfiles --oplogSize 128
    networks:
      - app-network
    ports:
      - "27018:27017"
    volumes:
      - ./mongo-rs0-2/data:/data/db
  mongo-rs0-3:
    image: "mongo"
    command: --replSet rs0 --smallfiles --oplogSize 128
    networks:
      - app-network
    ports:
      - "27019:27017"
    volumes:
      - ./mongo-rs0-3/data:/data/db
  setup-rs:
    image: "setup-rs"
    build: ./setup
    networks:
      - app-network
    depends_on:
      - "mongo-rs0-1"
  nodejs:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:8000"
    container_name: nodejs
    restart: unless-stopped
    networks:
      - app-network
    depends_on:
      - setup-rs
  nextjs:
    build:
      context: ../.
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    container_name: nextjs
    restart: unless-stopped
    networks:
      - app-network
    depends_on:
      - nodejs
  webserver:
    image: nginx:mainline-alpine
    container_name: webserver
    restart: unless-stopped
    ports:
      - "80:80"
    volumes:
      - ./picFolder:/picFolder
      - web-root:/var/www/html
      - ./nginx-conf:/etc/nginx/conf.d
      - certbot-etc:/etc/letsencrypt
      - certbot-var:/var/lib/letsencrypt
    depends_on:
      - nodejs
      - nextjs
      - setup-rs
    networks:
      - app-network

volumes:
  certbot-etc:
  certbot-var:
  web-root:
    driver: local
    driver_opts:
      type: none
      device: /
      o: bind

networks:
  app-network:
    driver: bridge  

3)这不需要修改我的nginx.conf文件,但是我已经在这里添加了它来简化:
server {
    listen 80;
    listen [::]:80;

    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com localhost;

    location /socket.io {
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "Upgrade";
      proxy_set_header Host $host;
      proxy_pass http://nodejs:8000/socket.io/;
    }

    location /back { 
      proxy_connect_timeout 75s;
      proxy_read_timeout 75s;
      proxy_send_timeout 75s;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_cache_bypass $http_upgrade;
      proxy_pass http://nodejs:8000/back/;
    }

    location /staticBack{
      alias /picFolder;
      expires 1y;
      access_log off;
      add_header Cache-Control "public";
    }

    location / {
      proxy_pass http://nextjs:3000;
    }

    location ~ /.well-known/acme-challenge {
      allow all;
      root /var/www/html;
    }
}

4)最后,我将连接字符串更改为'mongodb://mongo-rs0-1,mongo-rs0-2,mongo-rs0-3/test',如下所示(https://github.com/drginm/docker-boilerplates/blob/master/mongodb-replicaset/web-site/database.js)

这一切似乎都是正确的,但是当我尝试连接到Mongoose 时,我的nodejs抛出以下错误
mongoose.connect("mongodb://mongo-rs0-1,mongo-rs0-2,mongo-rs0-3/test");
MongoDB connection error: { MongoError: no mongos proxy available
    at Timeout.<anonymous> (/var/www/back/node_modules/mongodb-core/lib/topologies/mongos.js:757:28)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5) name: 'MongoError', [Symbol(mongoErrorContextSymbol)]: {} }

有人看到发生了什么吗?我曾尝试搜索此问题,但似乎通用的答案是mongodb某种程度上并未“看到”实例。我有点迷茫,任何帮助将不胜感激。

编辑:

经过进一步的挖掘后,我发现了该SO(Mongoose connection to replica set),并发现mongo.conf文件(https://github.com/drginm/docker-boilerplates/blob/master/mongodb-replicaset/mongo-rs0-1/mongo.conf)似乎表示副本集名称为rs0。所以现在我连接:
mongoose.connect("mongodb://mongo-rs0-1,mongo-rs0-2,mongo-rs0-3/test?replicaSet=rs0");
但是,我仍然收到以下错误(至少有所不同!):
MongoDB connection error: { MongoError: no primary found in replicaset or invalid replica set name
    at /var/www/back/node_modules/mongodb-core/lib/topologies/replset.js:636:11
    at Server.<anonymous> (/var/www/back/node_modules/mongodb-core/lib/topologies/replset.js:357:9)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Server.emit (events.js:211:7)
    at /var/www/back/node_modules/mongodb-core/lib/topologies/server.js:508:16
    at /var/www/back/node_modules/mongodb-core/lib/connection/pool.js:532:18
    at _combinedTickCallback (internal/process/next_tick.js:132:7)
    at process._tickCallback (internal/process/next_tick.js:181:9) name: 'MongoError', [Symbol(mongoErrorContextSymbol)]: {} }

我很确定副本集名称现在正确,但是我想也许我必须指定哪个是主要的?但是,此SO(https://dba.stackexchange.com/questions/136621/how-to-set-a-mongodb-node-to-return-as-the-primary-of-a-replication-set)似乎暗示这应该自动发生。任何帮助仍然赞赏。

编辑编辑:

进一步的研究发现了此SO帖子(Mongoose connection to replica set)。考虑到较新的v5 mongodb,以下选项connect现在似乎是最好的选择:
var options = {
  native_parser: true,
  auto_reconnect: false,
  poolSize: 10,
  connectWithNoPrimary: true,
  sslValidate: false,
  socketOptions: {
      keepAlive: 1000,
      connectTimeoutMS: 30000
  }
};

mongoose.connect("mongodb://mongo-rs0-1:27017,mongo-rs0-2:27017,mongo-rs0-3:27017/test?replicaSet=rs0", options);
connectWithNoPrimary: true似乎特别重要,因为nodejs处于竞争状态,而mongo服务从Docker启动时,它们可能尚未选择主要对象。

但是,我仍然收到以下错误(再次,稍有不同):
MongoDB connection error: { MongoError: no secondary found in replicaset or invalid replica set name
    at /var/www/back/node_modules/mongodb-core/lib/topologies/replset.js:649:11
    at Server.<anonymous> (/var/www/back/node_modules/mongodb-core/lib/topologies/replset.js:357:9)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Server.emit (events.js:211:7)
    at /var/www/back/node_modules/mongodb-core/lib/topologies/server.js:508:16
    at /var/www/back/node_modules/mongodb-core/lib/connection/pool.js:532:18
    at _combinedTickCallback (internal/process/next_tick.js:132:7)
    at process._tickCallback (internal/process/next_tick.js:181:9) name: 'MongoError', [Symbol(mongoErrorContextSymbol)]: {} }

因此,现在找不到辅助副本集。添加connectwithnosecondary不会执行任何操作,并且会导致相同的错误-我认为这不是有效的选择。仍然停留,任何帮助将不胜感激。

编辑编辑编辑:

我已将我的nodejs connect函数更改为存在错误的回归回调中。我的希望是,这将继续尝试连接,直到docker-compose中的任何潜在竞争条件解决并且我可以成功连接为止。但是,我不断收到上述错误MongoError: no secondary found in replicaset or invalid replica set name。因此,我不再认为问题是连接上的竞争条件-至少这似乎不是当前的错误。
  var options = {
    native_parser: true,
    auto_reconnect: false,
    poolSize: 10,
    connectWithNoPrimary: true,
    sslValidate: false
  };

  // mongoose.connect("mongodb://mongo-rs0-1,mongo-rs0-2,mongo-rs0-3/?replicaSet=rs0", {  useNewUrlParser: true, connectWithNoPrimary: true });
  const connectFunc = () => {
    mongoose.connect("mongodb://mongo-rs0-1:27017,mongo-rs0-2:27017,mongo-rs0-3:27017/test?replicaSet=rs0", options);
    mongoose.Promise = global.Promise;
    var db = mongoose.connection;
    db.on('error', (error)=>{
      console.log('MongoDB connection error:', error)
      console.log('now calling connectFunc() again');
      connectFunc()
    });
    db.once('open', function() {
      // we're connected!
      console.log('connected to mongoose db')
    });

  }

  connectFunc()

编辑x4:

这是另一个我遇到相同错误的示例教程:https://gist.github.com/patientplatypus/e48e6efdcc9f0f1aa551cc8342d0f2f3。我现在有一半人相信这可能是automattic / mongoose的错误,因此我打开了一个错误报告,可以在这里查看:https://github.com/Automattic/mongoose/issues/7705。这可能与2016年提交的这个长期存在的问题有关:https://github.com/Automattic/mongoose/issues/4596

编辑x5:

这是mongo容器之一的日志输出:
patientplatypus:~/Documents/patientplatypus.com/forum/back:19:47:11$docker ps
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                      NAMES
d46cfb5e1927        nginx:mainline-alpine   "nginx -g 'daemon of…"   3 minutes ago       Up 3 minutes        0.0.0.0:80->80/tcp         webserver
6798fe1f6093        back_nextjs             "npm start"              3 minutes ago       Up 3 minutes        0.0.0.0:3000->3000/tcp     nextjs
ab6888f703c7        back_nodejs             "/docker-entrypoint.…"   3 minutes ago       Up 3 minutes        0.0.0.0:8000->8000/tcp     nodejs
48131a82b34e        mongo-start             "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes        0.0.0.0:27017->27017/tcp   mongo1
312772b1b0f1        mongo                   "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes        0.0.0.0:27019->27017/tcp   mongo3
9fe9a16eb20e        mongo                   "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes        0.0.0.0:27018->27017/tcp   mongo2
patientplatypus:~/Documents/patientplatypus.com/forum/back:19:48:55$docker logs 9fe9a16eb20e
2019-04-12T00:45:29.689+0000 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-04-12T00:45:29.727+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=9fe9a16eb20e
2019-04-12T00:45:29.728+0000 I CONTROL  [initandlisten] db version v4.0.8
2019-04-12T00:45:29.728+0000 I CONTROL  [initandlisten] git version: 9b00696ed75f65e1ebc8d635593bed79b290cfbb
2019-04-12T00:45:29.728+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2019-04-12T00:45:29.728+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2019-04-12T00:45:29.728+0000 I CONTROL  [initandlisten] modules: none
2019-04-12T00:45:29.729+0000 I CONTROL  [initandlisten] build environment:
2019-04-12T00:45:29.729+0000 I CONTROL  [initandlisten]     distmod: ubuntu1604
2019-04-12T00:45:29.729+0000 I CONTROL  [initandlisten]     distarch: x86_64
2019-04-12T00:45:29.729+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2019-04-12T00:45:29.729+0000 I CONTROL  [initandlisten] options: { net: { bindIpAll: true }, replication: { oplogSizeMB: 128, replSet: "rs" }, storage: { mmapv1: { smallFiles: true } } }
2019-04-12T00:45:29.734+0000 W STORAGE  [initandlisten] Detected unclean shutdown - /data/db/mongod.lock is not empty.
2019-04-12T00:45:29.738+0000 I STORAGE  [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2019-04-12T00:45:29.741+0000 W STORAGE  [initandlisten] Recovering data from the last clean checkpoint.
2019-04-12T00:45:29.742+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1461M,session_max=20000,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),
2019-04-12T00:45:43.165+0000 I STORAGE  [initandlisten] WiredTiger message [1555029943:165420][1:0x7f7051ca9a40], txn-recover: Main recovery loop: starting at 7/4608 to 8/256
2019-04-12T00:45:43.214+0000 I STORAGE  [initandlisten] WiredTiger message [1555029943:214706][1:0x7f7051ca9a40], txn-recover: Recovering log 7 through 8
2019-04-12T00:45:43.787+0000 I STORAGE  [initandlisten] WiredTiger message [1555029943:787329][1:0x7f7051ca9a40], txn-recover: Recovering log 8 through 8
2019-04-12T00:45:43.849+0000 I STORAGE  [initandlisten] WiredTiger message [1555029943:849811][1:0x7f7051ca9a40], txn-recover: Set global recovery timestamp: 0
2019-04-12T00:45:43.892+0000 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2019-04-12T00:45:43.972+0000 W STORAGE  [initandlisten] Detected configuration for non-active storage engine mmapv1 when current storage engine is wiredTiger
2019-04-12T00:45:43.972+0000 I CONTROL  [initandlisten] 
2019-04-12T00:45:43.972+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-04-12T00:45:43.972+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-04-12T00:45:43.973+0000 I CONTROL  [initandlisten] 
2019-04-12T00:45:44.035+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2019-04-12T00:45:44.054+0000 I REPL     [initandlisten] Did not find local voted for document at startup.
2019-04-12T00:45:44.064+0000 I REPL     [initandlisten] Rollback ID is 1
2019-04-12T00:45:44.064+0000 I REPL     [initandlisten] Did not find local replica set configuration document at startup;  NoMatchingDocument: Did not find replica set configuration document in local.system.replset
2019-04-12T00:45:44.065+0000 I CONTROL  [LogicalSessionCacheRefresh] Sessions collection is not set up; waiting until next sessions refresh interval: Replication has not yet been configured
2019-04-12T00:45:44.065+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
2019-04-12T00:45:44.069+0000 I CONTROL  [LogicalSessionCacheReap] Sessions collection is not set up; waiting until next sessions reap interval: config.system.sessions does not exist
2019-04-12T00:45:45.080+0000 I FTDC     [ftdc] Unclean full-time diagnostic data capture shutdown detected, found interim file, some metrics may have been lost. OK

特别是这条线
Did not find local replica set configuration document at startup; NoMatchingDocument: Did not find replica set configuration document in local.system.replset
看起来令人担忧,我将不得不继续研究它。如果有人看到任何东西,请告诉我。

编辑x6

所以我将docker exec进入mongo副本之一并输出rs.status()。
我先使用docker exec -it mongo1 mongo,然后再使用rs.status(),得到以下输出:
{
    "operationTime" : Timestamp(0, 0),
    "ok" : 0,
    "errmsg" : "no replset config has been received",
    "code" : 94,
    "codeName" : "NotYetInitialized",
    "$clusterTime" : {
        "clusterTime" : Timestamp(0, 0),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

这似乎与上面的mongodb复制日志(2019-04-12T00:45:44.064+0000 I REPL [initandlisten] Did not find local replica set configuration document at startup; NoMatchingDocument: Did not find replica set configuration document in local.system.replset)中的错误非常相似。有人知道它认为缺失了什么吗?

最佳答案

您需要初始化副本集才能访问它。否则,应用程序将无法连接到本地实例。

理想情况下,您需要在副本集配置(setup-rs步骤)和下一个副本集配置之间增加一些时间,因为副本集配置所花费的时间可能比应用程序启动时间更长。

如果脚本本身有故障,请修复该脚本。

关于node.js - 连接到副本集时的“MongoError: no mongos proxy available”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55637808/

有关node.js - 连接到副本集时的“MongoError: no mongos proxy available”的更多相关文章

  1. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  2. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

  3. ruby-on-rails - 如何从过时的 TZInfo 标识符中获取 Rails TimeZone 名称? - 2

    已经有一个问题回答了如何将“America/Los_Angeles”转换为“PacificTime(US&Canada)”。但是我想将“美国/太平洋”和其他过时的时区转换为RailsTimeZone。我无法在图书馆中找到任何可以帮助我完成此任务的东西。 最佳答案 来自RailsActiveSupport::TimeZonedocs:TheversionofTZInfobundledwithActiveSupportonlyincludesthedefinitionsnecessarytosupportthezonesdefinedb

  4. ruby - 我的 Ruby IRC 机器人没有连接到 IRC 服务器。我究竟做错了什么? - 2

    require"socket"server="irc.rizon.net"port="6667"nick="RubyIRCBot"channel="#0x40"s=TCPSocket.open(server,port)s.print("USERTesting",0)s.print("NICK#{nick}",0)s.print("JOIN#{channel}",0)这个IRC机器人没有连接到IRC服务器,我做错了什么? 最佳答案 失败并显示此消息::irc.shakeababy.net461*USER:Notenoughparame

  5. ruby-on-rails - 连接字符串时如何在 <%=%> block 内输出 html_safe? - 2

    考虑一下:现在这些情况:#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2我需要用其他字符串输出URL。我如何保证&符号不会被转义?由于我无法控制的原因,我无法发送&。求助!把我的头发拉到这里:\编辑:为了澄清,我实际上有一个像这样的数组:@images=[{:id=>"fooid",:url=>"http://

  6. ruby - Faye WebSocket,关闭处理程序被触发后重新连接到套接字 - 2

    我有一个super简单的脚本,它几乎包含了FayeWebSocketGitHub页面上用于处理关闭连接的内容:ws=Faye::WebSocket::Client.new(url,nil,:headers=>headers)ws.on:opendo|event|p[:open]#sendpingcommand#sendtestcommand#ws.send({command:'test'}.to_json)endws.on:messagedo|event|#hereistheentrypointfordatacomingfromtheserver.pJSON.parse(event.d

  7. ruby-on-rails - 什么会导致与 APNS 的连接间歇性断开连接? - 2

    我有一个ruby​​脚本可以打开与Apple推送服务器的连接并发送所有待处理的通知。我看不出任何原因,但当Apple断开我的脚本时,我遇到了管道损坏错误。我已经编写了我的脚本来适应这种情况,但我宁愿只是找出它发生的原因,这样我就可以在第一时间避免它。它不会始终根据特定通知断开连接。它不会以特定的字节传输大小断开连接。一切似乎都是零星的。您可以在单个连接上发送的数据传输或有效负载计数是否有某些限制?看到人们的解决方案始终保持一个连接打开,我认为这不是问题所在。我看到连接在3次通知后断开,我看到它在14次通知后断开。我从未见过它能超过14点。有没有人遇到过这种类型的问题?如何处理?

  8. ruby - 如何断开现有的 ruby​​ 续集与数据库的连接? - 2

    我的意思是之前建立的那个DB=Sequel.sqlite('my_blog.db')或DB=Sequel.connect('postgres://user:password@localhost/my_db')或DB=Sequel.postgres('my_db',:user=>'user',:password=>'password',:host=>'localhost')等等。Sequel::Database类没有名为“disconnect”的公共(public)实例方法,尽管它有一个“connect”。也许有人已经遇到过这个问题。我将不胜感激。 最佳答案

  9. ruby-on-rails - Assets 管道损坏 : Not compiling on the fly css and js files - 2

    我开始了一个新的Rails3.2.5项目,Assets管道不再工作了。CSS和Javascript文件不再编译。这是尝试生成Assets时日志的输出:StartedGET"/assets/application.css?body=1"for127.0.0.1at2012-06-1623:59:11-0700Servedasset/application.css-200OK(0ms)[2012-06-1623:59:11]ERRORNoMethodError:undefinedmethod`each'fornil:NilClass/Users/greg/.rbenv/versions/1

  10. ruby-on-rails - Rails - 理解 application.js 和 application.css - 2

    rails新手。只是想了解\assests目录中的这两个文件。例如,application.js文件有如下行://=requirejquery//=requirejquery_ujs//=require_tree.我理解require_tree。只是将所有JS文件添加到当前目录中。根据上下文,我可以看出requirejquery添加了jQuery库。但是它从哪里得到这些jQuery库呢?我没有在我的Assets文件夹中看到任何jquery.js文件——或者直接在我的整个应用程序中没有看到任何jquery.js文件?同样,我正在按照一些说明安装TwitterBootstrap(http:

随机推荐