NAME

MySQL::Hi - Credentials for MySQL/MariaDB from config files

SYNOPSIS

my $hi = MySQL::Hi->new(
    user => $user,
    config => '/path/to/config.conf'
);

# Command line options
my @options = $hi->get_options( $db, $mode );

# DSN
my ( $dsn, $user, $password ) = $hi->get_dsn();

DESCRIPTION

The module reads a config file and memorises the settings which are necessary to connect to MySQL/MariaDB. It DOES NOT perform any conections, but rather provides a convenient way to get credentials for accessing MySQL/MariaDB servers from Perl code or CLI.

The module is used in the mysqlhi script, which is a part of this distribution. The script prepares and executes mysql command for fast access to MySQL/MariaDB from command line.

It can also be used in Perl code to get DSN from a config file ready to pass to DBD::mysql driver.

METHODS

new( [user => $user,] [config => $config ] )

Creates an object.

user

Username to connect to DB. If omitted, curent username is taken.

config

Path to a cofigfile. By default it searces for the file mysqlhi.conf in the user's home directory. See "Config file" in mysqlhi for config file format.

NOTE: I only tested it on Linux, not on other operating systems. As long as it uses File::HomeDir it should, in theory, work on other OSes too. If it does not, your patches are welcome.

get_options( $db[, $mode] )

Returns a list of parameters which can be directly used for the command exec:

exec 'mysql', $hi->get_options( $db, $mode );

NOTE: exec should be used with multiple parameters. This will make sure that all parameters are passed to the command correctly, and that mysql runs directly without sh -c predicate, which, in turn, guarantees that MySQL password is not exposed in the list of processes.

The method accepts two parameters: $db (database name) and $mode (optional, used to specify credentials for a certain mode). See mysqlhi for information about modes.

get_dsn( $db[, $mode] )

Accepts the same parameter as get_credentials, returns DSN, username and password which can be used directly in DBI-connect()>:

DBI->conect( $hi->get_dsn( $db, $mode ) )
get_credentials( [ $db, $mode] ] )

If $db and $mode are provided, returns a hashref with credentials for this databade in this mode. Returns default settings otherwise:

{
    host     => 'localhost',
    port     => 3306,
    password => '',
}

BUGS

Not reported... Yet...

SEE ALSO

mysqlhi

AUTHOR

Andrei Pratasavitski <andrei.protasovitski@gmail.com>

LICENSE

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