前言
由于线上neo4j免费账户到期,导致使用neo4j的agent模块跑不起来。日志如下:
INFO:hello_agents.memory.storage.qdrant_store:✅ 成功连接到Qdrant云服务: https://50.aws.cloud.qdrant.io
INFO:hello_agents.memory.storage.qdrant_store:✅ 使用现有Qdrant集合: hello_agents_vectors
INFO:hello_agents.memory.manager:MemoryManager初始化完成,启用记忆类型: ['episodic']
Batches: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 36.14it/s]
INFO:hello_agents.memory.types.semantic:✅ 嵌入模型就绪,维度: 4096
INFO:hello_agents.memory.types.semantic:✅ Qdrant向量数据库初始化完成
ERROR:hello_agents.memory.storage.neo4j_store:❌ Neo4j服务不可用: Failed to DNS resolve address 3a9229e9.databases.neo4j.io:7687: [Errno -2] Name or service not known
INFO:hello_agents.memory.storage.neo4j_store:💡 请检查URL和网络连接
ERROR:hello_agents.memory.types.semantic:❌ 数据库初始化失败: Failed to DNS resolve address 3a9229e9.databases.neo4j.io:7687: [Errno -2] Name or service not known
INFO:hello_agents.memory.types.semantic:💡 请检查数据库配置和网络连接
原neo4j的配置:
# Wait 60 seconds before connecting using these details, or login to https://console.neo4j.io to validate the Aura Instance is available
NEO4J_URI=neo4j+s://39.databases.neo4j.io
NEO4J_USERNAME=39
NEO4J_PASSWORD=vhZ5mY
NEO4J_DATABASE=39
AURA_INSTANCEID=39
AURA_INSTANCENAME=Instance02
1. 设计方案
方案:在另一台能跑 Docker 的机器上部署 Neo4j,当前 code-server Pod 通过 Bolt 协议远程连接。
架构示意:
┌─────────────────────────┐ ┌──────────────────────────┐
│ 当前机器 (K8s Pod) │ 7687 │ 另一台机器 (Docker 宿主机) │
│ HelloAgents / Python │ ──────► │ neo4j 容器 │
│ .env: NEO4J_URI=bolt://│ │ 端口 7687 对外暴露 │
└─────────────────────────┘ └──────────────────────────┘
在另一台机器上启动 Neo4j
远程连接时,端口要绑到 0.0.0.0,不要只绑 127.0.0.1:
docker run -d \
--name neo4j \
--restart unless-stopped \
-p 7474:7474 \
-p 7687:7687 \
-e NEO4J_AUTH=neo4j/hello-agents-password \
-v neo4j_data:/data \
neo4j:5
验证:
docker ps
docker logs neo4j --tail 20
(base) [my_learning]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
41347d72baf2 neo4j:5 "tini -g -- /startup…" 8 seconds ago Up 7 seconds 0.0.0.0:7474->7474/tcp, :::7474->7474/tcp, 7473/tcp, 0.0.0.0:7687->7687/tcp, :::7687->7687/tcp neo4j
(base) [my_learning]$ docker logs neo4j --tail 20
Changed password for user 'neo4j'. IMPORTANT: this change will only take effect if performed before the database is started for the first time.
2026-06-22 09:05:51.280+0000 INFO Logging config in use: File '/var/lib/neo4j/conf/user-logs.xml'
2026-06-22 09:05:51.302+0000 INFO Starting...
2026-06-22 09:05:52.798+0000 INFO This instance is ServerId{9f8d28c5} (9f8d28c5-d79e-4780-a92d-96954ef6447d)
2026-06-22 09:05:54.630+0000 INFO ======== Neo4j 5.26.27 ========
2026-06-22 09:05:57.836+0000 INFO Anonymous Usage Data is being sent to Neo4j, see https://neo4j.com/docs/usage-data/
2026-06-22 09:05:57.901+0000 INFO Bolt enabled on 0.0.0.0:7687.
2026-06-22 09:05:59.719+0000 INFO HTTP enabled on 0.0.0.0:7474.
2026-06-22 09:05:59.720+0000 INFO Remote interface available at http://localhost:7474/
2026-06-22 09:05:59.723+0000 INFO id: E33E15A3CE0D124F4F9EDD5E6A5B339BC543259BF222F70EDD0D26563969674A
2026-06-22 09:05:59.723+0000 INFO name: system
2026-06-22 09:05:59.723+0000 INFO creationDate: 2026-06-22T09:05:56.151Z
2026-06-22 09:05:59.724+0000 INFO Started.
2. 网络与安全配置(关键)
另一台机器需要放行 7687(Bolt 连接用;7474 是 Web 界面,可选):
| 检查项 | 说明 |
|---|---|
| 防火墙 | 放行 7687(ufw / 云厂商安全组) |
| 访问来源 | 最好只允许当前 Pod 所在网段或固定 IP |
| 公网暴露 | 不建议把 7687 对 0.0.0.0/0 开放;优先用内网 IP |
| 密码 | 使用强密码,不要用弱口令上公网 |
在当前 Pod 里先测网络是否通:
# 把 <NEO4J机器IP> 换成另一台机器的内网/公网 IP
nc -zv <NEO4J机器IP> 7687
# 或
curl -v telnet://<NEO4J机器IP>:7687
Connection refused → 端口未监听或未放行;timed out → 防火墙/安全组问题。
安装nc:
apt install netcat-openbsd -y
测试:
(base) root@# nc -zv 192.*.*.41 7687
Connection to 192.*.*.41 7687 port [tcp/*] succeeded!
(base) root# curl -v telnet://192.**.**.41:7687
* Trying 192.*****.41:7687...
* Connected to 192.******.41 (192.****.41) port 7687 (#0)
3. 修改当前 Pod 的 .env
# 远程 Neo4j(另一台 Docker 机器)
NEO4J_URI=bolt://<NEO4J机器IP>:7687
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=hello-agents-password
NEO4J_DATABASE=neo4j
# 注释掉已过期的 Aura 配置
# NEO4J_URI=neo4j+s://39.databases.neo4j.io
# NEO4J_USERNAME=39
# ...
注意:
- 远程自建一般用
bolt://,不是 Aura 的neo4j+s:// NEO4J_DATABASE社区版通常是neo4j
4. 连接验证
python3 -c "
from dotenv import load_dotenv; load_dotenv()
from hello_agents.tools import MemoryTool
m = MemoryTool(user_id='remote_neo4j_test', memory_types=['semantic'])
print(m.run({
'action': 'add',
'content': '远程 Neo4j 连接测试',
'memory_type': 'semantic',
'importance': 0.8
}))
"
测试:
(agent) root@# python3 -c "
from dotenv import load_dotenv; load_dotenv()
from hello_agents.tools import MemoryTool
m = MemoryTool(user_id='remote_neo4j_test', memory_types=['semantic'])
print(m.run({
'action': 'add',
'content': '远程 Neo4j 连接测试',
'memory_type': 'semantic',
'importance': 0.8
}))
"
INFO:hello_agents.memory.storage.qdrant_store:✅ 成功连接到Qdrant云服务: https://93cce-3da9-c5-5f-3c.us-east-2-0.aws.cloud.qdrant.io
INFO:hello_agents.memory.storage.qdrant_store:✅ 使用现有Qdrant集合: hello_agents_vectors
INFO:hello_agents.memory.types.semantic:✅ Qdrant向量数据库初始化完成
INFO:hello_agents.memory.storage.neo4j_store:✅ 成功连接到Neo4j服务: bolt://192.1.*.41:7687
INFO:hello_agents.memory.storage.neo4j_store:✅ Neo4j索引创建完成
INFO:hello_agents.memory.types.semantic:✅ Neo4j图数据库初始化完成
INFO:hello_agents.memory.types.semantic:🏥 数据库健康状态: Qdrant=✅, Neo4j=✅
INFO:hello_agents.memory.types.semantic:✅ 加载中文spaCy模型: zh_core_web_sm
WARNING:hello_agents.memory.types.semantic:⚠️ 英文spaCy模型不可用: en_core_web_sm
INFO:hello_agents.memory.types.semantic:🎯 主要使用中文spaCy模型
INFO:hello_agents.memory.types.semantic:📚 可用语言模型: 中文
INFO:hello_agents.memory.types.semantic:增强语义记忆初始化完成(使用Qdrant+Neo4j专业数据库)
INFO:hello_agents.memory.manager:MemoryManager初始化完成,启用记忆类型: ['semantic']
Batches: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 33.97it/s]
INFO:hello_agents.memory.storage.qdrant_store:[Qdrant] add_vectors start: n_vectors=1 n_meta=1 collection=hello_agents_vectors
INFO:hello_agents.memory.storage.qdrant_store:[Qdrant] upsert begin: points=1
INFO:hello_agents.memory.storage.qdrant_store:[Qdrant] upsert done
INFO:hello_agents.memory.storage.qdrant_store:✅ 成功添加 1 个向量到Qdrant
INFO:hello_agents.memory.types.semantic:✅ 添加语义记忆: 0个实体, 0个关系
✅ 记忆已添加 (ID: 334cafe5...)
5. 常见问题
| 现象 | 可能原因 |
|---|---|
Connection refused | Neo4j 未启动,或 Docker 只绑了 127.0.0.1 |
timed out | 安全组/防火墙未放行 7687 |
| 认证失败 | 用户名/密码与 NEO4J_AUTH 不一致 |
| semantic 初始化慢 | 首次会连 Qdrant + 加载 embedding,属正常 |
转载自 CSDN-专业IT技术社区
原文链接:https://blog.csdn.net/try2find/article/details/162208804



