Test::mysqld(3pm)

πŸ“¦
version 1.0030 Β· 4 versions
, in fedora 43, fedora 44

πŸ’– 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 get undef back, so always check $Test::mysqld::errstr or 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 the Mysql admin database isn't there yet, she'll run mysql_install_db to 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 (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 of perl v5.42.2 and the page itself is dated 2026-07-06 β€” so yeah, go forth, spin up infinite mysqlds, and remember to let them DESTROY themselves. slay. πŸ’–βœ¨πŸ¦‹πŸŒΈπŸ’…πŸ«Ά
πŸ“¦ where this page comes from