Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySQL 数据库无法连接 #62

Open
DickDock opened this issue Sep 13, 2023 · 12 comments
Open

MySQL 数据库无法连接 #62

DickDock opened this issue Sep 13, 2023 · 12 comments

Comments

@DickDock
Copy link

按照文档进行操作,MySQL 数据库无法连接。报错如下:
panic: cannot find database driver for specified database type "mysql", did you misspell type name "mysql" or forget importing the database driver? possible reference: https://github.com/gogf/gf/tree/master/contrib/drivers [recovered]
panic: failed to connect database

goroutine 1 [running]:
github.com/cool-team-official/cool-admin-go/cool.InitDB.func1()

可能是配置文件错误

@vera-byte
Copy link
Contributor

请将https://github.com/gogf/gf/tree/master/contrib/drivers放到main的improt 加载mysql驱动 默认是sqlite

@homeofmny
Copy link

请将https://github.com/gogf/gf/tree/master/contrib/drivers放到main的improt 加载mysql驱动 默认是sqlite

你好,windows环境下根据文档import添加github.com/gogf/gf/contrib/drivers/mysql/v2后依然会报这个错误,应该如何解决呢?

@Dr-Urso
Copy link

Dr-Urso commented Jan 27, 2024

我也出现了同样的问题,依然无法解决

@homeofmny
Copy link

homeofmny commented Jan 28, 2024

我也出现了同样的问题,依然无法解决

我通过改库文件临时解决了这个问题,在cool-admin-go\cool\initdb.go中的import里加入_ "github.com/gogf/gf/contrib/drivers/mysql/v2"可临时解决。

改库文件只是针对本机,对于不同开发环境不适用,这种情况可以尝试在你的项目目录下git submodule add https://github.com/cool-team-official/cool-admin-go.git cool-admin-go把cool-admin-go这个项目下载下来,然后在按照前述方法修改后在你的项目目录下创建go.work,这样做对cool-admin-go的引用等你的项目的一切都不需要改变并且给其他人使用时也不需要再去修改库文件,go.work内容如下:

go 你的版本号

use (
	.
	./cool-admin-go
	./cool-admin-go/contrib/drivers/mysql
	./cool-admin-go/contrib/drivers/pgsql
	./cool-admin-go/contrib/drivers/sqlite
	./cool-admin-go/contrib/files/local
	./cool-admin-go/contrib/files/minio
	./cool-admin-go/contrib/files/oss
	./cool-admin-go/cool
	./cool-admin-go/cool-tools
	./cool-admin-go/docs
	./cool-admin-go/frontend
	./cool-admin-go/modules/base
	./cool-admin-go/modules/demo
	./cool-admin-go/modules/dict
	./cool-admin-go/modules/space
	./cool-admin-go/modules/task
)

我的这个方法破坏了cool-admin-go的驱动隔离性,上不了台面,就不发pull requests了,期待作者大大更好的解决方案

@vera-byte
Copy link
Contributor

如下https://github.com/gogf/gf/tree/master/contrib/drivers 放到main的improt加载mysql驱动默认是sqlite

你好,windows环境下根据文档导入添加github.com/gogf/gf/contrib/drivers/mysql/v2后仍然会报这个错误,应该如何解决呢?

windows 下报什么错误方便提示下么

将mysql驱动放置main.go import前面
例如:
image

@homeofmny
Copy link

如下https://github.com/gogf/gf/tree/master/contrib/drivers 放到main的improt加载mysql驱动默认是sqlite

你好,windows环境下根据文档导入添加github.com/gogf/gf/contrib/drivers/mysql/v2后仍然会报这个错误,应该如何解决呢?

windows 下报什么错误方便提示下么

将mysql驱动放置main.go import前面 例如: image

你好,我的main.go是这样的:
image
直接使用运行会报错:

2024-01-28 14:23:11.183 [DEBU] {ec90b247a96eae17a7e8de66a13221da} module cool init finished ...
panic: cannot find database driver for specified database type "mysql", did you misspell type name "mysql" or forget importing the database driver? possible reference: https://github.com/gogf/gf/tree/master/contrib/drivers [recovered]
        panic: failed to connect database

goroutine 1 [running]:
github.com/cool-team-official/cool-admin-go/cool.InitDB.func1()
        cool-admin-go/cool/initdb.go:22 +0x36
panic({0x132cbc0?, 0xc0002fc550?})
        go/src/runtime/panic.go:914 +0x21f
