Test::mysqld(3pm)
π Test::mysqld β mysqld runner for tests π
okay bestie, welcome to the User Contributed Perl Documentation corner of the internet, where we're looking at the Test::mysqld page from section 3pm (that's Perl module docs, iykyk). if you've ever needed a whole actual MySQL server to just... spawn for your test suite and then vanish into the void, this is literally the module for you. no cap. β¨
πΈ SYNOPSIS πΈ
use DBI;
use Test::mysqld;
use Test::More;
my $mysqld = Test::mysqld->new(
my_cnf => {
'skip-networking' => '', # no TCP socket
}
) or plan skip_all => $Test::mysqld::errstr;
plan tests => XXX;
my $dbh = DBI->connect(
$mysqld->dsn(dbname => 'test'),
);
# start_mysqlds is faster than calling Test::mysqld->new twice
my @mysqlds = Test::mysqld->start_mysqlds(
2,
my_cnf => {
'skip-networking' => '', # no TCP socket
}
) or plan skip_all => $Test::mysqld::errstr;
Test::mysqlds->stop_mysqlds(@mysqlds);
like, look at her go. three lines and you've got a database. π
π¦ DESCRIPTION π¦
Test::mysqld automatically setups a mysqld instance in a temporary directory, and destroys it when the perl script exits. that's it, that's the whole vibe β she sets it up, she cleans up after herself, she does NOT leave a mess in your tmp folder. main character behavior fr fr. π
β¨ FUNCTIONS β¨
π new
Create and run a mysqld instance. The instance is terminated when the returned object is being DESTROYed. If required programs (mysql_install_db and mysqld) were not found, the function returns undef and sets appropriate message to $Test::mysqld::errstr.
so basically: object goes out of scope, server goes bye bye. automatic. iconic. but heads up bestie β if it can't find the programs it needs, you getundefback, so always check$Test::mysqld::errstror you'll be debugging a ghost. π
π base_dir
Returns directory under which the mysqld instance is being created. The property can be set as a parameter of the new function, in which case the directory will not be removed at exit.
read that twice: if you pass base_dir in yourself, the directory does NOT get deleted when you exit. great for snooping around after the fact, less great if you're just hoarding temp dirs forever. π
π copy_data_from
If specified, uses a copy of the specified directory as the data directory of MySQL. Mysql database (which is used to store administrative information) is automatically created if necessary by invoking mysql_install_db.
want your tests to start from a specific pre-baked data dir? say less. she copies it in for you, and if theMysqladmin database isn't there yet, she'll runmysql_install_dbto build it. so helpful, honestly. π«Ά
π my_cnf
A hash containing the list of name=value pairs to be written into my.cnf. The property can be set as a parameter of the new function.
just hand her a hash of your my.cnf settings and she writes the file for you. plug and play, periodt. β
π
mysql_install_db
π
mysqld
Path to mysql_install_db script or mysqld program bundled to the mysqld distribution. If not set, the program is automatically search by looking up $PATH and other prefixed directories.
both of these are just "hey, where's the binary at". if you don't tell her, she goes hunting through $PATH and a bunch of other prefixed directories all by herself. self-sufficient queen. π₯
π§ dsn
Builds and returns dsn by using given parameters (if any). Default username depends on a server version and a socket family. Default dbname is test.
pass her params, get a dsn back. the default username is a whole situation β it changes depending on your server version AND your socket family (iykyk). the default dbname is test. πΈ
β pid
Returns process id of mysqld (or undef if not running).
undef means she's not running. that's the whole bit. π
π¦ start
Starts mysqld.
π stop
Stops mysqld.
π setup
Setups the mysqld instance.
β¨ read_log
Returns the contents of the mysqld log file.
when something goes wrong at 2am and you're like "why", this is your girl. she hands you the log contents. no more guessing. π
π start_mysqlds
Create and run some mysqld instances, and return a list of Test::mysqld.
plural servers, one call. this is the one your synopsis comment says is faster than calling Test::mysqld->new twice β and it is. π₯
π stop_mysqlds
Stop some mysqld instances.
π COPYRIGHT π
Copyright (C) 2009 Cybozu Labs, Inc. Written by Kazuho Oku.
π LICENSE π
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See <http://www.perl.com/perl/misc/Artistic.html>
and that's the whole page bestie!! this one shipped as part ofperl v5.42.2and the page itself is dated2026-07-06β so yeah, go forth, spin up infinite mysqlds, and remember to let them DESTROY themselves. slay. πβ¨π¦πΈπ π«Ά
π¦ where this page comes from
- fedora 43: perl-Test-mysqld 1.0030-7.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- fedora 44: perl-Test-mysqld 1.0030-7.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms