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

使用pymqi连接远程MQ管理器遇MQRC_CONNECTION_BROKEN(2009)错误求助

问题

本地运行Python MQ收发程序连接远程机器qmgr.test.com上的队列管理器QMGR1、QMGR2时,出现MQRC 2009错误:

MQ Error for QMGR1: 2009 - FAILED: MQRC_CONNECTION_BROKEN
MQ Error for QMGR2: 2009 - FAILED: MQRC_CONNECTION_BROKEN

已完成以下排查:

  • 本地与MQ主机网络连通正常(telnet主机及端口成功)
  • QMGR1和QMGR2运行状态良好
  • 本地可通过amqsputc命令向同一队列管理器及队列收发消息
  • 已通过代码打印验证MQ连接信息无误

附程序代码及客户端错误日志,求问导致该错误的原因:

Python程序代码

import pymqi
import time
import logging

# Configuration details for IBM MQ

queue_managers = [
    {
        'queue_manager': 'QMGR1',
        'channel': 'QMGR1.SVRCONN',
        'queue_name': 'QMGR1.QUEUE',
        'host': 'qmgr.test.com',
        'port': '1441',
        'user': 'mquser',
        'password': '******'
    },
    {
        'queue_manager': 'QMGR2',
        'channel': 'QMGR2.SVRCONN',
        'queue_name': 'QMGR2.QUEUE',
        'host': 'qmgr.test.com',
        'port': '1441',
        'user': 'mquser',
        'password': '******'
    },
]
logging.basicConfig(filename='mq_health_check.log',level=logging.INFO)

def check_queue_manager_health(queue_manager_info, message):
    qmgr = None
    try:
        # Connection info
        conn_info = f"{queue_manager_info['host']}({queue_manager_info['port']})"

        qmgr = pymqi.connect(queue_manager_info['queue_manager'],queue_manager_info['channel'],conn_info,queue_manager_info['user'],queue_manager_info['password'])
        
        # open the queue
        queue = pymqi.Queue(qmgr, queue_manager_info['queue_name'])

        # Send the message
        queue.put(message)
        logging.info(f"Message sent to {queue_manager_info['queue_manager']} on {queue_manager_info['queue_name']}.")

        # Retrieve the message 
        received_message = queue.get()

        # check queue depth
        queue_depth = queue.inquire(pymqi.CMQC.MQIA_CURRENT_Q_DEPTH)
        logging.info(f"Queue Depth for {queue_manager_info['queue_manager']} on {queue_manager_info['queue_name']}: {queue_depth}")

        queue.close()

    except pymqi.MQMIError as e:
        print(f"MQ Error for {queue_manager_info['queue_manager']}: {e.reason} - {e.errorAsString()}")

    except Exception as e:
        print(f"General Error for {queue_manager_info['queue_manager']}: {e.errorAsString()}")
    
    finally:
        if qmgr:
            qmgr.disconnect()

if __name__ == "__main__":
    # Message
    message = "Test Message for monitoring"
    # Send the message to each queue manager
    for manager in queue_managers:
        check_queue_manager_health(manager, message)

客户端错误日志(翻译后)

----- amqxufnx.c : 1446 ------------------------------------------------------- 10/16/24 15:58:21 - 进程(15507.1) 程序(Python)
                    主机(AB89548) 安装包(MQNI93L22121400D)
                    版本(9.3.1.0)
                    时间(2024-10-16T20:58:21.725Z)
                    数值参数1(24948) 数值参数2(2)
                    文本参数1(/opt/homebrew/lib64/libmqe_r.dylib)
                    文本参数2(没有该文件或目录)
                    文本参数3(64)
                    AMQ6174I: 无法打开错误消息ID 0x6174的目录,参数:24948, 2, /opt/homebrew/lib64/libmqe_r.dylib, 没有该文件或目录 和 64。请在其他系统上执行"mqrc AMQ6174"查看消息描述。
----- amqxufnx.c : 1446 -------------------------------------------------------

可能的原因及解决方案

从错误日志里的AMQ6174I信息可以看出,核心问题是Python运行环境找不到MQ客户端依赖的动态库/opt/homebrew/lib64/libmqe_r.dylib,这会导致pymqi无法正常初始化MQ连接,最终触发MQRC 2009(连接中断)错误。

具体分析及解决步骤:

  1. MQ客户端库路径配置问题

    • 本地通过amqsputc能正常收发消息,说明系统级的MQ客户端配置正常,但Python进程未加载到正确的库路径。
    • 解决方案:在Python代码开头添加环境变量配置,指定MQ客户端库的正确路径:
      import os
      # 根据实际MQ客户端安装路径调整,Homebrew默认路径通常为/opt/homebrew/lib/
      os.environ['DYLD_LIBRARY_PATH'] = '/opt/homebrew/lib/'
      
    • 或者启动脚本前先设置环境变量:
      export DYLD_LIBRARY_PATH=/opt/homebrew/lib/:$DYLD_LIBRARY_PATH
      python your_script.py
      
  2. pymqi与MQ客户端版本不兼容

    • 日志显示MQ客户端版本为9.3.1.0,若pymqi版本与该MQ版本不匹配,可能出现库加载异常。
    • 解决方案:重新安装适配版本的pymqi,例如:
      pip uninstall pymqi
      pip install pymqi==1.16.0  # 该版本适配MQ 9.3.x,可根据实际情况调整
      
  3. Homebrew安装的MQ客户端路径错误

    • Homebrew在macOS上的默认lib路径是/opt/homebrew/lib/,而非日志中显示的/opt/homebrew/lib64/,路径错误可能是pymqi或MQ客户端配置问题导致。
    • 解决方案:创建软链接纠正路径:
      sudo ln -s /opt/homebrew/lib/ /opt/homebrew/lib64
      

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 03:46:17