DBIx::Conn::MySQL - Shortcut to connect to MySQL database


This document describes version 0.001 of DBIx::Conn::MySQL (from Perl distribution DBIx-Conn-MySQL), released on 2018-07-02.


In the command-line, instead of:

 % perl -MDBI -E'my $dbh = DBI->connect("dbi:mysql:database=mydb", "someuser", "somepass"); $dbh->selectrow_array("query"); ...'


 % perl -MDBIx::Connect::MySQL -E'my $dbh = DBI->connect("dbi:mysql:database=mydb"); $dbh->selectrow_array("query"); ...'

you can just:

 % perl -MDBIx::Conn::MySQL=mydb -E'$dbh->selectrow_array("query"); ...'

To connect with some DBD::mysql parameters:

 % perl -MDBIx::Conn::MySQL='database=mydb;host=;port=23306' -E'$dbh->selectrow_array("query"); ...'

To change the exported database variable name from the default '$dbh'

 % perl -MDBIx::Conn::MySQL=mydb,'$handle' -E'$handle->selectrow_array("query"); ...'

To supply username and password:

 % perl -MDBIx::Conn::MySQL=mydb,myuser,mysecret -E'$handle->selectrow_array("query"); ...'


This module offers some saving in typing when connecting to a MySQL database using DBI, and is particularly handy in one-liners. First, it uses DBIx::Connect::MySQL to connect so you don't have to supply username and password if you have configuration file (e.g. ~/.my.cnf); that module will search the username and password from configuration files.

Second, it automatically connect() and exports the database handle $dbh for you.

You often only have to specify the database name in the import argument:


This will result in the following DSN:


If you need to specify other parameters in the DSN, e.g. when the host is not localhost, or the port is not the default port, you can specify that in the first import argument too (note the quoting because the shell will interpret ; as command separator). When the first import argument contains =, the module knows that you want to specify DSN parameters:


this will result in the following DSN:


If you want to use another variable name other than the default $dbh for the database handle, you can specify this in the second import argument (note the quoting because otherwise the shell will substitute with shell variable):


Lastly, if you want to supply username and password anyway, you can do that via the third and fourth import arguments (or the second and third import arguments, as long as the username does not begin with $):


But note that specifying passwords on the command-line is not recommended (hence the use of DBIx::Connect::MySQL in the first place).


Please visit the project's homepage at


Source repository is at


Please report any bugs or feature requests on the bugtracker website

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.


perlancar <>


This software is copyright (c) 2018 by

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.