安装mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#此次安装为mysql5.7版本,使用yum install mysql默认安装为mariadb

wget https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm
#下载rpm包
rpm -ivh mysql80-community-release-el7-3.noarch.rpm
#安装rpm包
yum repolist all | grep mysql
#查看包版本
yum-config-manager --disable mysql80-community
#取消mysql80-community的默认安装
yum-config-manager --enable mysql57-community
#更改为mysql57-community默认安装版本
#如不能使用使用yum-config-manager请使用yum -y install yum-utils
yum install mysql-community-server
yum -y install mysql-devel
#安装完毕

mysql使用

mysql服务

1
2
3
4
5
6
7
8
9
10
11
CentOS 7之前
service mysqld start ——启动mysql服务
service mysqld stop ——停止mysql服务
service mysqld restart ——重启mysql服务
service mysqld status ——查看mysql服务
CentOS 7.x开始,CentOS开始使用systemd服务来代替daemon
systemctl start mysqld.service ——启动mysql服务
systemctl stop mysqld.service ——停止mysql服务
systemctl restart mysqld.service ——重启mysql服务
systemctl status mysqld.service ——查看mysql状态
mysql -V --查看mysql版本

连接数据库

1
2
3
4
5
6
7
8
mysql -u root -p
#本地登陆

mysql -h主机地址 -P端口 -u用户名 -p用户密码
#远程登陆

grep "temporary password" /var/log/mysqld.log
#获取临时密码

修改默认密码

1
2
3
4
mysql> ALTER USER USER() IDENTIFIED BY 'nmk0718';
#如需弱密码请使用一下命令
mysql> set global validate_password_policy=LOW;
mysql> set global validate_password_length=6;

开启远程连接

1
2
mysql> grant all privileges on *.* to 'root'@'%' identified by 'nmk0718';
mysql> flush privileges;

退出mysql

1
mysql> exit/quit;

忘记密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
systemctl stop mysqld.service
#停止服务

vi /etc/my.cnf
#编辑配置文件在socket=/var/lib/mysql/mysql.sock下一行加入skip-grant-tables
#作用为跳过密码验证

systemctl start mysqld.service
#启动服务

mysql -u root
mysql>update mysql.user set authentication_string=password('nmk0718') where user='root';
mysql> flush privileges;
mysql> exit
#进入mysql修改密码

systemctl stop mysqld.service

vi /etc/my.cnf
#删除加入的skip-grant-tables

systemctl start mysqld.service

查看密码策略

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#查看密码策略
mysql> show variables like '%validate_password_policy%';
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| validate_password_policy | MEDIUM |
+--------------------------+--------+
1 row in set (0.00 sec)

mysql> show variables like '%validate_password_length%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| validate_password_length | 8 |
+--------------------------+-------+
1 row in set (0.00 sec)
#修改密码策略
set global validate_password_policy=0;
set global validate_password_length=1;

validate_password.policy(校验规则),取值范围[0,1,2],默认值1。0(LOW):只校验长度;1(MEDIUM):校验长度、大小写和特殊字符;2(STRONG):校验长度、大小写、特殊字符和dictionary_file

密码策略的所有参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 4 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | LOW |
| validate_password_special_char_count | 1 |
+--------------------------------------+-------+
7 rows in set (0.00 sec)

mysql常用命令

创建数据库

1
mysql> create database <数据库名> default character set <编码格式> collate <校验集>;

显示所有数据库

1
mysql> show databases;

删除数据库

1
mysql> drop database <数据库名>;

选择数据库

1
mysql> use <数据库名>;

创建表

1
mysql> create table <表名> (<字段名1> <类型1> [,..<字段名n> <类型n>]);

显示表

1
mysql> show tables;

显示表结构

1
mysql> desc <表名>;或者show columns from 表名;

删除表

1
mysql> drop table <表名>;

插入表数据

1
mysql> insert into <表名> [( <字段名1>[,..<字段名n > ])] values ( 值1 )[, ( 值n )]

查询表数据