github.com/gogf/gf/v2/frame/gins.Database.func1()
        GOPATH/pkg/mod/github.com/gogf/gf/[email protected]/frame/gins/gins_database.go:148 +0xbed
github.com/gogf/gf/v2/container/gmap.(*StrAnyMap).doSetWithLockCheck(0xc0003ac370, {0xc0000f50b0, 0x22}, {0x12369e0, 0xc0000b7200})
        GOPATH/pkg/mod/github.com/gogf/gf/[email protected]/container/gmap/gmap_hash_str_any_map.go:219 +0x132
github.com/gogf/gf/v2/container/gmap.(*StrAnyMap).GetOrSetFuncLock(0x40?, {0xc0000f50b0, 0x22}, 0x0?)
        GOPATH/pkg/mod/github.com/gogf/gf/[email protected]/container/gmap/gmap_hash_str_any_map.go:256 +0x51
github.com/gogf/gf/v2/internal/instance.GetOrSetFuncLock({0xc0000f50b0, 0x22}, 0xc000147cc8?)
        GOPATH/pkg/mod/github.com/gogf/gf/[email protected]/internal/instance/instance.go:65 +0x89
github.com/gogf/gf/v2/frame/gins.Database({0xc000298be0, 0x1, 0x1})
        GOPATH/pkg/mod/github.com/gogf/gf/[email protected]/frame/gins/gins_database.go:37 +0x172
github.com/gogf/gf/v2/frame/g.DB(...)
        GOPATH/pkg/mod/github.com/gogf/gf/[email protected]/frame/g/g_object.go:87
github.com/cool-team-official/cool-admin-go/cool.InitDB({0x13ae1de, 0x7})
        cool-admin-go/cool/initdb.go:25 +0xa5
github.com/cool-team-official/cool-admin-go/cool.getDBbyModel({0x1593e28?, 0xc000454000?})
        cool-admin-go/cool/initdb.go:44 +0x85
github.com/cool-team-official/cool-admin-go/cool.CreateTable({0x1593e28?, 0xc000454000})
        cool-admin-go/cool/initdb.go:57 +0x37

在cool-admin-go\cool\initdb.go中的import里加入_ "github.com/gogf/gf/contrib/drivers/mysql/v2"就正常了

@vera-byte
Copy link
Contributor

2024-01-28 14:23:11.183 [DEBU] {ec90b247a96eae17a7e8de66a13221da} module cool init finished ...
panic: cannot find database driver for specified database type "mysql", did you misspell type name "mysql" or forget importing the database driver? possible reference: https://github.com/gogf/gf/tree/master/contrib/driv

方便提供下你的项目配置文件吗

@homeofmny
Copy link

2024-01-28 14:23:11.183 [DEBU] {ec90b247a96eae17a7e8de66a13221da} module cool init finished ...
panic: cannot find database driver for specified database type "mysql", did you misspell type name "mysql" or forget importing the database driver? possible reference: https://github.com/gogf/gf/tree/master/contrib/driv

方便提供下你的项目配置文件吗

好的,我的config.yaml如下:

server:
  serverRoot: "resource/public"
  clientMaxBodySize: 104857600  # 100MB in bytes 100*1024*1024

logger:
  path: "./data/logs/"
  file: "{Y-m-d}.log"
  level : "all"
  stdout: true


database:
  default: # 数据源名称,当不指定数据源时 default 为默认数据源
    type: "mysql" # 数据库类型
    host: "127.0.0.1" # 数据库地址
    port: "3306" # 数据库端口
    user: "root" # 数据库用户名
    pass: "123456" # 数据库密码
    name: "cool" # 数据库名称
    charset: "utf8mb4" # 数据库编码
    timezone: "Asia/Shanghai" # 数据库时区
    debug: true # 是否开启调试模式,开启后会打印SQL日志
    createdAt: "createTime" # 创建时间字段
    updatedAt: "updateTime" # 更新时间字段
redis:
  cool:
    address: "127.0.0.1:6379"
    db: 0
    
cool:
  autoMigrate: true
  eps: true
  file:
    mode: "local"
    domain: "http://127.0.0.1:80"

modules:
  base:
    jwt:
      sso: false
      secret: "cool-admin-go"
      token:
        expire: 7200 # 2*3600
        refreshExpire: 1296000 # 24*3600*15
    middleware:
      authority:
        enable: 1
      log:
        enable: 1

@vera-byte
Copy link
Contributor

