coaidev / coai

🚀 Next Generation AI One-Stop Internationalization Solution. 🚀 下一代 AI 一站式 B/C 端解决方案,支持 OpenAI,Midjourney,Claude,讯飞星火,Stable Diffusion,DALL·E,ChatGLM,通义千问,腾讯混元,360 智脑,百川 AI,火山方舟,新必应,Gemini,Moonshot 等模型,支持对话分享,自定义预设,云端同步,模型市场,支持弹性计费和订阅计划模式,支持图片解析,支持联网搜索,支持模型缓存,丰富美观的后台管理与仪表盘数据统计。
https://coai.dev
Apache License 2.0
6.95k stars 906 forks source link

连接已存在的MySQL数据库失败 #29

Closed pppfz closed 10 months ago

pppfz commented 10 months ago

现象描述

1、在1panel的面板环境下,已经安装MySQL和redis服务器在1panel-network(1panel面板的默认网络)。且我的MySQL、redis、chatnio在同一个网段,可以ping通。

{
    "Name": "1panel-network",
    "Id": "ded70d4xxxxxxxxxxxxxxxxxxxxxxxxxx0815",
    "Created": "xxxxxxxxxx",
    "Scope": "local",
    "Driver": "bridge",
    "EnableIPv6": false,
    "IPAM": {
      "Driver": "default",
      "Options": null,
      "Config": [
        {
          "Subnet": "172.18.0.0/16",
          "Gateway": "172.18.0.1"
        }
      ]
    },
    "Internal": false,
    "Attachable": false,
    "Ingress": false,
    "ConfigFrom": {
      "Network": ""
    },
    "ConfigOnly": false,
    "Containers": {
      "0b8f6fxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2e3379e": {
        "Name": "chatnio",
        "EndpointID": "ee76bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx17a64a79293f",
        "MacAddress": "02:42:ac:12:00:0d",
        "IPv4Address": "172.18.0.13/16",
        "IPv6Address": ""
      },
      "0b9f1e9ffxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx6df76f7550b7": {
        "Name": "mysql",
        "EndpointID": "460678adb4xxxxxxxxxxxxxxxxxxxxxxxxxxxx9dfe5f645c7e79e10",
        "MacAddress": "02:42:ac:12:00:08",
        "IPv4Address": "172.18.0.8/16",
        "IPv6Address": ""
      },
      "d7cef7b56bafa71xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxc295cd17985ea": {
        "Name": "redis",
        "EndpointID": "9f9a8aefxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx958f5fea",
        "MacAddress": "02:42:ac:12:00:09",
        "IPv4Address": "172.18.0.9/16",
        "IPv6Address": ""
      }
    },
    "Options": {},
    "Labels": {}
  }

image

使用的配置文件:

version: '3'
services:
  chatnio:
      image: programzmh/chatnio
      container_name: chatnio
      restart: always
      ports:
          - "8000:8094"
      environment:
          MYSQL_HOST: mysql
          MYSQL_USER: chanio
          MYSQL_PASSWORD: xxxxxxxxxxxx
          MYSQL_DATABASE: chanio
          REDIS_HOST: redis
          REDIS_PORT: 6379
          REDIS_PASSWORD: ""
          REDIS_DB: 0
          SERVE_STATIC: "true"
      volumes:
        - ./config:/config
        - ./logs:/logs

networks:
  1panel-network:
      external: true

这里的数据库和用户名就是chanio image

数据库连接检测

在MySQL容器中,成功连接数据库 image

报错

报错如下:

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

2023/12/26 08:59:24 [connection] failed to connect to mysql server: mysql, will retry in 5 seconds
2023/12/26 08:59:29 [connection] failed to connect to mysql server: mysql, will retry in 5 seconds
2023/12/26 08:59:34 [connection] failed to connect to mysql server: mysql, will retry in 5 seconds
2023/12/26 08:59:39 [connection] failed to connect to mysql server: mysql, will retry in 5 seconds
2023/12/26 08:59:44 [connection] failed to connect to mysql server: mysql, will retry in 5 seconds
2023/12/26 08:59:49 [connection] failed to connect to mysql server: mysql, will retry in 5 seconds
2023/12/26 08:59:54 [connection] failed to connect to mysql server: mysql, will retry in 5 seconds

问题

请问,我的问题是什么,该怎么修改。

zmh-program commented 10 months ago

redis 已经连接成功,则代表不是所述 1panel_network 的网络连接问题。 请自行排查 进入 chatnio 容器测试 SQL 连接情况。

zmh-program commented 10 months ago

顺带一提不是 chanio 而是 chatnio 请检查拼写是否有问题。

pppfz commented 10 months ago

redis 已经连接成功,则代表不是所述 1panel_network 的网络连接问题。 请自行排查 进入 chatnio 容器测试 SQL 连接情况。

请教两个问题 1、我该如何在docker容器中测试MySQL和redis连接(是apt 安装吗) 2、请问如何先连接redis数据库,而不是MySQL。

zmh-program commented 10 months ago

见 Dockerfile, chatnio 使用 centos 7.8,因此应为 yum 而非 Debian/Ubuntu 的 apt

FROM centos:7.8.2003

第二个问题我没有听懂是什么意思, chatnio 的 Redis 和 MySQL 都有断开重连机制。

pppfz commented 10 months ago

1、第一个问题是用yum 命令安装什么工具进行连接测试MySQL数据库。我只知道MySQL数据库自带的MySQL命令可以。(redis,我也不知道用什么工具测试)

2、我想表达的意思就是: 在检查日志报错的时候,只报出mysql数据库的连接失败情况,没有报出来redis数据库的连接情况。我想知道redisr数据库的连接情况是否正常(通过看日志文件)。

我就想让redis先进行连接,再连接mysql数据库,我这样就可以先于mysql数据库的连接,来判断出redis数据库的连接情况。

获取Outlook for Androidhttps://aka.ms/AAb9ysg


From: Zhang Minghan @.> Sent: Tuesday, December 26, 2023 6:07:46 PM To: Deeptrain-Community/chatnio @.> Cc: pppfz @.>; Author @.> Subject: Re: [Deeptrain-Community/chatnio] 连接已存在的MySQL数据库失败 (Issue #29)

见 Dockerfile, chatnio 使用 centos 7.8,因此应为 yum 而非 Debian/Ubuntu 的 apt

FROM centos:7.8.2003

第二个问题我没有听懂是什么意思, chatnio 的 Redis 和 MySQL 都有断开重连机制。

― Reply to this email directly, view it on GitHubhttps://github.com/Deeptrain-Community/chatnio/issues/29#issuecomment-1869428716, or unsubscribehttps://github.com/notifications/unsubscribe-auth/A4OGGPWEH7455H6BLRDEYI3YLKOXFAVCNFSM6AAAAABBDCFFTKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNRZGQZDQNZRGY. You are receiving this because you authored the thread.Message ID: @.***>

zmh-program commented 10 months ago
  1. 你可以尝试使用 mysql client 进入 chatnio 的容器内测试连接情况,如 (假设主机名为 mysql):

    mysql -h mysql -u chanio -p
  2. 如果 redis 也未连接,会提示:

    [connection] failed to connect to redis host
zmh-program commented 10 months ago

在此之前你可以进入容器 ping 一下 mysql 和 redis

pppfz commented 10 months ago

一、问题分析

1、ping值检测

image

2、mysql连接测试

image

3、redis连接测试

image

4、报错

2023/12/27 03:35:03 [connection] failed to connect to mysql server: mysql (message: ), will retry in 5 seconds
2023/12/27 03:35:08 [connection] failed to connect to mysql server: mysql (message: ), will retry in 5 seconds
2023/12/27 03:35:13 [connection] failed to connect to mysql server: mysql (message: ), will retry in 5 seconds
2023/12/27 03:35:18 [connection] failed to connect to mysql server: mysql (message: ), will retry in 5 seconds
2023/12/27 03:35:23 [connection] failed to connect to mysql server: mysql (message: ), will retry in 5 seconds
2023/12/27 03:35:28 [connection] failed to connect to mysql server: mysql (message: ), will retry in 5 seconds
2023/12/27 03:35:33 [connection] failed to connect to mysql server: mysql (message: ), will retry in 5 seconds

5、日志对比检查

我发现还是不行,我开始使用另一台服务器尝试使用你给的默认docker-compose.yml文件进行构建容器,得到如下日志:

[service] config.yaml not found, creating one from template: config.example.yaml
2023/12/27 03:41:28 [connection] failed to connect to mysql server: mysql (message: ), will retry in 5 seconds
2023/12/27 03:41:33 [connection] failed to connect to mysql server: mysql (message: ), will retry in 5 seconds
2023/12/27 03:41:38 [connection] failed to connect to mysql server: mysql (message: ), will retry in 5 seconds
2023/12/27 03:41:43 [connection] failed to connect to mysql server: mysql (message: ), will retry in 5 seconds
2023/12/27 03:41:48 [connection] connected to mysql server (host: mysql)
[service] no user found, creating root user (username: root, password: chatnio123456, email: root@example.com)
2023/12/27 03:41:49 [connection] connected to redis (host: redis)
[service] start serving static files from ~/app/dist

发现有一个配置文件:config.yaml 创建

随即到文件中查看配置文件如下:

mysql:
  db: chatnio
  host: localhost
  password: chatnio123456
  port: 3306
  user: root

redis:
  host: localhost
  port: 6379
  db: 0
  password: ""

secret: SbitdyN5ZH39cNxSrG3kMNZ1GfiyyQ43

auth:
  use_deeptrain: false

server:
  port: 8094
system:
  general:
    backend: ""
  mail:
    host: ""
    port: 465
    username: ""
    password: ""
    from: ""
  search:
    endpoint: https://duckduckgo-api.vercel.app
    query: 5

二、发现问题

1、问题一

我又发现有数据库的配置信息,我回到出问题的数据库上检查这个文件,发现问题所在:配置文件中的内容没有跟随docker-compose.yaml的environment变量改变:(还是同上一样)

mysql:
  db: chatnio
  host: localhost
  password: chatnio123456
  port: 3306
  user: root

redis:
  host: localhost
  port: 6379
  db: 0
  password: ""

secret: SbitdyN5ZH39cNxSrG3kMNZ1GfiyyQ43

auth:
  use_deeptrain: false

server:
  port: 8094
system:
  general:
    backend: ""
  mail:
    host: ""
    port: 465
    username: ""
    password: ""
    from: ""
  search:
    endpoint: https://duckduckgo-api.vercel.app
    query: 5

image-20231227120030978

2、问题二

且发现docker-compose.yaml中的密码 chatnio123456! 和配置文件config.yaml:中的密码chatnio123456 不一致。

三、解决方法

修改配置文件config.yaml 中的数据库连接信息为自己已存在的数据信息。

四、疑惑

zmh-program commented 10 months ago

且发现docker-compose.yaml中的密码 chatnio123456! 和配置文件config.yaml:中的密码chatnio123456 不一致。

这个, viper是优先读取环境变量如果有环境变量优先返回,然后再去读取配置的,配置是个模板,不影响。如果解决 就代表你之前的环境变量没有设置正确。

zmh-program commented 10 months ago

以及这个配置生成是因为chatnio 检测到没有配置自动copy模板了

zmh-program commented 10 months ago

debug 模式是因为你没开DEBUG="false"

zmh-program commented 10 months ago

配置文件中的secret变量没有自动随机,会不会有什么安全影响。

这个倒是没什么,这是jwt 的“盐” 可以进行自动生成,但是本质上没有什么用,里面也没有什么安全漏洞信息。