Mysql在写入压力很大,怎么办?
- 高并发下的性能最大的问题,大都在数据库,以前我们做二十万超级群,mongodb每个月都会出事故.
- 我们聊聊,高并发下如何缓解mysql的压力
- ⚠️:mysql是锁锁表不锁库,sqlite是锁库不锁表
环境准备
- Mac
- mysql
- navicat
- wrk压测工具
- node.js环境
下载wrk
brew install wrk
- 如果这里卡住,可以调整
<pre style="box-sizing: border-box; outline: none; margin: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 18px; line-height: inherit; font-family: couriernew, courier, monospace; vertical-align: baseline; color: rgb(93, 93, 93); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">
`替换brew.git:
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
替换homebrew-core.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git`
</pre>
- 再执行一次brew install wrk即可
使用Node.js连接mysql
- 先准备一个执行sql语句函数
<pre style="box-sizing: border-box; outline: none; margin: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 18px; line-height: inherit; font-family: couriernew, courier, monospace; vertical-align: baseline; color: rgb(93, 93, 93); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">
`const mysql = require('mysql');
const { MYSQL_CONF } = require('./config');
const con = mysql.createConnection(MYSQL_CONF);
//建立连接
con.connect();
//统一执行sql的方法
function exec(sql) {
const promise = new Promise((resolve, reject) => {
con.query(sql, (error, result) => {
if (error) {
reject(error);
}
resolve(result);
});
});
return promise;
}
//关闭连接
function kill() {
con.end();
}
module.exports = { exec, kill };`
</pre>
- 再准备一个配置文件 config.js(可以根据环境变量区分配置)
<pre style="box-sizing: border-box; outline: none; margin: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 18px; line-height: inherit; font-family: couriernew, courier, monospace; vertical-align: baseline; color: rgb(93, 93, 93); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">
`//获取环境变量
const env = process.env.NODE_ENV;
let MYSQL_CONF;
//开发环境的配置
MYSQL_CONF = {
host: 'localhost',
user: 'root',
password: '123456',
port: '3306',
database: 'blog',
socketPath: '/tmp/mysql.sock',
};
module.exports = { MYSQL_CONF };`
</pre>
- 如上所示,跑起来
- 首先创建测试表
<pre style="box-sizing: border-box; outline: none; margin: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 18px; line-height: inherit; font-family: couriernew, courier, monospace; vertical-align: baseline; color: rgb(93, 93, 93); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">
`exec('CREATE TABLE IF NOT EXISTS TEST_WRITE(first_column INT , second_column VARCHAR(100));')`
![](https://imgkr.cn-bj.ufileos.com/89f03976-a79d-4242-bdf0-090a53f6438c.png)
</pre>
- 通过可视化工具Navicat可以看到表已经创建成功
开始模拟写入
- 先写一个接口,用来模拟用户请求,写入数据库
<pre style="box-sizing: border-box; outline: none; margin: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 18px; line-height: inherit; font-family: couriernew, courier, monospace; vertical-align: baseline; color: rgb(93, 93, 93); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">
`app.get('/test', (req, res) => {
exec("INSERT INTO first_table(first_column, second_column) VALUES(1, 'aaa');");
res.json(success({ errcode: 0, data: {} }));
});`
</pre>
介绍下wrk
- wrk是一个用来做HTTP benchmark测试的工具。可以产生显著的压力。相比于Apache ab功能更为强大,可以使用lua脚本来支持更为复杂的测试场景,例如PUT请求等。在对于Restful架构的API接口来说,测试起来更加便捷。
- 使用方法
<pre style="box-sizing: border-box; outline: none; margin: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 18px; line-height: inherit; font-family: couriernew, courier, monospace; vertical-align: baseline; color: rgb(93, 93, 93); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">
`使用方法: wrk <选项> <被测HTTP服务的URL>
Options:
-c, --connections <N> 跟服务器建立并保持的TCP连接数量
-d, --duration <T> 压测时间
-t, --threads <N> 使用多少个线程进行压测
-s, --script <S> 指定Lua脚本路径
-H, --header <H> 为每一个HTTP请求添加HTTP头
--latency 在压测结束后,打印延迟统计信息
--timeout <T> 超时时间
-v, --version 打印正在使用的wrk的详细版本信息
<N>代表数字参数,支持国际单位 (1k, 1M, 1G)
<T>代表时间参数,支持时间单位 (2s, 2m, 2h)`
</pre>
- 目前我们服务跑在本地http://localhost://8080
- 测试下
<pre style="box-sizing: border-box; outline: none; margin: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 18px; line-height: inherit; font-family: couriernew, courier, monospace; vertical-align: baseline; color: rgb(93, 93, 93); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">
`wrk -t8 -c500 -d2s --latency "http://localhost:8080/test"`
</pre>
- 采用8个线程,500个长链接,压测2秒
- 结果:
<pre style="box-sizing: border-box; outline: none; margin: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 18px; line-height: inherit; font-family: couriernew, courier, monospace; vertical-align: baseline; color: rgb(93, 93, 93); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">
`Running 2s test @ http://localhost:8080/test
8 threads and 500 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 40.70ms 9.60ms 83.48ms 66.26%
Req/Sec 640.89 328.89 1.43k 64.29%
Latency Distribution
50% 39.26ms
75% 46.33ms
90% 54.32ms
99% 65.23ms
8980 requests in 2.08s, 3.13MB read
Socket errors: connect 253, read 201, write 0, timeout 0
Requests/sec: 4321.60
Transfer/sec: 1.50MB`
</pre>
- 2s内完成了8980个请求,3.13mb的数据读取
- 当然你也可以用lua脚本个性化测试,这里不做过度的讲解,有兴趣可以去学习下
- 数据库结果,写入成功
加大压力测试
- 加大压力测试
<pre style="box-sizing: border-box; outline: none; margin: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 18px; line-height: inherit; font-family: couriernew, courier, monospace; vertical-align: baseline; color: rgb(93, 93, 93); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">
`wrk -t15 -c1000 -d30s --latency "http://localhost:8080/test"`
</pre>
- 此时cpu打到了25%,当然我这是一台Mac pro,如果是普通的机器估计此时已经...
- 压测结果:
<pre style="box-sizing: border-box; outline: none; margin: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 18px; line-height: inherit; font-family: couriernew, courier, monospace; vertical-align: baseline; color: rgb(93, 93, 93); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">
`Running 30s test @ http://localhost:8080/test
15 threads and 1000 connections
(平均值) (标准差)(最大值)(正负一个标准差所占比例)
Thread Stats Avg Stdev Max +/- Stdev
(延迟)
Latency 35.32ms 17.38ms 345.78ms 96.45%
Req/Sec 0.95k 661.40 2.38k 54.50%
Latency Distribution
50% 33.36ms
75% 37.61ms
90% 42.49ms
99% 76.00ms
197231 requests in 30.09s, 68.65MB read
Socket errors: connect 754, read 188, write 0, timeout 0
Requests/sec: 6554.26
Transfer/sec: 2.28MB`
</pre>
- wrk使用方便,结果清晰。并且因为非阻塞IO的使用,可以在普通的测试机上创建出大量的连接,从而达到较好的压测效果。
- 当我继续调大压测的值时,出现了OOM的情况,而且我的Node.js版本还是12.x版本.
- 此时我的表字段很少,而且都是非常简单的数据,读写也没有同时进行,压力也不大,但是却先出现OOM了。
这里说明,我们的这种直接写入是有问题的,这样长时间的高频直接写入,即使数据库还能扛住,但是会很容易出现OOM,此时应该需要消息队列流量削峰,限流,也可以事务写入,但是事务写入如果失败,就默认全部失败...
数据库什么时候会出现锁库?
- 读写同时进行,高频耗时....
- 这个数据库我也不是理解很透彻
网友评论