mycli helper - more convenient to connect online MySQL through ssh tunnel

Keywords: Python MySQL Database github ssh

mycli helper

github: https://github.com/fengjx/too...

purpose

It is more convenient to use MySQL command-line client mycli to connect to remote MySQL through ssh tunnel remote server port forwarding.

The detailed usage of mycli can be found on the official website https://www.mycli.net/

Network topology

Usually, we can't connect to the online production environment database. We need a server as a transit server. We can log in to the transit server and access MySQL through the transit server

Environmental dependence

Installation dependency

mycli https://www.mycli.net/install

pip install mycli 

sshtunnel https://github.com/pahaz/ssht...

pip install sshtunnel

ps: Recommended pyenv Or other python dependency management tools to manage your dependencies

configuration file

[user-mysql]
desc=User database
remote_host=192.168.1.106
remote_port=22
remote_username=fengjx
remote_password=
remote_pkey=~/.ssh/id_rsa
remote_pkey_password=

mysql_host=192.168.1.106
mysql_port=3306
mysql_user=root
mysql_password=1234

Remote is the server configuration of ssh login agent port forwarding. Remote password, remote pkey and remote pkey password can be used to choose whether to log in with password or secret key according to the actual situation.

MySQL is a remote MySQL login configuration

Usage method

There is only one optional parameter, - c specifies the configuration file path. If it is not specified, the mycli.ini configuration file in the current directory will be used.

python mycli-helper.py -h
usage: mycli helper [-h] [-c CONFIG]

optional arguments:
  -h, --help            show this help message and exit
  -c CONFIG, --config CONFIG
                        //Configuration file, default to mycli.ini in the current directory

Example

python mycli-helper.py -c mycli.ini
//Load configuration: mycli.ini
//The MySQL instance number of the connection
[0] - user-mysql, User database
[1] - order-mysql, Order database
1
connect to order-mysql, bind local port 64069
mycli mysql://root:1234@localhost:64069
mysql 5.7.28
mycli 1.20.1
Chat: https://gitter.im/dbcli/mycli
Mail: https://groups.google.com/forum/#!forum/mycli-users
Home: http://mycli.net
Thanks to the contributor - Frederic Aoustin
mysql root@localhost:(none)>
mysql root@localhost:(none)> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
6 rows in set
Time: 0.026s
mysql root@localhost:(none)> use mydb;
You are now connected to database "mydb" as user "root"
Time: 0.004s
mysql root@localhost:mydb> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| t1             |
+----------------+
1 row in set
Time: 0.050s
mysql root@localhost:mydb>
mysql root@localhost:mydb> insert into t1(id, c) values (1, 1024);
Query OK, 1 row affected
Time: 0.013s
mysql root@localhost:mydb> select * from t1;
+----+------+
| id | c    |
+----+------+
| 1  | 1024 |
+----+------+
1 row in set
Time: 0.033s

Posted by drcdeath on Tue, 03 Dec 2019 00:37:20 -0800