modulepackage
2.0.3+incompatible
Repository: https://github.com/scofieldpeng/mysql-go.git
Documentation: pkg.go.dev
# README
mysql-go
a simple mysql package for golang projects base on xorm,support connection pools and master&slave operation
install
go get github.com/scofieldpeng/mysql-go
Usage
First, create a ini file under your $ProjectPath/config
(or any dir that you like) directory like below:
# node_* sections are the mysql nodes configurations,the node name is the `*` part,ie node_default,the node name is default
[mysql_node_default]
# mysql dsn driver configuration,more detail see here https://github.com/go-sql-driver/mysql#dsn-data-source-name
dsn=root:123456@tcp(127.0.0.1:3306)/test?charset=utf8mb4
# if the node have slaves nodes,add the node name,each nodes devide by `,`
# note make every slave have configuration in the ini file!!!
slave=slave1,slave2,slave3
# connection pool max connect number, only affect to this node
maxConn=100
# max idle connect number, only affect to this node
maxIdle=5
slave config
[mysql_node_slave1] dsn=root:123456@tcp(127.0.0.1:3306)/test?charset=utf8mb4
Second, initialize the mysql engine
// initialize the mysql ini file,more document see the package https://github.com/scofieldpeng/config
connConfig := config.Data("mysql")
mysqlConfig := mysql.Config{
Debug: false,
MaxIdle:5,
MaxConn:10,
}
if err := mysql.Init(mysqlConfig,mysqlFileConfig);err != nil {
fmt.Println(err)
}
Now, you can use as you like
// return a xorm engine object,now you can use mysql operation freely
xormEngine := mysql.Select("default").Engine()
// or you can use the master node
xormMasterEngine := mysql.Select("default").Master()
// most of time we always choose the master node, but you want to use slave,you can use the Slave method,
// when you set the slave name param, you can operation the specific slave node, or the system will return a random slave node
xormSlaveEngine := mysql.Select("default").Slave("slave1")
if you are not familiar with xorm, you can see the xorm document http://xorm.io
Licence
MIT Licence
Thanks
# Functions
初始化.
No description provided by the author
选择某个节点,如果没有选择节点名称,默认选择default节点 BUG: 当用户没有设置default节点配置时如果又通过default来获取,可能会获取不到结果.
# Constants
No description provided by the author
默认的连接池空闲数大小.
默认的最大打开连接数.
默认超时时间.
# Structs
engine mysql的连接对象.
No description provided by the author
WhereBuilder用来构建xorm的engine所需的where模块 使用方法很easy mysql.NewWhereBuilder() mysql.Add("id",1) 然后使用的时候直接mysql.Select().
# Interfaces
No description provided by the author