NAME
mysqlhi - MySQL Hop In: easy run of MySQL/MariaDB client
SYNOPSIS
$ mysqlhi dbname
$ mysqlhi dbname -c /path/to/config.conf
$ mysqlhi dbname -e 'SHOW TABLES'
DESCRIPTION
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.
Typical usecase
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:
$ 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:
$ mysqlhi dbname
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.
Command line keys
The forst parameter is always a database name. If it is not specified, the error message will be thrown. All other parameters are optional.
- --user=<username>|-u <username>
-
Username which will be used to connect to server. By default
mysqlhi
uses current user name. With-u
key you can override it. - --config </path/to/config.conf>|-c </path/to/config.conf>
-
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.
- --mode=<modename>|-m <modename>
-
Which connection mode to use.
- --exec='<SQL STATEMENT>'|-e '<SQL STATEMENT>'
-
Executes
<SQL STATEMENT>
and exits. In fact, the parameter is passed as-is to the key-e
of themysql
command. - --command
-
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.
Config file
By default mysqlhi
searches for the file mysqlhi.conf in user's home directory. You can cpecify another config file with -c
parameter.
The format of the config file is the following:
[dbname:mode]
host=localhost
port=3306
password=t0p$eCr3T
Where:
- dbname
-
Database name, this will be used in
-D
parameter ofmysql
command. - mode
-
The
:mode
part can be omitted. Though, it can be useful to separate connections to the same schema on different boxes in replication chain. - host
-
Hostname where MySQL/MariaDB server is runing. If omitted,
localhost
is used - port
-
Port on the host which is used by MySQL/MariaDB for connections. By default 3306 is used.
- password
-
The password for to connect to the MySQL/MariaDB server. Default value is empty string.
BUGS
Not reported... Yet...
AUTHOR
Andrei Pratasavitski <andrei.protasovitski@gmail.com>
LICENSE
This script is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.