1
2
3
4
5
6
mysql> select <目标列表表达式列表> 
from < 表名或视图名 >
where < 条件 >
group by <分组表达式>
having <条件>
order by <排序表达式>[ASC|DESC]

删除表数据

1
mysql> delete from 表名 where 表达式;

修改表数据

1
mysql> update 表名 set 字段=新值,… where 条件;

修改表名

1
mysql> rename table 原表名 to 新表名;

增加表字段

1
mysql> alter table 表名 add 字段 类型 其他;

修改字段类型

1
mysql> alter table 表名 modify 字段 旧类型 新类型;

删除字段

1
mysql> alter table 表名 drop 字段;

修改字段的注释

1
mysql> alter table `表名` modify column `id` comment '学号';

加索引

1
mysql> alter table 表名 add index 索引名 (字段名1[,字段名2 …]);

加主关键字的索引

1
mysql> alter table 表名 add primary key (字段名);

加唯一限制条件的索引

1
mysql> alter table 表名 add unique 索引名 (字段名);

删除索引

1
mysql> alter table 表名 drop index 索引名;

备份数据库

1
2
#mysqldump 在windows下进行备份需要cmd的管理员身份
mysqldump -h主机名(ip) -P端口 -u 用户名 -p密码 数据库名1 数据库名2 > 文件名.sql

备份数据库的某张表

1
mysqldump -h主机名(ip) -u 用户名 -p 数据库名 表1 表2 表3 > 文件名.sql

备份所有数据库

1
mysqldump -uroot -p --all-databases > 文件名.sql

还原数据库

1
mysqldump -uroot -p 数据库名 < 文件名.sql

删除数据库

1
mysql> drop database 数据库名

恢复数据库

1
2
3
4
#先创建同名数据库
CREATE DATABASE 数据库名;
use 数据库名;
source 备份文件的路径 文件名.sql

mysql开启binlog

查看binlog日志的状态

1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> show variables like '%log_bin%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| log_bin | OFF |
| log_bin_basename | |
| log_bin_index | |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+-------+
6 rows in set (0.00 sec)
得知 log_bin为OFF

退出mysql,修改my.cnf

1
2
3
4
server-id=1
log-bin=/var/lib/mysql/mysql-bin
# server-id表示单个结点的id,这里由于只有一个结点,所以可以把id随机指定为一个数,这里将id设置成1。若集群中有个 结点,则id不能相同
# 第二句是指定binlog日志文件的名字为mysql-bin,以及其存储路径

重启myql

1
systemctl restart mysqld

进入mysql,查看binlog日志的状态

1
2
3
4
5
6
7
8
9
10
11
12
mysql>  show variables like '%log_bin%';
+---------------------------------+--------------------------------+
| Variable_name | Value |
+---------------------------------+--------------------------------+
| log_bin | ON |
| log_bin_basename | /var/lib/mysql/mysql-bin |
| log_bin_index | /var/lib/mysql/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+--------------------------------+
6 rows in set (0.00 sec)

MySQL数据库 Too many connections

从官方文档知道Linux上面编译安装的mysql默认的连接为100个

文档:http://dev.mysql.com/doc/refman/5.0/en/too-many-connections.html

mysql官方告诉我们需要修改max_connections的值,那么我们怎么去修改呢?有两种方法

1、修改配置文件文件

修改/etc/my.cnf这个文件,在[mysqld] 中新增max_connections=N,如果你没有这个文件请从编译源码中的support-files文件夹中复制你所需要的*.cnf文件为到 /etc/my.cnf。我使用的是my-medium.cnf,中型服务器配置。例如我的[mysqld]的内容如下

1
[mysqld]` `port = 3306` `socket = ``/tmp/mysql``.sock` `skip-locking` `key_buffer = 160M` `max_allowed_packet = 1M` `table_cache = 64` `sort_buffer_size = 512K` `net_buffer_length = 8K` `read_buffer_size = 256K` `read_rnd_buffer_size = 512K` `myisam_sort_buffer_size = 8M` `max_connections=1000

由于对mysql还不是很熟悉,所以很多参数没有修改。哈哈。。

2、非使用mysqld脚本自动启动的用户。

