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

使用django_neomodel连接最新版Neo4j Desktop数据库遇认证问题

问题描述

初始认证错误

使用最新版Neo4j Desktop新建数据库,通过django_neomodel连接时抛出认证异常:

neo4j.exceptions.AuthError: {code: Neo.ClientError.Security.Unauthorized} {message: The client is unauthorized due to authentication failure.}

追踪后发现连接时auth参数实际为None,已将django_neomodel安装并添加至Django项目的INSTALLED_APPS中。

注:未使用默认neo4j:neo4j账号密码的原因:最新版Neo4j Desktop无法设置密码为neo4j,创建数据库时设置的neo4j:ArrowGlue也无效,因此在Neo4j Browser中执行:server user add命令,添加了拥有管理员权限的用户ArrowGlue:ArrowGlue。

后续连接错误

将Neo4j连接URL从环境变量移至Django的settings.py后,出现新的连接错误:

File "C:\Python311\Lib\site-packages\neo4j\_sync\io\_bolt.py", line 808, in fetch_message
  res = self._process_message(tag, fields)
File "C:\Python311\Lib\site-packages\neo4j\_sync\io\_bolt5.py", line 361, in _process_message
  response.on_failure(summary_metadata or {})
File "C:\Python311\Lib\site-packages\neo4j\_sync\io\_common.py", line 247, in on_failure
  raise Neo4jError.hydrate(**metadata)

neo4j.exceptions.ClientError: {code: Neo.ClientError.Security.CredentialsExpired} {message: ACCESS on database 'neo4j' is not allowed.
The credentials you provided were valid, but must be changed before you can use this instance. If this is the first time you are using Neo4j, this is to ensure you are not using the default credentials in production. If you are not using default credentials, you are getting this message because an administrator requires a password change.
To change your password, issue an `ALTER CURRENT USER SET PASSWORD FROM 'current password' TO 'new password'` statement against the system database.}
解决方案

1. 处理CredentialsExpired错误

首先解决密码强制修改问题,需在Neo4j Browser中切换到system数据库执行修改命令:

  • 打开Neo4j Browser,点击左上角数据库选择器,切换到system数据库
  • 执行以下Cypher语句(替换对应密码):
ALTER CURRENT USER SET PASSWORD FROM 'ArrowGlue' TO '你的新密码';

若需指定用户修改,可执行:

ALTER USER ArrowGlue SET PASSWORD FROM 'ArrowGlue' TO '你的新密码';

2. 修正django_neomodel的认证配置

在Django的settings.py中正确配置Neo4j连接信息,确保认证参数被正确传递:

# settings.py
NEOMODEL_NEO4J_BOLT_URL = "bolt://ArrowGlue:你的新密码@localhost:7687"

# 也可拆分配置(按需选择)
NEOMODEL_NEO4J_USER = "ArrowGlue"
NEOMODEL_NEO4J_PASSWORD = "你的新密码"
NEOMODEL_NEO4J_BOLT_URL = f"bolt://{NEOMODEL_NEO4J_USER}:{NEOMODEL_NEO4J_PASSWORD}@localhost:7687"

注意:确保django_neomodel版本与Neo4j 5.x+版本兼容,避免因版本不匹配导致的参数传递问题

3. 验证连接有效性

重启Django项目后,可在Django Shell中执行以下代码测试连接:

from neomodel import config
from neomodel.db import db
from django.conf import settings

config.DATABASE_URL = settings.NEOMODEL_NEO4J_BOLT_URL
db.set_connection(config.DATABASE_URL)
try:
    db.cypher_query("MATCH (n) RETURN count(n)")
    print("连接成功")
except Exception as e:
    print(f"连接失败:{e}")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 04:00:07