代码之家  ›  专栏  ›  技术社区  ›  GCSDC

在多个集合上执行事务时出现MongoDB Atlas错误(代码8000)

  •  4
  • GCSDC  · 技术社区  · 7 年前

    here )我得到以下错误:

    code:8000
    codeName:"AtlasError"
    errmsg:"internal atlas error checking things: Failure getting dbStats: read tcp 192.168.254.78:50064->192.168.254.78:27000: i/o timeout"
    message:"internal atlas error checking things: Failure getting dbStats: read tcp 192.168.254.78:50064->192.168.254.78:27000: i/o timeout"
    name:"MongoError"
    

    附加信息:

    • 交易

    • 如果我删除所有其他操作,只留下一个(不

    • 如果我改变操作顺序(任何顺序),错误是

    • 如果所有操作都对同一个数据库和集合执行,那么就可以正常工作

    我的代码:

    async function connect () {
    
    if (dbClient !== null && dbClient.isConnected()) {
        console.log('Reusing db connection => ' + JSON.stringify(dbClient));
      } else {
          console.log('Connecting to database');
          dbClient = await MongoClient.connect(url, { useNewUrlParser: true });
          console.log('Successfully connected to database');
      }
    }
    
    async function insertDocuments(document1, document2, document3) {
    
      try {
        await connect();
      } catch (error) {
        throw error
      }
    
      let session = dbClient.startSession();
    
      session.startTransaction({
        readConcern: { level: 'snapshot' },
        writeConcern: { w: 'majority' }
      });
    
      const collection1 = dbClient.db('mydbname').collection('collection1');
      const collection2 = dbClient.db('mydbname').collection('collection2');
      const collection3 = dbClient.db('mydbname').collection('collection3');
      const logsCollection = dbClient.db('mydbname').collection('logs');
    
      await collection1.replaceOne(
        { _id: document1._id },
        document1,
        {
          upsert: true,
          session
        }
      );
      await collection2.replaceOne(
        { _id: document2._id },
        document2,
        {
          upsert: true,
          session
        }
      );
      await collection3.replaceOne(
        { _id: document3._id },
        document3,
        {
          upsert: true,
          session
        }
      );
      await logsCollection.updateOne(
        { _id: document1._id },
        { $unset: { estoque: '' } },
        { session }
      );
    
      try {
        await commitWithRetry(session);
      } catch (error) {
        await session.abortTransaction();
        throw error;
      }
    }
    
    async function commitWithRetry(session) {
      try {
        await session.commitTransaction();
        console.log('Transação gravada com sucesso');
      } catch (error) {
        if (
          error.errorLabels &&
          error.errorLabels.indexOf('UnknownTransactionCommitResult') >= 0
        ) {
          console.log('Transação não realizada, tentando novamente ...');
          await commitWithRetry(session);
        } else {
          console.log('Erro ao gravar no banco de dados ...');
          throw error;
        }
      }
    }
    

    你知道怎么解决这个问题吗?提前谢谢!

    3 回复  |  直到 7 年前
        1
  •  4
  •   Michael McCabe    7 年前

    我也有同样的问题,解决不了。我最终在Azure上创建了一个新的集群(而不是AWS),它看起来运行得很好。

    不确定这是否是他们在AWS上实现的问题,或者是具体的错误配置

    :新群集上现在发生相同的错误。我开了一张罚单,得到了如下答复;

    这已被确定为一个bug,目前影响Atlas的M0免费集群和共享层(M2和M5)。我们已经打开了一个内部错误报告来解决这个问题。虽然我们的云产品的内部开发队列并不公开,但我们正在跟踪使多文档事务在无Atlas层集群上工作所需的工作。

        2
  •  2
  •   GCSDC    7 年前

    正如评论中提到的,根据MongoDB团队的说法,这个问题已经在版本4.0.5上得到了解决

    我已经测试了同样的代码张贴在我的问题,它现在工作良好。

    对于面临相同问题的任何人,请确保使用等于或高于4.0.5的版本。

        3
  •  1
  •   halfer Jatin Pandey    6 年前

        4
  •  1
  •   El_Sadany32    5 年前

    我解决了这个问题,在我的数据库网络访问,在mongo db Cloub和删除了旧的IP地址,并把一个新的IP地址,通过单击添加IP地址,并选择了这个当前的ID地址

        5
  •  0
  •   David Buck Richard Ferris    6 年前

    步骤1下载 https://robomongo.org/download

    • 当您拥有Studio 3T试用版许可证界面时,请转至快速入门

    localhost:27017 然后给你的项目命名

    去: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

    1. brew tap mongodb/brew
    2. brew install mongodb-community@4.2
    3. mongo . 如果您得到==> Successfully started `mongodb-community` (label: homebrew.mxcl.mongodb-community) ,则希望终端上没有错误。

    最后一步:返回界面,点击蓝色按钮DB会自动连接。

    // .env file on project root folder
    MONGO_URI='mongodb://localhost:27017/projectname'
    

    const express = require('express');
    const mongoose = require('mongoose');
    
    require('dotenv').config();
    
    const app = express();
    
    mongoose
      .connect(process.env.MONGO_URI, { useNewUrlParser: true })
      .then(() => console.log('DB Connected'));
    
    mongoose.connection.on('error', (err) => {
      console.log(`DB connection error: ${err.message}`);
    });
    
    app.get('/', (req, res) => {
      res.send('hello from the other side');
    });
    
        6
  •  0
  •   Susnata Goswami    5 年前

    我得到了一个类似的错误,我花了几个小时试图调试它。然后我删除了有AWS的旧集群,用googlecloud创建了一个新集群,它运行得很好。

        7
  •  0
  •   Kundan    5 年前

    我也遇到过类似的问题,只要检查一下你的连接URI。我的密码错了。

        8
  •  0
  •   Nelcon Croos    5 年前

        9
  •  0
  •   Diwakar Prasad    5 年前

    我通过在MongoDb站点上用atlas创建一个数据库来解决这个问题。