You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js驱动与MongoDB wire版本不兼容问题排查及解决

解决MongoServerSelectionError:Wire Version不兼容问题

问题场景

升级老旧Node.js应用至最新npm包后,出现MongoDB驱动兼容错误:

MongoServerSelectionError: Server at 127.0.0.1:27017 reports maximum wire version 5, but this version of the Node.js Driver requires at least 6 (MongoDB 3.6)

当前环境

  • Node.js v20.17.0
  • connect-mongo 5.1.0(已适配MongoStore相关引用)
  • mongod --version显示MongoDB v6.0.17,可通过Robo 3T连接27017端口
  • 操作系统:Mac Sonoma 14.6.1
  • 不愿回退Node.js或MongoDB版本

相关配置

package.json

{  
  "name": "tc",  
  "version": "0.0.0",  
  "private": true,  
  "scripts": {  
    "start": "pm2 startOrReload pm2.json",  
    "prod": "pm2 startOrReload pm2.json --env production",  
    "logs": "pm2 logs",  
    "test": "node --use_strict $(npm bin)/jasmine"  
  },  
  "dependencies": {  
    "angular2": "2.0.0-beta.15",  
    "async": "^3.2.6",  
    "base64url": "^3.0.1",  
    "bcrypt": "^5.1.1",  
    "body-parser": "^1.20.2",  
    "bootstrap": "^5.3.3",  
    "bootstrap-less": "^3.3.8",  
    "bson": "^6.8.0",  
    "codemirror": "^5.65.17",  
    "connect-flash": "^0.1.1",  
    "connect-mongo": "^5.1.0",  
    "cookie-parser": "^1.4.6",  
    "dropzone": "^6.0.0-beta.2",  
    "ejs": "^3.1.10",  
    "express": "^4.19.2",  
    "express-session": "^1.18.0",  
    "file-saver": "^2.0.5",  
    "gridfs-stream": "^1.1.1",  
    "jquery": "^3.7.1",  
    "jsdom": "^24.1.1",  
    "jszip": "^3.10.1",  
    "jszip-utils": "^0.1.0",  
    "libxmljs": "^1.0.11",  
    "lodash": "^4.17.21",  
    "mongo-connect": "^0.0.6",  
    "mongoose": "^8.5.3",  
    "morgan": "^1.10.0",  
    "multer": "^1.4.5-lts.1",  
    "nodemailer": "^6.9.14",  
    "nodemailer-mailgun-transport": "^2.1.5",  
    "openseadragon": "^5.0.0",  
    "passport": "^0.7.0",  
    "passport-facebook": "^3.0.0",  
    "passport-google-oauth": "^2.0.0",  
    "passport-local": "^1.0.0",  
    "passport-twitter": "^1.0.4",  
    "promise-defer": "^1.0.0",  
    "require-in-the-middle": "^7.4.0",  
    "serve-favicon": "^2.5.0",  
    "sort-array": "^4.1.5",  
    "urijs": "^1.19.11"  
  },  
  "devDependencies": {  
    "css-loader": "^7.1.2",  
    "jasmine": "^5.2.0",  
    "less": "^4.2.0",  
    "less-loader": "^12.2.0",  
    "mini-css-extract-plugin": "^2.9.1",  
    "style-loader": "^4.0.0",  
    "webpack": "^5.94.0"  
  }
}

webpack.js

var _ = require('lodash')  
  , webpack = require('webpack')  
  , ResolverPlugin = webpack.ResolverPlugin  
  , ProvidePlugin = webpack.ProvidePlugin  
  , IgnorePlugin = webpack.IgnorePlugin//  , ExtractTextPlugin = require("extract-text-webpack-plugin")  
  , path = require('path')  
  , clientRoot = path.resolve(__dirname)  
  , bowerRoot = path.resolve(clientRoot, '..', 'bower_components')  
  , nodeRoot = path.resolve(clientRoot, '..', 'node_modules')  
  , devtool = 'eval-source-map'  
  , debug = true;

switch (process.env.NODE_ENV) {  
  case 'production':  
    devtool = '#source-map';  
    debug = false;  
    break;  
  case 'development':  
    devtool = 'eval-source-map';  
    debug = true;  
    break;
}

var config = {  
  context: clientRoot,  
  entry: {  
    app: path.join(clientRoot, 'app/boot.js'),  
    t: path.join(clientRoot, 'app/t.js'),  
  },  
  output: {  
    path: path.join(clientRoot, 'dist'),  
    filename: '[name].bundle.js',  
    devtoolModuleFilenameTemplate(info) {   
      return `file:///${info.absoluteResourcePath.replace(/\\/g, '/')}`;   
    },  
  },  
  externals: {  
    jquery: 'jQuery',  
    rxjs: 'Rx',  
    lodash: '_',  
    bson: 'bson',  
    'codemirror/lib/codemirror': 'CodeMirror',  
    'codemirror/mode/xml/xml': false,  
  },  
  module: {  
    rules: [  
      {  
        test: /\.png(\?v=\d+\.\d+\.\d+)?$/,  
        use: "url?limit=1000&minetype=image/jpg&prefix=dist/"  
      }, {  
        test: /\.jpg(\?v=\d+\.\d+\.\d+)?$/,  
        use: "url?limit=1000&minetype=image/jpg&prefix=dist/"  
      }, {  
        test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,  
        use: "url?limit=1000&minetype=application/font-woff&prefix=dist/"  
      }, {  
        test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,  
        use: "url?limit=1000&minetype=application/font-woff&prefix=dist/"  
      }, {  
        test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,  
        use: "url?limit=1000&minetype=application/octet-stream&prefix=dist/"  
      }, {  
        test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,  
        use: "url?limit=1000&minetype=application/vnd.ms-fontobject&prefix=dist/"  
      }, {  
        test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,  
        use: "url?limit=1000&minetype=image/svg+xml&prefix=dist/"  
      },  
      {  
        test: /\.css$/,   
        use:["style-loader", "css-loader"]  
        //loader: 'style!css'  
      },  
      {  
        test: /\.less$/,  
        use: [  
          "style-loader",  
          "css-loader",  
          {  
            loader: "less-loader",  
            options: {  
              lessOptions: {    //          javascriptEnabled: true,  
              },  
            },   
          },  
        ],  
      },  
    ], /*,  
    noParse: [  
    ] */  
  },  
  resolve: { /*   root: [clientRoot], */  
    modules: [clientRoot, 'web_modules', 'node_modules', 'bower_components', ],  
    fallback: {  
        "fs": false,  
        "tls": false,  
        "net": false,  
        "path": false,  
        "zlib": false,  
        "http": false,  
        "https": false,  
        "stream": false,  
        "crypto": false,  
        "crypto-browserify": false, //if you want to use this module also don't forget npm i crypto-browserify       
    },  
    alias: {  
      bower: bowerRoot,  
      node: nodeRoot,  
    },  
  },  
  plugins: [  
    new webpack.DefinePlugin({  
      'process.env': {  
        NODE_ENV: JSON.stringify(process.env.NODE_ENV),  
      },  
    }),   
    new webpack.LoaderOptionsPlugin({  
        debug: true  
    }),/*  
    new ResolverPlugin(new ResolverPlugin.DirectoryDescriptionFilePlugin(  
      'bower.json', ['main']  
    ),   
      new webpack.ResolverPlugin(  
      new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin("package.json", ["main"])  
    ), */   
   // prevent webpack accident include server security information//    new IgnorePlugin(new RegExp('config\/production.*')),//    new ExtractTextPlugin("app.css"),/*  
    new webpack.optimize.CommonsChunkPlugin({  
      name: 'vendor', filename: 'vendor.bundle.js',  
      chunks: ['app'],  
      minChunks: function(module, count) {  
        return module.resource && module.resource.indexOf(clientRoot) === -1;  
      }  
    }), */  
  ],  
  devtool: devtool,  
};

var compiler = webpack(config);

if (process.env.NODE_ENV === 'development') {  
  compiler.watch({  
    aggregateTimeout: 300,  
    poll: 1000,  
  }, handleError);  
} else { compiler.watch({  
    aggregateTimeout: 300,  
    poll: 1000,  
  }, handleError);  
  //compiler.run(handleError);  
}

function handleError(err, stats) {  
  console.log(stats.toString({  
    colors: true,  
    cached: false,  
  }));  
}

解决步骤

排查后确认,虽然mongod --version显示v6.0.17,但实际运行的数据库实例为旧版本(wire version 5对应MongoDB 3.2-3.4)。通过以下流程解决:

  1. 备份数据:执行mongodump命令导出所有数据库内容
  2. 升级MongoDB:替换为MongoDB Community 7.0版本
  3. 启动新实例:启动空的7.0版本MongoDB服务
  4. 恢复数据:执行mongorestore命令导入之前备份的数据

总结

遇到同类wire version不兼容问题且不愿回退Node.js或驱动版本时,优先确认实际运行的MongoDB实例版本,采用备份→升级→恢复的流程处理,避免直接覆盖旧版本导致的数据丢失风险。

内容的提问来源于stack exchange,提问作者peter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 09:14:53