前情提要

  • 之前看到有坛友MJJ发帖一个新的支持 mysql 的哪吒探针镜像里有人给哪吒加了多数据库支持,主控高并发时 SQLite 卡顿,改 MySQL 后好很多。
  • 上游源码: https://github.com/Shannon-x/nezha ,镜像:ghcr.io/shannon-x/nezha:latest
  • 但我实际拉取镜像部署后实测:MySQL OK,但 PostgreSQL 仍有不兼容问题(SQL 语法和字段类型),遂自改。

我做的主要改动

项目地址:https://github.com/cold-sword/nezha
DockerHub:https://hub.docker.com/r/coldsword/nezha-pgfix

  1. 清理任务 SQL 适配 PG
    • service/singleton/singleton.go 中的清理语句去掉 MySQL 专属的反引号和 datetime() 包装,改为 PG 兼容的子查询与时间比较。
  2. 通知表字段类型兼容 PG
    • model/notification.gogorm:"type:longtext" 改为 gorm:"type:text",避免 PG 建表失败。
    • 恢复通知占位符处理函数 replaceParamsInString(之前删掉导致编译错误);去掉对 #SERVER.HOST# 的错误使用(Host 是 struct 不是 string)。
  3. 构建镜像方式调整
    • Dockerfile.build 升级 Go 1.25,纯 Go 构建,移除 npm;前端采用预编译 dist 包,go:embed 自动打包。
    • 构建时先下载前端 dist:
      curl -L -o admin-dist.zip https://github.com/nezhahq/admin-frontend/releases/download/v1.14.4/dist.zipunzip -q admin-dist.zip -d cmd/dashboard/ && mv cmd/dashboard/dist cmd/dashboard/admin-dist && rm admin-dist.zipcurl -L -o user-dist.zip https://github.com/hamster1963/nezha-dash-v1/releases/download/v1.32.0/dist.zipunzip -q user-dist.zip -d cmd/dashboard/ && mv cmd/dashboard/dist cmd/dashboard/user-dist && rm user-dist.zip
  4. 可选:GeoIP 步骤
    • 如果用不到 IPInfo 的 GeoIP,删掉工作流中的下载步骤;需要的话在仓库 Secrets 配 IPINFO_TOKEN

常见错误与对应修复

  • 编译报 ns.replaceParamsInString undefinedmodel/notification.go 末尾的 helper 被删,补回即可(上面已恢复)。
  • 编译报 cannot use ns.Server.Host (type *Host) as string:占位符里把 Host struct 当字符串用,删除/改为具体字段,当前已删除。
  • PG 建表失败/longtext 无此类型:改为 gorm:"type:text"(已改)。
  • 白屏/无前端资源:仓库不含前端源码,需下载 dist 放到 cmd/dashboard/admin-distuser-dist 后再构建,go:embed 会打包。
  • IPINFO 下载 exit 6:未配置 Token,可删步骤或配置 IPINFO_TOKEN

使用方式(示例)

  1. 拉取或构建镜像:
    # 从 GitHub Container Registry 拉取docker pull ghcr.io/cold-sword/nezha:latest# 或从 DockerHub 拉取docker pull coldsword/nezha-pgfix:latest
  2. data/config.yaml 配置 PostgreSQL:
    database:   type: postgres  host: your-pg-host  port: 5432  username: nezha  password: your_password  dbname: nezha  sslmode: disable   # 按需调整
  3. docker-compose 中替换镜像为 ghcr.io/cold-sword/nezha:latestcoldsword/nezha-pgfix:latest,启动即可。