使用pyorient连接OrientDB遇协议版本37不支持问题求新方案
解决PyOrient不支持OrientDB协议版本37的问题
问题重现
报错信息:
[...] File "C:\Users\civan\Documents\GitHub\code\venv\lib\site-packages\pyorient\orient.py", line 101, in connect raise PyOrientWrongProtocolVersionException( pyorient.exceptions.PyOrientWrongProtocolVersionException: Protocol version 37 is not supported yet by this client.
使用的代码:
import pyorient client = pyorient.OrientDB("etc", 2424) session_id = client.db_open("db", "admin", "pass!") sql = "SELECT id, name, ifnull(org_id, 1) AS org_id, create_dt.format('yyyy-MM-dd HH:mm:ss') AS create_dt FROM Client" records = client.query(sql) data = [o.oRecordData for o in records] print(f'Fetched {len(data)} rows')
已尝试旧社区方案但无效,以下是可行的新解决办法:
可行解决方案
1. 切换到维护中的PyOrient分支或官方推荐客户端
官方pyorient包已长时间未更新,无法支持新版本OrientDB的协议。可以选择:
- 安装社区维护的适配版本,比如
pyorient3,执行命令:
替换代码中的pip install pyorient3import pyorient为import pyorient3 as pyorient,其余代码逻辑无需修改。 - 改用OrientDB官方推荐的
orientdb-python-client,安装命令:
示例代码:pip install orientdb-python-clientfrom orientdb import OrientDBClient with OrientDBClient(host="etc", port=2424) as client: session = client.session("db", "admin", "pass!") result = session.query("SELECT id, name, ifnull(org_id, 1) AS org_id, create_dt.format('yyyy-MM-dd HH:mm:ss') AS create_dt FROM Client") data = [record.to_dict() for record in result] print(f'Fetched {len(data)} rows')
2. 降级OrientDB服务端的协议版本(临时方案)
如果暂时无法更换客户端,可以修改OrientDB服务端配置,降低协议版本到pyorient支持的范围:
- 找到OrientDB安装目录下的
config/orientdb-server-config.xml文件 - 找到
<network>节点下的<parameter name="network.binary.protocolVersion" value="37"/> - 将
value改为36或33(旧版pyorient通常支持到36,可根据实际测试调整) - 重启OrientDB服务
3. 使用HTTP API替代二进制客户端
绕过二进制协议限制,直接通过OrientDB的REST API操作:
import requests import json # 认证获取会话令牌 auth_url = "http://etc:2480/db/_session" auth_payload = json.dumps({ "user": "admin", "password": "pass!" }) auth_headers = {"Content-Type": "application/json"} response = requests.post(auth_url, data=auth_payload, headers=auth_headers) session_token = response.json()["token"] # 执行查询 query_url = "http://etc:2480/db/query/sql" query_params = { "q": "SELECT id, name, ifnull(org_id, 1) AS org_id, create_dt.format('yyyy-MM-dd HH:mm:ss') AS create_dt FROM Client" } query_headers = { "Content-Type": "application/json", "Authorization": f"Bearer {session_token}" } response = requests.get(query_url, params=query_params, headers=query_headers) data = response.json()["result"] print(f'Fetched {len(data)} rows')
内容的提问来源于stack exchange,提问作者Claudiu
相关产品推荐
相关产品推荐

