Connection pool
The go language comes with a database connection pool, which can be used directly and conveniently.
func init() { db, _ = sql.Open("mysql", "root:[email protected](127.0.0.1:3306)/betting?charset=utf8") // 设置最大打开的连接数,默认值为0表示不限制。 db.SetMaxOpenConns(2000) // 用于设置闲置的连接数。 db.SetMaxIdleConns(1000) db.Ping()}
- By setting the maximum number of connections, you can avoid too many connections errors that occur when you connect to MySQL with too high concurrency.
- Set the number of idle connections when a connection is opened, it can be placed in the pool for the next use.
Call a stored procedure
When I use go to invoke a stored procedure, how to get the value in the Out parameter and never find the correct method, I only summarize the method using the in parameter.
Stored Procedures
CREATE DEFINER = ‘root‘@‘localhost‘ PROCEDURE `proc_test`( IN `in_id` INTEGER ) NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER COMMENT ‘‘BEGIN SELECT log_ip FROM admin_log WHERE id=in_id;END;
Call in Go
func main() { db, err := sql.Open("mysql", "root:[email protected](127.0.0.1:3306)/betting?charset=utf8") checkError("连接数据库", err) defer db.Close() var outArg string row := db.QueryRow("call proc_test(?)", 2000) row.Scan(&outArg) fmt.Println(outArg)}
Execution results
192.168.88.91
MySQL database operation in go Language (ii)