修改MYSQLHOME/bin/mysqldsafe文件例如:/usr/local/mysql/bin/mysqldsafe这个文件grep−n‘maxconnection′

MYSQLHOME/bin/mysqldsafe文件例如:/usr/local/mysql/bin/mysqldsafe这个文件grep−n‘maxconnection′MYSQL_HOME/bin/mysqld_safe

修改对应行号的max_connections参数值

3、服务器登录mysql : mysql -u root -p

百分之九十进不去,进不去的执行重启命令 :/etc/init.d/mysql restart(centos系统)

此时重启mysql就能连接mysql了,如果还有时间,可以继续下一步,治病要治本

打开配置文件 添加一下配置 vi /etc/my.cnf

1
wait_timeout = 600` `interactive_timeout = 600

再次重启mysql即可

原理解答

mysql 默认100 连接数,超过则连不上,实际工作的连接数远远没有100,大部分在sleep

所以要么增大连接数,要么杀掉无用连接,推荐后者。

使用Navicat工具连接>服务器监控>查看进程数,杀掉进程数到100以内即可

Linux下配置mysql允许指定IP远程访问

登录数据库

1
2
3
4
5
6
7
8
9
10
11
12
[root@dev-database ~]# mysql -uroot -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10511
Server version: 5.7.32 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

查看用户表

进入mysql数据库,查看用户表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select Host,User from user;
+-----------+---------------+
| Host | User |
+-----------+---------------+
| localhost | root |
| localhost | exporter |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
8 rows in set (0.00 sec)

更新/授权用户表

1
2
3
4
5
6
7
8
9
10
11
12
//更新用户表
mysql> update user set Host = '%' where Host = 'localhost' and user = 'root';

mysql> UPDATE `user` SET `Host` = '192.168.50.*' where `Host` = 'localhost' and user = 'root';

mysql> flush privileges;


192.168.50.*是允许远程访问的IP的值,root是账户名
即,允许来自10.42.*.*的连接并使用root账户进行访问。
而IP这里,可以使用%来表示所有IP。
第二行是使设置立刻生效。

mysql 创建只读账户

1
2
3
4
5
6
7
1、创建账户 并授权SELECT查询权限。 dbname可使用*代替所有数据库

> GRANT SELECT ON dbname.* TO 'onlyread'@'%' IDENTIFIED BY "password";

2.刷新mysql权限,使用户创建、授权生效。

> FLUSH PRIVILEGES;

性能测试 —— MySQL 基准测试

主要是 4 个指标:

  • TPS :Transactions Per Second ,即数据库每秒执行的事务数,以 commit 成功次数为准。
  • QPS :Queries Per Second ,即数据库每秒执行的 SQL 数(含 insert、select、update、delete 等)。
  • RT :Response Time ,响应时间。包括平均响应时间、最小响应时间、最大响应时间、每个响应时间的查询占比。比较需要重点关注的是,前 95-99% 的最大响应时间。因为它决定了大多数情况下的短板。
  • Concurrency Threads :并发量,每秒可处理的查询请求的数量。

sysbench

sysbench 是一个模块化的、跨平台、多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况。它主要包括以下几种方式的测试:

  • CPU 性能
  • 磁盘 IO 性能
  • 调度程序性能
  • 内存分配及传输速度
  • POSIX 线程性能
  • 数据库性能(OLTP 基准测试)

目前 sysbench 主要支持 MySQL、PgSQL、Oracle 这 3 种数据库。

安装工具
1
2
curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.rpm.sh | sudo bash
sudo yum -y install sysbench

查看版本号

1
2
3
sysbench --version

sysbench 1.0.20
准备数据

需要先创建数据库sbtest

1
2
cd /usr/share/sysbench/
sysbench oltp_common.lua --time=300 --mysql-host=127.0.0.1 --mysql-port=3306 --mysql-user=root --mysql-password=nmk@0718 --mysql-db=sbtest --table-size=1000000 --tables=10 --threads=32 --events=999999999 prepare

oltp_common.lua :执行的测试脚本。 /usr/share/sysbench/ 目录下,可看到 sysbench 自带的 lua 测试脚本。

--time :最大的总执行时间,以秒为单位,默认为 10 秒。

--events :最大允许的事件个数,默认为 0 个。
应该和 --time 互相形成最大的执行时间与次数。

--mysql-host :MySQL server host 。

--mysql-port :MySQL server port 。

--mysql-user :MySQL server 账号。

--mysql-password :MySQL server 密码。

--mysql-db :MySQL Server 数据库名。

--table-size :表记录条数。

--tables :表名。

--threads :要使用的线程数,默认 1 个。

--report-interval :以秒为单位定期报告具有指定间隔的中间统计信息,默认为 0 ,表示禁用中间报告。

prepare :执行准备数据。

执行命令后,会自动生成数据库的表、和数据。如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
sysbench oltp_common.lua --time=300 --mysql-host=127.0.0.1 --mysql-port=3306 --mysql-user=root --mysql-password=nmk@0718 --mysql-db=sbtest --table-size=1000000 --tables=10 --threads=32 --events=999999999 prepare
sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)

Initializing worker threads...

Creating table 'sbtest10'...
Creating table 'sbtest9'...
Creating table 'sbtest1'...
Creating table 'sbtest4'...
Creating table 'sbtest7'...
Creating table 'sbtest3'...
Creating table 'sbtest8'...
Creating table 'sbtest5'...
Creating table 'sbtest6'...
Creating table 'sbtest2'...
Inserting 1000000 records into 'sbtest1'
Inserting 1000000 records into 'sbtest4'
Inserting 1000000 records into 'sbtest8'
Inserting 1000000 records into 'sbtest10'
Inserting 1000000 records into 'sbtest6'
Inserting 1000000 records into 'sbtest9'
Inserting 1000000 records into 'sbtest2'
Inserting 1000000 records into 'sbtest3'
Inserting 1000000 records into 'sbtest7'
Inserting 1000000 records into 'sbtest5'
Creating a secondary index on 'sbtest8'...
Creating a secondary index on 'sbtest10'...
Creating a secondary index on 'sbtest7'...
Creating a secondary index on 'sbtest1'...
Creating a secondary index on 'sbtest5'...
Creating a secondary index on 'sbtest2'...
Creating a secondary index on 'sbtest6'...
Creating a secondary index on 'sbtest3'...
Creating a secondary index on 'sbtest4'...
Creating a secondary index on 'sbtest9'...
执行测试
1
sysbench oltp_read_write.lua --time=300 --mysql-host=127.0.0.1 --mysql-port=3306 --mysql-user=root --mysql-password=nmk@0718 --mysql-db=sbtest --table-size=1000000 --tables=10 --threads=16 --events=999999999  --report-interval=10  run

oltp_read_write.lua :执行的测试脚本。此时,我们在 /usr/share/sysbench/ 下,寻找我们想要测试的场景。
oltp_read_write.lua ,表示混合读写,在一个事务中,默认比例是:select:update_key:update_non_key:delete:insert = 14:1:1:1:1 。这也是为什么,我们测试出来的 TPS 和 QPS 的比例,大概在 1:18~20 左右。相当于说,一个事务中,有 18 个读写操作。

run :执行测试。

执行后,效果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)

Running the test with following options:
Number of threads: 16
Report intermediate results every 10 second(s)
Initializing random number generator from current time


Initializing worker threads...

Threads started!

[ 10s ] thds: 16 tps: 84.35 qps: 1712.07 (r/w/o: 1199.55/342.21/170.31) lat (ms,95%): 493.24 err/s: 0.00 reconn/s: 0.00
[ 20s ] thds: 16 tps: 81.80 qps: 1636.72 (r/w/o: 1147.52/325.60/163.60) lat (ms,95%): 484.44 err/s: 0.00 reconn/s: 0.00
[ 30s ] thds: 16 tps: 67.11 qps: 1323.22 (r/w/o: 923.59/265.42/134.21) lat (ms,95%): 484.44 err/s: 0.00 reconn/s: 0.00
[ 40s ] thds: 16 tps: 69.40 qps: 1390.70 (r/w/o: 974.00/277.90/138.80) lat (ms,95%): 484.44 err/s: 0.00 reconn/s: 0.00
[ 50s ] thds: 16 tps: 64.80 qps: 1296.69 (r/w/o: 907.10/260.00/129.60) lat (ms,95%): 520.62 err/s: 0.00 reconn/s: 0.00
[ 60s ] thds: 16 tps: 57.20 qps: 1162.30 (r/w/o: 815.80/232.10/114.40) lat (ms,95%): 467.30 err/s: 0.00 reconn/s: 0.00
[ 70s ] thds: 16 tps: 67.50 qps: 1332.64 (r/w/o: 932.26/265.39/134.99) lat (ms,95%): 458.96 err/s: 0.00 reconn/s: 0.00
[ 80s ] thds: 16 tps: 75.90 qps: 1531.58 (r/w/o: 1073.65/306.12/151.81) lat (ms,95%): 493.24 err/s: 0.00 reconn/s: 0.00
[ 90s ] thds: 16 tps: 66.60 qps: 1314.38 (r/w/o: 917.29/263.90/133.20) lat (ms,95%): 511.33 err/s: 0.00 reconn/s: 0.00
[ 100s ] thds: 16 tps: 66.70 qps: 1346.71 (r/w/o: 944.91/268.40/133.40) lat (ms,95%): 442.73 err/s: 0.00 reconn/s: 0.00
[ 110s ] thds: 16 tps: 78.30 qps: 1555.41 (r/w/o: 1086.40/312.40/156.60) lat (ms,95%): 442.73 err/s: 0.00 reconn/s: 0.00
[ 120s ] thds: 16 tps: 67.00 qps: 1335.70 (r/w/o: 934.50/267.20/134.00) lat (ms,95%): 484.44 err/s: 0.00 reconn/s: 0.00
[ 130s ] thds: 16 tps: 79.90 qps: 1602.00 (r/w/o: 1121.60/320.60/159.80) lat (ms,95%): 442.73 err/s: 0.00 reconn/s: 0.00
[ 140s ] thds: 16 tps: 74.50 qps: 1492.78 (r/w/o: 1046.49/297.30/149.00) lat (ms,95%): 493.24 err/s: 0.00 reconn/s: 0.00
[ 150s ] thds: 16 tps: 66.40 qps: 1320.31 (r/w/o: 922.11/265.40/132.80) lat (ms,95%): 502.20 err/s: 0.00 reconn/s: 0.00
[ 160s ] thds: 16 tps: 72.40 qps: 1448.70 (r/w/o: 1014.20/289.70/144.80) lat (ms,95%): 493.24 err/s: 0.00 reconn/s: 0.00
[ 170s ] thds: 16 tps: 61.20 qps: 1228.50 (r/w/o: 860.80/245.30/122.40) lat (ms,95%): 539.71 err/s: 0.00 reconn/s: 0.00
[ 180s ] thds: 16 tps: 71.10 qps: 1421.40 (r/w/o: 994.90/284.30/142.20) lat (ms,95%): 493.24 err/s: 0.00 reconn/s: 0.00
[ 190s ] thds: 16 tps: 85.50 qps: 1728.80 (r/w/o: 1211.20/346.60/171.00) lat (ms,95%): 475.79 err/s: 0.00 reconn/s: 0.00
[ 200s ] thds: 16 tps: 83.10 qps: 1642.10 (r/w/o: 1148.00/327.90/166.20) lat (ms,95%): 484.44 err/s: 0.00 reconn/s: 0.00
[ 210s ] thds: 16 tps: 73.40 qps: 1481.79 (r/w/o: 1040.30/294.70/146.80) lat (ms,95%): 530.08 err/s: 0.00 reconn/s: 0.00
[ 220s ] thds: 16 tps: 66.00 qps: 1302.51 (r/w/o: 908.31/262.20/132.00) lat (ms,95%): 484.44 err/s: 0.00 reconn/s: 0.00
[ 230s ] thds: 16 tps: 78.00 qps: 1560.70 (r/w/o: 1092.70/312.00/156.00) lat (ms,95%): 442.73 err/s: 0.00 reconn/s: 0.00
[ 240s ] thds: 16 tps: 80.20 qps: 1624.76 (r/w/o: 1139.97/324.39/160.40) lat (ms,95%): 493.24 err/s: 0.00 reconn/s: 0.00
[ 250s ] thds: 16 tps: 85.50 qps: 1692.85 (r/w/o: 1183.14/338.71/171.01) lat (ms,95%): 502.20 err/s: 0.00 reconn/s: 0.00
[ 260s ] thds: 16 tps: 81.50 qps: 1627.90 (r/w/o: 1139.10/325.80/163.00) lat (ms,95%): 511.33 err/s: 0.00 reconn/s: 0.00
[ 270s ] thds: 16 tps: 79.00 qps: 1579.80 (r/w/o: 1105.20/316.60/158.00) lat (ms,95%): 502.20 err/s: 0.00 reconn/s: 0.00
[ 280s ] thds: 16 tps: 83.38 qps: 1688.98 (r/w/o: 1185.67/336.54/166.77) lat (ms,95%): 475.79 err/s: 0.00 reconn/s: 0.00
[ 290s ] thds: 16 tps: 71.51 qps: 1412.67 (r/w/o: 986.29/283.35/143.03) lat (ms,95%): 467.30 err/s: 0.00 reconn/s: 0.00
[ 300s ] thds: 16 tps: 87.60 qps: 1759.34 (r/w/o: 1234.26/350.19/174.89) lat (ms,95%): 493.24 err/s: 0.00 reconn/s: 0.00
SQL statistics:
queries performed:
read: 311990
write: 89140
other: 44570
total: 445700
transactions: 22285 (74.27 per sec.)
queries: 445700 (1485.31 per sec.)
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)

General statistics:
total time: 300.0677s
total number of events: 22285

Latency (ms):
min: 8.23
avg: 215.40
max: 1186.15
95th percentile: 493.24
sum: 4800079.56

Threads fairness:
events (avg/stddev): 1392.8125/8.30
execution time (avg/stddev): 300.0050/0.01

机械盘16 线程 + 每个表 100w 数据:74TPS+1485QPS+215ms延迟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
SQL statistics:
queries performed:
read: 1076334
write: 307524
other: 153762
total: 1537620
transactions: 76881 (256.22 per sec.)
queries: 1537620 (5124.31 per sec.)
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)

General statistics:
total time: 300.0601s
total number of events: 76881

Latency (ms):
min: 12.82
avg: 62.43
max: 316.03
95th percentile: 125.52
sum: 4799650.04

Threads fairness:
events (avg/stddev): 4805.0625/25.22
execution time (avg/stddev): 299.9781/0.01

SSD盘16 线程 + 每个表 100w 数据:256TPS+5124QPS+62ms延迟

清理数据
1
sysbench oltp_read_write.lua --time=300 --mysql-host=127.0.0.1 --mysql-port=3306 --mysql-user=root --mysql-password=MyNewPass4! --mysql-db=sbtest --table-size=1000000 --tables=10 --threads=16 --events=999999999  --report-interval=10  cleanup
  • cleanup :执行清理数据。

效果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@iZuf6hci646px19gg3hpuwZ sysbench]# sysbench oltp_read_write.lua --time=300 --mysql-host=127.0.0.1 --mysql-port=3306 --mysql-user=root --mysql-password=MyNewPass4! --mysql-db=sbtest --table-size=1000000 --tables=10 --threads=16 --events=999999999 --rate=0 --histogram=on  --report-interval=10  runC^C
[root@iZuf6hci646px19gg3hpuwZ sysbench]# sysbench oltp_read_write.lua --time=300 --mysql-host=127.0.0.1 --mysql-port=3306 --mysql-user=root --mysql-password=MyNewPass4! --mysql-db=sbtest --table-size=1000000 --tables=10 --threads=16 --events=999999999 --report-interval=10 cleanup
sysbench 1.0.17 (using system LuaJIT 2.0.4)

