dongmi19720408
dongmi19720408
2016-10-15 07:29
浏览 1.1k

golang:如何在Go中连接到多个MySQL数据库? [关闭]

operating 3 or more database at the same time, Read/Write Splitting, have connection pool.

图片转代码服务由CSDN问答提供 功能建议

同时操作3个或更多数据库, 读取/写入拆分, 具有连接池。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doubo82706
    doubo82706 2016-10-15 12:14
    已采纳

    Just create different variables (db connections) using the Open function of sql library

    import "database/sql"
    import _ "github.com/go-sql-driver/mysql"
    
    func Connect() (*DB, *DB, *DB) {
        db1, err := sql.Open("mysql", "user1:password1@/dbname1")
        //check err
        db2, err := sql.Open("mysql", "user2:password2@/dbname2")
        //check err
        db3, err := sql.Open("mysql", "user3:password3@/dbname3")
        //check err
        return db1, db2, db3
    }
    

    And you can use them further absolutely separately.

    点赞 评论
  • jay_2008
    golang人工智能框架 2021-05-13 17:24

    gorm多数据库连接方法

    点赞 评论

相关推荐