Instructions
Compilation and Installation
git clone https://github.com/zyguan/mysql-replay.git && cd mysql-replay
git checkout dev
go build
Offline Replay
First, use tcpdump to capture database traffic on the specified node to obtain a pcap file.
Tips: You can specify the capture time using the -G and -W parameters.
Replay the pcap files offline using the mysql-replay replay subcommand.
Replay pcap files
Usage:
mysql-replay replay [flags]
Flags:
–conn-cache-size uint packet cache size for each connection
–dry-run dry run mode (just print statements)
-h, --help help for replay
–ports ints ports to filter in (default [4000])
–speed float replay speed ratio (default 1)
–target-dsn string target dsn
Global Flags:
–log-level string log level (default “info”)
–log-output strings log output (default [stdout])
--dry-run and --target-dsn require one to be specified: dry-run outputs the SQL to be executed without actually connecting to the database for replay; target-dsn format must comply with go-sql-driver specifications.
--speed is used to control the replay speed, 1 means replay at original speed, 0.5 and 2 mean half speed and double speed respectively, 0 means full speed, i.e., as fast as possible (limited by the target db).
--ports specifies port filtering conditions, which can be understood as the bpf filter expression tcp port <port>.
Online Replay
Start the replay server using the mysql-replay serve subcommand.
Serve for replay requests
Usage:
mysql-replay serve [flags]
Flags:
--addr string address to listen on (default ":5000")
--archive-dir string directory to save dumped files (default "archives")
--conn-cache-size uint packet cache size for each connection (default 128)
--dry-run dry run mode (just print statements)
-h, --help help for serve
--meta string path to meta data (default "meta.db")
--ports ints ports to filter in (default [4000])
--speed float replay speed ratio (default 1)
--target-dsn string target dsn
Global Flags:
--log-level string log level (default "info")
--log-output strings log output (default [stdout])
Start tcpdump on the node where traffic needs to be captured, and set the post rotate action script. In the script, notify the replay server using the mysql-replay notify subcommand. Refer to the brief tcpdump instructions for more details.
Text Format Export and Replay
To facilitate exporting, anonymizing, and bringing user scenarios back for internal testing, the new version of mysql-replay supports text format export and replay (text format also supports prepared statements). Operations can be performed using text <dump|play>.
Text Format Definition
mysql-replay can parse MySQL packets in pcap into a series of logical events (MySQLEvent), currently mainly supporting Query, StmtPrepare, StmtExecute, etc., which can cover most application scenarios. The export format is: each session corresponds to a file, and each line in the file corresponds to an event. The event serialization format is similar to the following tsv:
1629093374259 0 "test"
1629093374283 3 1 "SELECT c_discount, c_last, c_credit, w_tax FROM customer, warehouse WHERE w_id = ? AND c_w_id = w_id AND c_d_id = ? AND c_id = ?"
1629093374706 2 "START TRANSACTION"
1629093374709 4 1 [iii 23 6 2255
1629093374755 2 "COMMIT"
Export Text
mysql-replay text dump [--force-start] -o <output-directory> <input-pcap-files>
Parameter Description:
–force-start By default, mysql-replay requires a session to start from the establishment of the TCP connection. If the connection already exists when capturing, it will be ignored. Specifying this option forces mysql-replay to process these connections (starting from the middle of the connection may produce some abnormal states).
The output directory, each session will generate a file, the file name is ...tsv.
The list of input pcap files. When specifying multiple files, ensure that these files are continuous.
Execute Text
mysql-replay text play [flags] <input-directory>
Flags:
–dry-run dry run mode (just print events)
–max-line-size int max line size (default 16777216)
–query-timeout duration timeout for a single query (default 1m0s)
–report-interval duration report interval (default 5s)
–speed float speed ratio (default 1)
–target-dsn string target dsn
Parameter Description:
The directory of the exported text.
dry-run Only print the SQL to be executed without actually executing it.
max-line-size By default, a single line in the input file cannot exceed 16M. Otherwise, set a larger value using this parameter.
query-timeout The timeout for a single query. mysql-replay will actively abandon queries that timeout.
report-interval The frequency of statistical output, default is one output every 5s.
speed Replay speed, 1 means original speed, 1.5 means 1.5 times speed, 0 means no speed limit.
target-dsn The target database address, see go-sql-driver for the format. Note that dbname needs to be specified.