Dropping table 'sbtest1'...
Dropping table 'sbtest2'...
Dropping table 'sbtest3'...
Dropping table 'sbtest4'...
Dropping table 'sbtest5'...
Dropping table 'sbtest6'...
Dropping table 'sbtest7'...
Dropping table 'sbtest8'...
Dropping table 'sbtest9'...
Dropping table 'sbtest10'...
其他参数
  • --warmup_time :预热时间,预防冷数据对测试结果的影响。
  • 这个参数加下也是有必要的,因为线上的数据,实际是一直在跑的,不会处于冷数据的状态。
  • -rate :指定数量多少事件(事务)平均每秒钟应该执行的所有线程。0(默认)意味着无限的速率,即事件尽快执行。
  • 不是很理解这个参数,不过确实增加了这个参数,QPS 和 TPS 都有一定的提升。
  • -histogram :输出测试过程中系统响应时间的分布。
  • 增加该参数,执行结果会多一个柱状图结果。
  • percentile :在延迟统计数据中计算的百分点 (1-100),使用特殊值 0 来禁用百分比计算,默认为 95 。
mysqlslap

mysqlslap 是一个 MySQL 官方提供的压力测试工具。

比较大的优势,在于 mysqlslap 是 MySQL 官方所提供,并且提供多种引擎的性能测试。

测试过程

相比 sysbench 来说,mysqlslap 的测试过程还是比较简洁的,一个命令,即可完成整个过程。如下:

1
mysqlslap --concurrency=16,32 --iterations=3 --number-int-cols=1 --number-char-cols=2 --auto-generate-sql --auto-generate-sql-add-autoincrement --engine=innodb --number-of-queries=10000 --create-schema=sbtest2 -uroot -pMyNewPass4!
  • --concurrency :并发量,也就是模拟多少个客户端同时执行命令。可指定多个值,以逗号或者 –delimiter 参数指定的值做为分隔符
  • --iterations :测试执行的迭代次数。
  • --number-int-cols :自动生成的测试表中包含多少个数字类型的列,默认 1 。此处设置为 1 的原因是,因为我们上面 sysbench 我们生成了一个 int 类型的字段。
  • --number-char-cols :自动生成的测试表中包含多少个字符类型的列,默认 1 。此处设置为 2 的原因是,因为我们上面 sysbench 我们生成了一个 char 类型的字段。
  • --auto-generate-sql :自动生成测试表和数据。这个命令,带来的效果,就类似 sysbench 命令的 prepare 指令。
  • --auto-generate-sql-add-autoincrement :增加 auto_increment 一列。
  • 如果想看,生成的具体脚本,可以用 –only-print 指令,只打印测试语句而不实际执行。
  • --engine :创建测试表所使用的存储引擎,可指定多个。
  • --number-of-queries :总的测试查询次数(并发客户数×每客户查询次数)。
  • --create-schema :测试的 schema ,MySQL中 schema 也就是 database 数据库名。
  • -uroot -pMyNewPass4! :设置 MySQL 账号和密码。

执行命令后,效果如下图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@iZuf6hci646px19gg3hpuwZ sysbench]# mysqlslap --concurrency=16,32 --iterations=3 --number-int-cols=1 --number-char-cols=2 --auto-generate-sql --auto-generate-sql-add-autoincrement --engine=innodb --number-of-queries=10000 --create-schema=sbtest2 -uroot -pMyNewPass4!
mysqlslap: [Warning] Using a password on the command line interface can be insecure.
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.489 seconds
Minimum number of seconds to run all queries: 0.486 seconds
Maximum number of seconds to run all queries: 0.496 seconds
Number of clients running queries: 16
Average number of queries per client: 625

Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.379 seconds
Minimum number of seconds to run all queries: 0.377 seconds
Maximum number of seconds to run all queries: 0.382 seconds
Number of clients running queries: 32
Average number of queries per client: 312
  • 第一个,使用 16 个线程(客户端),平均延迟在 0.489 秒。
  • 第二个,使用 32 个线程(客户端),平均延迟在 0.379 秒。

参考文档
https://www.jianshu.com/p/d464e51aa026