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

Docker Compose部署Neo4j v4.1遇cypher-shell认证失败求助

问题描述

尝试通过Docker Compose运行Neo4j v4.1企业版数据库,配置文件如下:

docker-compose.yml

services:
  neo4j:
    build: .
    container_name: neo4j
    ports:
      - "7474:7474"  # HTTP port
      - "7687:7687"  # Bolt port
    volumes:
      - ./data:/data
      - ./import:/var/lib/neo4j/import
      - ./plugins:/plugins
    environment:
      - NEO4J_AUTH=neo4j/mypass 
      - NEO4J_ACCEPT_LICENSE_AGREEMENT=yes

Dockerfile

FROM neo4j:4.1-enterprise
# Copy your initialization scripts into the container
COPY scripts /var/lib/neo4j/scripts
RUN chmod +x scripts/entrypoint.sh
ENTRYPOINT ["bash", "/var/lib/neo4j/scripts/entrypoint.sh"]

entrypoint.sh

#!/bin/bash

# Start Neo4j in the background
neo4j start

# Wait for Neo4j to be ready
echo "Waiting for Neo4j to start..."
sleep 15

echo "Testing cypher-shell authentication..."
cypher-shell -u neo4j -p mypass "RETURN 1;"

neo4j stop

# Start Neo4j in the foreground
neo4j console

执行docker-compose up后,日志出现认证失败:

neo4j  | Testing cypher-shell authentication...
neo4j  | The client is unauthorized due to authentication failure.

已尝试以下操作但无效:

  • 设置NEO4J_AUTH=none或NEO4J_AUTH=neo4j/neo4j
  • 删除./data目录并重新构建镜像(docker-compose build --no-cache)
  • 检查/var/lib/neo4j/logs/debug.log未发现错误

此外,访问https://localhost:7474/时提示连接关闭:

This site can’t be reached
localhost unexpectedly closed the connection.
Try:

Checking the connection
Checking the proxy and the firewall
Running Windows Network Diagnostics
ERR_CONNECTION_CLOSED

解决方法

1. 修复自定义Entrypoint的初始化逻辑

官方Neo4j镜像的/docker-entrypoint.sh负责处理NEO4J_AUTH等环境变量的初始化(比如设置初始密码),你的自定义entrypoint跳过了这一步,导致密码配置完全未生效,这是认证失败的核心原因。修改entrypoint.sh,先执行官方初始化脚本:

#!/bin/bash

# 先执行官方entrypoint完成环境变量初始化
/docker-entrypoint.sh neo4j start

# 用循环等待Neo4j就绪,替代固定sleep(更可靠)
echo "Waiting for Neo4j to start..."
until cypher-shell -u neo4j -p mypass "RETURN 1;" 2>/dev/null; do
    sleep 3
done

echo "Testing cypher-shell authentication..."
cypher-shell -u neo4j -p mypass "RETURN 1;"

# 停止后台进程,切换到前台运行以保持容器存活
neo4j stop
/docker-entrypoint.sh neo4j console

2. 修正浏览器访问地址

Neo4j 4.1默认HTTP端口是7474(对应http://localhost:7474),HTTPS端口为7473。你访问https://localhost:7474会触发协议不匹配导致连接关闭,改用HTTP协议访问即可。

3. 彻底清理旧数据并重新构建

确保完全清除旧数据和缓存:

  • 删除本地./data目录:rm -rf ./data
  • 重新构建镜像:docker-compose build --no-cache
  • 启动容器:docker-compose up

备选方案:不替换官方Entrypoint

如果不需要复杂的自定义启动逻辑,直接删除Dockerfile中的ENTRYPOINT配置,让官方镜像的entrypoint自动处理初始化,NEO4J_AUTH会直接生效。若需执行初始化脚本,可将脚本挂载到/var/lib/neo4j/scripts,并在docker-compose的环境变量中添加NEO4J_INITIAL_SCRIPT=/var/lib/neo4j/scripts/your-script.cypher。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 07:45:12