使用语法如下:
官方手册 : https://dev.mysql.com/doc/refman/5.7/en/mysqlslap.html
[root@dbus1 ~]# mysqlslap --help
mysqlslap Ver 1.0 Distrib 5.7.33, for Linux (x86_64)
Copyright (c) 2005, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Run a query multiple times against the server.
Usage: mysqlslap [OPTIONS]
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf
The following groups are read: mysqlslap client
The following options may be given as the first argument:
--print-defaults Print the program argument list and exit.
--no-defaults Don't read default options from any option file,
except for login file.
--defaults-file=# Only read default options from the given file #.
--defaults-extra-file=# Read this file after the global files are read.
--defaults-group-suffix=#
Also read groups with concat(group, suffix)
--login-path=# Read this path from the login file.
-?, --help Display this help and exit.
-a, --auto-generate-sql
Generate SQL where not supplied by file or command line.在文件或命令行未提供的位置生成SQL
--auto-generate-sql-add-autoincrement
Add an AUTO_INCREMENT column to auto-generated tables.将自动增量列添加到自动生成的表中
--auto-generate-sql-execute-number=#
Set this number to generate a set number of queries to
run. 设置此数字以生成一组要运行的查询
--auto-generate-sql-guid-primary
Add GUID based primary keys to auto-generated tables.将基于GUID的主键添加到自动生成的表中
--auto-generate-sql-load-type=name
Specify test load type: mixed, update, write, key, or
read; default is mixed.指定测试负载类型:mixed, update, write, key, or read;
--auto-generate-sql-secondary-indexes=#
Number of secondary indexes to add to auto-generated
tables. 要添加到自动生成表中的辅助索引数
--auto-generate-sql-unique-query-number=#
Number of unique queries to generate for automatic tests. 为自动测试生成的唯一查询数
--auto-generate-sql-unique-write-number=#
Number of unique queries to generate for
auto-generate-sql-write-number. 要为自动生成sql写入编号生成的唯一查询数
--auto-generate-sql-write-number=#
Number of row inserts to perform for each thread (default
is 100). 为每个线程执行的行插入数(默认值为100)
--commit=# Commit records every X number of statements. 每X个语句提交一次记录, 多少条DML后提交一次
-C, --compress Use compression in server/client protocol. 在服务器/客户端协议中使用压缩
-c, --concurrency=name
Number of clients to simulate for query to run. 表示并发量,也就是模拟多少个客户端同时执行select。可指定多个值,以逗号或者--delimiter参数指定的值做为分隔符。例如:--concurrency=100,200,500。
--create=name File or string to use create tables. 要模拟以运行查询的客户端数。用于创建表的文件或字符串
--create-schema=name
Schema to run tests in. 代表自定义的测试库名称,测试的schema,MySQL中schema也就是database
--csv[=name] Generate CSV output to named file or to stdout if no file
is named.
-#, --debug[=#] This is a non-debug version. Catch this and exit.
--debug-check This is a non-debug version. Catch this and exit.
-T, --debug-info This is a non-debug version. Catch this and exit.
--default-auth=name Default authentication client-side plugin to use.
-F, --delimiter=name
Delimiter to use in SQL statements supplied in file or
command line.
--detach=# Detach (close and reopen) connections after X number of
requests.
--enable-cleartext-plugin
Enable/disable the clear text authentication plugin.
-e, --engine=name Storage engine to use for creating the table.代表要测试的引擎,可以有多个,用分隔符隔开。例如:--engines=myisam,innodb
-h, --host=name Connect to host.
-i, --iterations=# Number of times to run the tests.测试执行的迭代次数,代表要在不同并发环境下,各自运行测试多少次
--no-drop Do not drop the schema after the test.
-x, --number-char-cols=name
Number of VARCHAR columns to create in table if
specifying --auto-generate-sql. 自动生成的测试表中包含多少个字符类型的列,默认1
-y, --number-int-cols=name
Number of INT columns to create in table if specifying
--auto-generate-sql. 自动生成的测试表中包含多少个数字类型的列,默认1
--number-of-queries=#
Limit each client to this number of queries (this is not
exact). 总的测试查询次数(并发客户数×每客户查询次数)
--only-print Do not connect to the databases, but instead print out
what would have been done.
-p, --password[=name]
Password to use when connecting to server. If password is
not given it's asked from the tty. 密码
--plugin-dir=name Directory for client-side plugins.
-P, --port=# Port number to use for connection. 端口号
--post-query=name Query to run or file containing query to execute after
tests have completed.
--post-system=name system() string to execute after tests have completed.
--pre-query=name Query to run or file containing query to execute before
running tests.
--pre-system=name system() string to execute before running tests.
--protocol=name The protocol to use for connection (tcp, socket, pipe,
memory).
-q, --query=name Query to run or file containing query to run. 使用自定义脚本执行测试,例如可以调用自定义的一个存储过程或者sql语句来执行测试。
--secure-auth Refuse client connecting to server if it uses old
(pre-4.1.1) protocol. Deprecated. Always TRUE
-s, --silent Run program in silent mode - no output.
-S, --socket=name The socket file to use for connection.
--sql-mode=name Specify sql-mode to run mysqlslap tool.
--ssl-mode=name SSL connection mode.
--ssl Deprecated. Use --ssl-mode instead.
(Defaults to on; use --skip-ssl to disable.)
--ssl-verify-server-cert
Deprecated. Use --ssl-mode=VERIFY_IDENTITY instead.
--ssl-ca=name CA file in PEM format.
--ssl-capath=name CA directory.
--ssl-cert=name X509 cert in PEM format.
--ssl-cipher=name SSL cipher to use.
--ssl-key=name X509 key in PEM format.
--ssl-crl=name Certificate revocation list.
--ssl-crlpath=name Certificate revocation list path.
--tls-version=name TLS version to use, permitted values are: TLSv1, TLSv1.1,
TLSv1.2
--server-public-key-path=name
File path to the server public RSA key in PEM format.
--get-server-public-key
Get server public key
-u, --user=name User for login if not current user. 用户名
-v, --verbose More verbose output; you can use this multiple times to
get even more verbose output.
-V, --version Output version information and exit.
[root@dbus1 ~]#
示例语法解释:
[root@dbus1 ~]# mysqlslap -a --auto-generate-sql-load-type=read --number-of-queries 10000 -uroot -p'1234'
mysqlslap: [Warning] Using a password on the command line interface can be insecure.
Benchmark
Average number of seconds to run all queries: 1.487 seconds ==》运行所有语句的平均秒数
Minimum number of seconds to run all queries: 1.487 seconds ==》运行所有语句的最小秒数
Maximum number of seconds to run all queries: 1.487 seconds ==》运行所有语句的最大秒数
Number of clients running queries: 1 ==》运行客户端数
Average number of queries per client: 10000 ==》每个客户端运行查询的平均数
-- 1.单线程测试:
mysqlslap -a -uroot -p'1234'
-- 高负荷攻击MySQL”的数据性能报告语法:
-- 2.多线程测试。使用–concurrency来模拟并发连接,1000并发测试。
mysqlslap -a -c 1000 -uroot -p'1234'
-- 3.迭代测试。用于需要多次执行测试得到平均值。
mysqlslap -a -i 10 -uroot -p'1234'
-- 4. 综合迭代并发测试测试: 执行10次500并发测试
mysqlslap -a -i 10 -c 500 -uroot -p'1234'
-- 依次类推至5000并发,每次递增500并发
mysqlslap -a -i 10 -c 1000 -uroot -p'1234'
mysqlslap -a -i 10 -c 1500 -uroot -p'1234'
mysqlslap -a -i 10 -c 2000 -uroot -p'1234'
mysqlslap -a -i 10 -c 2500 -uroot -p'1234'
mysqlslap -a -i 10 -c 3000 -uroot -p'1234'
mysqlslap -a -i 10 -c 3500 -uroot -p'1234'
mysqlslap -a -i 10 -c 4000 -uroot -p'1234'
mysqlslap -a -i 10 -c 4500 -uroot -p'1234'
mysqlslap -a -i 10 -c 5000 -uroot -p'1234'
-- 5. 分别50和100,200个并发,执行10000次总查询,迭代测试10次
mysqlslap -a --concurrency=50,100,200 --number-of-queries 10000 --iterations=10 -uroot -p'1234'
-- 依次类推至500并发,每次递增100并发
mysqlslap -a --concurrency=300,400,500 --number-of-queries 10000 --iterations=10 -uroot -p'1234'
-- 6.分别50和100,200个并发,执行100000次总查询,迭代测试10次
mysqlslap -a --concurrency=50,100,200 --number-of-queries 100000 --iterations=10 -uroot -p'1234'
-- 依次类推至500并发,每次递增100并发
.....
-- 7. 执行100000次总查询,迭代测试10次
mysqlslap -a --auto-generate-sql-load-type=read --number-of-queries 100000 --iterations=10 -uroot -p'1234'
-- 8. 执行100000次总写入,迭代测试10次
mysqlslap -a --auto-generate-sql-load-type=write --number-of-queries 100000 --iterations=10 -uroot -p'1234'
-- 9. 执行100000次总更新,迭代测试10次
mysqlslap -a --auto-generate-sql-load-type=update --number-of-queries 100000 --iterations=10 -uroot -p'1234'
-- 10.执行100000次读,写,更新,迭代测试10次
mysqlslap -a --auto-generate-sql-load-type=read,write,update --number-of-queries 100000 --iterations=10 -uroot -p'1234'
-- 11. 执行100000次读,写,更新,迭代测试10次,分别50和100,200个并发
mysqlslap -a --auto-generate-sql-load-type=read,write,update --number-of-queries 100000 --concurrency=50,100,200 --iterations=10 -uroot -p'1234'
-- 依次类推至500并发,每次递增100并发
mysqlslap -a --auto-generate-sql-load-type=read,write,update --number-of-queries 100000 --concurrency=300,400,500 --iterations=10 -uroot -p'1234'
-- 复杂查询及全表扫描查询:
mysql> SELECT COUNT(*) FROM zhxx_six_mes.sys_log ;
+----------+
| COUNT(*) |
+----------+
| 3024896 |
+----------+
1 row in set (0.92 sec)
mysql>
-- 12. 大表数据库查询查询全表: 迭代测试10次,分别50和100,200个并发
mysqlslap -a –query="SELECT * FROM zhxx_six_mes.sys_log;" –concurrency=1000,2000,3000 --iterations=10 -uroot -p'1234'
mysqlslap -a –query="SELECT COUNT(*) FROM zhxx_six_mes.sys_log;" –concurrency=1000,2000,3000 --iterations=10 -uroot -p'1234'
-- 14. 测试同时不同的存储引擎的性能进行对比:迭代测试10次,分别1000,2000个并发
mysqlslap -a --concurrency=1000,2000 --number-of-queries 1000 --iterations=10 --engine=myisam,innodb -uroot -p'1234'