gpt4 book ai didi

mysql - SetMaxOpenConns() 没有解决 `Error 1040: Too many connections`

转载 作者:IT王子 更新时间:2023-10-29 01:47:47 28 4
gpt4 key购买 nike

我将 golang 与 Mysql 结合使用。我正在使用 apache 基准测试工具 测试服务器上的负载。我收到太多连接错误。我读了this post .因此,我在我的代码中添加了 SetMaxOpenConns(100)。我仍然收到太多连接错误。

我正在做以下查询

ab -n 1000 -k -c 20 -p post.txt -T application/x-www-form-urlencoded http://localhost:8084/abcd

注意: post.txt 文件包含 35 个 id(整数类型)的数组。这是我的主要功能:

db, err := models.NewDB("root:@/rules")
if err != nil {
panic(err)
}

db.SetMaxOpenConns(100)
http.Handle("/abcd", getReq(db))

log.Fatal(http.ListenAndServe(":8084", nil))

我在 go routine 中查询这个函数以获取所有 id。

func getRuleforProduct(db *sql.DB, id int) map[int]string {
m := make(map[int]string)
var res string
err := db.QueryRow("select rules from table where product_id = ?", id).Scan(&res)
checkError(err)
m[id] = res
return m
}

即使每秒请求较少,如何解决此问题。我希望代码至少适用于 20 个并发请求。

最佳答案

要更新开放连接限制并查看该限制,您只需登录到 mysql 并执行以下命令。

mysql> SHOW VARIABLES LIKE 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 90 |
+-----------------+-------+
1 row in set (0.00 sec)

下面的命令会将打开连接的限制设置为 1000,可以根据您的需要进行更改。

mysql> SET GLOBAL max_connections = 1000;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES LIKE 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 90 |
+-----------------+-------+
1 row in set (0.00 sec)

作为引用,您可以从这里转到mysql 官方链接mysql link

关于mysql - SetMaxOpenConns() 没有解决 `Error 1040: Too many connections`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39226889/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com