Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure mysql connection Pool with go

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use go to configure mysql connection pool", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to configure mysql connection pool with go"!

Go configure mysql connection pooling

Mysql configuration document

Max-connections=10000

Code

Orm

1.ab initiates concurrent request test

-c: concurrency 100

-n: total number of requests 300

➜~ ab-c 100-n 300 "http://localhost:8080/pool"This is ApacheBench, Version 2.3 Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation Http://www.apache.org/Benchmarking localhost (be patient) Completed 100 requestsCompleted 200 requestsCompleted 300 requestsFinished 300 requestsServer Software:Server Hostname: localhostServer Port: 8080Document Path: / poolDocument Length: 304bytesConcurrency Level: 100Time taken for tests: 4.073 secondsComplete requests: 300Failed requests: 0Total transferred: 128400 bytesHTML transferred: 91200 bytesRequests per second: 73.66 [# / sec] (mean) Time per request: 1357.601 [ms] (mean) Time per request: 13.576 [ms] (mean Across all concurrent requests) Transfer rate: 30.79 [Kbytes/sec] receivedConnection Times (ms) min mean [+ /-sd] median maxConnect: 02 2.2 1 8Processing: 1001 1012 8.2 1010 1036Waiting: 1001 1012 8.1 1010 1036Total: 1002 1014 9.5 1012 1043Percentage of the requests served within a certain time (ms) 50 101266 1016 75% 1019 80% 1021 90% 1031 95% 1035 98% 1039 99% 1040 100% 1043 (longest request) ➜~ 2.PoolHandler trigger sql query

Set idle connections to 10

The maximum number of connections that can be opened is the maximum number of connections that can be modified in 10 mysql.conf.

Db/db.go

SqlDB.SetMaxIdleConns (10) / / SetMaxOpenConns sets the maximum number of open database connections. SqlDB.SetMaxOpenConns (10) / / SetConnMaxLifetime sets the maximum time that the connection can be reused. SqlDB.SetConnMaxLifetime (time.Hour)

Controller/pool.go

Func PoolHandler (c * gin.Context) {time.Sleep (time.Second) var user model.User tx: = db.DB.Raw ("SELECT id, name, age FROM users WHERE name =? Limit 1 "," D42 "). Scan (& user) if tx.Error! = nil {panic (tx.Error)} logger.Debugf (" raw sql id:%v name:%v age:%v ", user.ID, user.Name, user.Age) c.JSON (200, user)} 3. View the request SHOW PROCESSLIST created by the request mysql during the test Id User Host db Command Time State Info8 root localhost:51148 demo_go Query 0 starting SHOW PROCESSLIST900 root localhost:64598 demo_go Sleep 0 901 root localhost:64754 demo_go Sleep 0 902 root localhost:64755 demo_go Sleep 0 903 root localhost:64756 demo_go Sleep 0 904 root localhost:64757 demo_go Sleep 0 905 root localhost:64758 demo_go Sleep 0 906 root localhost:64759 demo_go Sleep 0 907 root localhost:64760 demo_go Sleep 0 908 root localhost:64761 demo_go Sleep 0 909 root localhost:64762 demo_go Sleep 0 Thank you for your reading. The above is the content of "how to configure mysql connection pool with go". After the study of this article, I believe you have a deeper understanding of how to configure mysql connection pool with go, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report