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'
$ mysqlho --version
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
to connect to it.
Typical usecase
You use mysql
command line tool often to connect to different schemas and/or databases. Each database has its own permissions, passwords, etc., they can be phisycally on different servers.
Instead of typing a long list of command line parameters and remembering passwords for each host/schema, you can create $HOME/mysqlhi.cong
file where you describe the settings for all DBs you need to have CLI access and then access them as simple as that:
$ 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. For example, you may want to have access to both development and production DBs, so you can create modes prod
and dev
. Or even further with replication: prod_rw
, prod-ro
, dev:rw
, dev:ro
, and so on.
NOTE: Do not use .
in the mode because this character has special meaning in Config::Simple and it may be parsed wrongly. Any symbol that has special meaning in your shell must be escaped according to the syntax of your shell.
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=hunter2
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. See "--mode=<modename>|-m <modename>". - 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 of a user to access
dbname
on the MySQL/MariaDB server. Default value is empty string.
You can omit any of the host
, port
, in this case the default values will be used. If you omit password
, the password prompt will be shown.
If you omit all host
, port
and password
, the default parameters will used (localhost
, 3306
), the password will be prompted and a warning message will be thrown.
Command line keys
The first parameter must be a database name. If it is not specified, an error will occur. All other parameters are optional.
- --user=<username>|-u <username>
-
MySQL username. By default
mysqlhi
uses current user name. With-u
key you can override it. - --config=</path/to/config.conf>|-c </path/to/config.conf>
-
Path to the config file. If omitted, mysqlhi.conf from user's home directory is used.
NOTE: I only tested it on Linux, not on other operating 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. There can be several different hosts with the same DB name, for example, in replication chain. Or, there can be different permissions to the same DB. Modes can be used as short and/or meaningful extentions to a DB name that specify the exact way of connecting.
In your ""Config file" you can have something like this:
[db:rw] host=master.host [db:ro] host=slave.host
By specifying
-m rw
you will connect to the master host, while-m ro
will lead you to the slave one. - --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's not escaping for special characters is applied.
- --version|-v
-
Shows the version of the distribution. If used as the first command key, all following keys will be ignored.
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.