第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 golang 中使用 gorm 通過 ssh 連接數(shù)據(jù)庫?

如何在 golang 中使用 gorm 通過 ssh 連接數(shù)據(jù)庫?

Go
翻翻過去那場雪 2022-07-04 10:37:21
我正在嘗試通過 ssh 連接到數(shù)據(jù)庫,我正在使用database/sql包連接到數(shù)據(jù)庫,但我不明白如何實現(xiàn)database/sqlgorm jinzhu/gorm。有人可以告訴我如何實現(xiàn)它嗎?或者有什么方法可以通過使用 gorm 包來連接?我對golang還很陌生。這就是代碼的樣子。主要問題是NewBrandsRepository方法不能使用類型*sql.DB作為*gorm.DB// Connect to the SSH Serversshcon, errSSH := ssh.Dial("tcp", fmt.Sprintf("%s:%d", sshHost, sshPort), sshConfig)if errSSH != nil {}defer sshcon.Close()// Now we register the ViaSSHDialer with the ssh connection as a parametermysql.RegisterDial("mysql+tcp", (&ViaSSHDialer{sshcon}).Dial)// And now we can use our new driver with the regular mysql connection string tunneled through the SSH connectiondb, errDB := sql.Open("mysql", fmt.Sprintf("%s:%s@mysql+tcp(%s)/%s", dbUser, dbPass, dbHost, dbName))if errDB != nil {    fmt.Printf("Failed to connect to the db: %s\n", errDB.Error())}fmt.Printf("Successfully connected to the db\n")test := NewBrandsRepository(db)NewBrandsRepository方法是這樣的type brandsRepositoryImpl struct {    db *gorm.DB}func NewBrandsRepository(db *gorm.DB) *brandsRepositoryImpl {    return &brandsRepositoryImpl{db}}
查看完整描述

1 回答

?
慕桂英4014372

TA貢獻1871條經(jīng)驗 獲得超13個贊

繼續(xù)執(zhí)行相同的步驟,但不要使用 sql.Open 使用


dsn := fmt.Sprintf("%s:%s@mysql+tcp(%s)/%s", dbUser, dbPass, dbHost, dbName)

db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})


查看完整回答
反對 回復 2022-07-04
  • 1 回答
  • 0 關注
  • 363 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號