mysqlhi - MySQL Hop In: easy run of MySQL/MariaDB client
$ mysqlhi dbname $ mysqlhi dbname -c /path/to/config.conf $ mysqlhi dbname -e 'SHOW TABLES'
Allows to run MySQL/MariaDB client with credentials read from a config file.
The mysqlhi script reads a config file, searches for settings for the database provided as a first parameter and runs mysql with proper params.
mysqlhi
mysql
You use mysql command line tool often to connect to different schemas and/or databases. Each database has it's own permissions, passwords, etc., they can be phisycally on different servers.
Instead of typing long list of command line parameters and remembering passwords for each host/schema, you can create $HOME/mysqlhi file where you store all necessary settings to access to your databases by simply typing:
$HOME/mysqlhi
$ mysqlhi dbname
If you have replicas, you can extend your settings with modes, so for each host in the replication chain you have your own settings. In the simplest case of Master/Slave setup you can have a modeless setting for a slave and rw mode for master. When you need to access the slave you type:
rw
For accessing master you secify the mode:
$ mysqlhi dbname -m rw
There are no predefined modes, feel free to create as many as you want at your convenience.
The forst parameter is always a database name. If it is not specified, the error message will be thrown. All other parameters are optional.
Username which will be used to connect to server. By default mysqlhi uses current user name. With -u key you can override it.
-u
Config file where credentials are placed. If omitted, mysqlhi.conf from you home directory is used.
NOTE: I only tested it on Debian and Ubuntu. I have not tested it on other operation systems. It should, in theory, work on other OSes too. If it does not, your patches are welcome.
Which connection mode to use.
Executes <SQL STATEMENT> and exits. In fact, the parameter is passed as-is to the key -e of the mysql command.
<SQL STATEMENT>
-e
Shows the command before executing it. Can be useful for debugging your config file.
NOTE 1: A password is not being shown.
NOTE 2: The comand may not be useful for copying and pasting, because it no escaping mechanism is applied.
By default mysqlhi searches for the file mysqlhi.conf in user's home directory. You can cpecify another config file with -c parameter.
-c
The format of the config file is the following:
[dbname:mode] host=localhost port=3306 password=t0p$eCr3T
Where:
Database name, this will be used in -D parameter of mysql command.
-D
The :mode part can be omitted. Though, it can be useful to separate connections to the same schema on different boxes in replication chain.
:mode
Hostname where MySQL/MariaDB server is runing. If omitted, localhost is used
localhost
Port on the host which is used by MySQL/MariaDB for connections. By default 3306 is used.
The password for to connect to the MySQL/MariaDB server. Default value is empty string.
Not reported... Yet...
Andrei Pratasavitski <andrei.protasovitski@gmail.com>
This script is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install MySQL::Hi, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MySQL::Hi
CPAN shell
perl -MCPAN -e shell install MySQL::Hi
For more information on module installation, please visit the detailed CPAN module installation guide.