2024-01-28 14:23:11.183 [DEBU] {ec90b247a96eae17a7e8de66a13221da}模块cool init完成...
恐慌:找不到指定数据库类型“mysql”的数据库驱动程序,您是否拼写错误类型名称“mysql”或忘记导入数据库司机?可能的参考: https: //github.com/gogf/gf/tree/master/contrib/drive

方便提供下您的项目配置文件吗

好了,我的config.yaml如下:

server:
  serverRoot: "resource/public"
  clientMaxBodySize: 104857600  # 100MB in bytes 100*1024*1024

logger:
  path: "./data/logs/"
  file: "{Y-m-d}.log"
  level : "all"
  stdout: true


database:
  default: # 数据源名称,当不指定数据源时 default 为默认数据源
    type: "mysql" # 数据库类型
    host: "127.0.0.1" # 数据库地址
    port: "3306" # 数据库端口
    user: "root" # 数据库用户名
    pass: "123456" # 数据库密码
    name: "cool" # 数据库名称
    charset: "utf8mb4" # 数据库编码
    timezone: "Asia/Shanghai" # 数据库时区
    debug: true # 是否开启调试模式,开启后会打印SQL日志
    createdAt: "createTime" # 创建时间字段
    updatedAt: "updateTime" # 更新时间字段
redis:
  cool:
    address: "127.0.0.1:6379"
    db: 0
    
cool:
  autoMigrate: true
  eps: true
  file:
    mode: "local"
    domain: "http://127.0.0.1:80"

modules:
  base:
    jwt:
      sso: false
      secret: "cool-admin-go"
      token:
        expire: 7200 # 2*3600
        refreshExpire: 1296000 # 24*3600*15
    middleware:
      authority:
        enable: 1
      log:
        enable: 1

github.com/cool-team-official/cool-admin-go/cool 的版本多少呢

@homeofmny
Copy link

2024-01-28 14:23:11.183 [DEBU] {ec90b247a96eae17a7e8de66a13221da}模块cool init完成...
恐慌:找不到指定数据库类型“mysql”的数据库驱动程序,您是否拼写错误类型名称“mysql”或忘记导入数据库司机?可能的参考: https: //github.com/gogf/gf/tree/master/contrib/drive

方便提供下您的项目配置文件吗

好了,我的config.yaml如下:

server:
  serverRoot: "resource/public"
  clientMaxBodySize: 104857600  # 100MB in bytes 100*1024*1024

logger:
  path: "./data/logs/"
  file: "{Y-m-d}.log"
  level : "all"
  stdout: true


database:
  default: # 数据源名称,当不指定数据源时 default 为默认数据源
    type: "mysql" # 数据库类型
    host: "127.0.0.1" # 数据库地址
    port: "3306" # 数据库端口
    user: "root" # 数据库用户名
    pass: "123456" # 数据库密码
    name: "cool" # 数据库名称
    charset: "utf8mb4" # 数据库编码
    timezone: "Asia/Shanghai" # 数据库时区
    debug: true # 是否开启调试模式,开启后会打印SQL日志
    createdAt: "createTime" # 创建时间字段
    updatedAt: "updateTime" # 更新时间字段
redis:
  cool:
    address: "127.0.0.1:6379"
    db: 0
    
cool:
  autoMigrate: true
  eps: true
  file:
    mode: "local"
    domain: "http://127.0.0.1:80"

modules:
  base:
    jwt:
      sso: false
      secret: "cool-admin-go"
      token:
        expire: 7200 # 2*3600
        refreshExpire: 1296000 # 24*3600*15
    middleware:
      authority:
        enable: 1
      log:
        enable: 1

github.com/cool-team-official/cool-admin-go/cool 的版本多少呢

你好,使用的是1.5.7最新版,go版本是1.21.5

@vera-byte
Copy link
Contributor

vera-byte commented Jan 28, 2024

1.5.7 我这边测试也没问题
这是cool-admin-go\cool\initdb.go 引入
image
cool-admin-go 都是采用的gf v2版本
image

mysql驱动包也是使用的gf v2版本
image

@homeofmny
Copy link

1.5.7 我这边测试也没问题 这是cool-admin-go\cool\initdb.go 引入 image cool-admin-go 都是采用的gf v2版本 image

mysql驱动包也是使用的gf v2版本 image

你好,我尝试了半天,发现问题出现在go版本上,如果切换到1.20.6或者1.20.8这样的1.20版本就正常了,如果使用1.21.5这个新一点的版本就会发生错误 o(TωT)o 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants