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

AWS ECS任务启动失败:Couchbase libcouchbase致命错误求助

问题:ECS部署NestJS+Couchbase服务启动崩溃(libcouchbase断言失败)

我在AWS ECS上部署NestJS应用,Couchbase数据库部署在EC2实例上,服务启动时会执行N1QL全量查询,但每次ECS任务创建后都会进入Deprovisioning状态,容器日志报错如下:

FATAL ERROR:
    libcouchbase experienced an unrecoverable error and terminates the program
    to avoid undefined behavior.
    The program should have generated a "corefile" which may used
    to gather more information about the problem.
    If your system doesn't create "corefiles" I can tell you that the
    assertion failed in ../deps/lcb/src/mcserver/negotiate.cc at line 50
Aborted (core dumped)

我的Couchbase查询连接代码如下:

export class CouchbaseService implements OnModuleInit {
    private cluster: Cluster;
    private bucket: Bucket;
    private collection: Collection;
    private isInit: boolean = false;
    cols: Map<string, CollectionHelper> = new Map();

    constructor(
        private readonly configService: ConfigService,
        @InjectSentry() private readonly sentry: SentryService,
    ) {}

    async onModuleInit(): Promise<void> {
        await this.connect();
    }

    async connect() {
        // Make connection
        this.cluster = await connect(this.configService.get('couchbase.url'), {
            username: this.configService.get<string>('couchbase.username'),
            password: this.configService.get<string>('couchbase.password'),
        });
        // Initialise bucket
        this.bucket = this.cluster.bucket(
            this.configService.get<string>('couchbase.bucket'),
        );
        this.collection = this.bucket.defaultCollection();
        this.isInit = true;
        console.log('COUCHBASE INITIATED');
    }

    query<T>(query: string, options?: QueryOptions) {
        return this.handleErrors(async () => {
            try {
                const result = await this.cluster.query(query, options);
                return result?.rows ? result?.rows : [];
            } catch (e) {
                console.log('N1QL ERROR: ', e);
                return [];
            }
        });
    }
}

解决方案

这个错误是libcouchbase在与Couchbase服务器协商连接时断言失败,通常和网络连通性、版本兼容性、连接配置有关,按以下步骤排查修复:

1. 确认ECS与EC2的网络连通性

  • 检查ECS任务安全组是否开放对Couchbase端口的访问:需要放行8091(管理端口)、8093(N1QL端口)、11210(数据端口)
  • 在ECS容器内执行telnet <EC2私有IP> 8093或ping <EC2私有IP>,验证网络可达性
  • 确保ECS和EC2在同一VPC,或配置了正确的跨VPC路由规则

2. 检查SDK与服务器版本兼容性

  • 核对couchbase npm包版本与EC2上Couchbase服务器版本的兼容性:比如SDK 4.x对应服务器6.5+,SDK 3.x对应服务器6.0+,不匹配则升级/降级SDK到兼容范围

3. 修复连接代码的初始化逻辑

当前代码仅获取bucket实例,但未等待bucket完成初始化,添加等待逻辑:

async connect() {
    // Make connection
    this.cluster = await connect(this.configService.get('couchbase.url'), {
        username: this.configService.get<string>('couchbase.username'),
        password: this.configService.get<string>('couchbase.password'),
    });
    // Initialise bucket
    this.bucket = this.cluster.bucket(
        this.configService.get<string>('couchbase.bucket'),
    );
    // 新增:等待bucket就绪,超时时间按需调整
    await this.bucket.waitUntilReady(60000);
    this.collection = this.bucket.defaultCollection();
    this.isInit = true;
    console.log('COUCHBASE INITIATED');
}

同时确认couchbase.url配置的是EC2的私有IP(VPC内访问优先),而非公网IP(除非必要)

4. 排查SSL协商问题(可选)

如果是SSL/TLS协商失败导致的断言:

  • 临时禁用SSL测试(生产环境不建议):在连接选项中添加security: { enableTls: false }
  • 设置环境变量LCB_LOGLEVEL=5,获取更详细的libcouchbase日志,定位具体协商失败原因

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 06:07:41