当前位置: 首页 > 后端技术 > Python

mycli辅助工具-通过ssh隧道连接在线MySQL

时间:2023-03-26 14:10:59 Python

更方便转发命令行客户端mycli连接到远程MySQL。mycli的详细使用方法可以参考官网https://www.mycli.net/网络拓扑通常我们无法连接线上生产环境数据库,需要一台服务器作为中转。我们可以登录这个中转服务器,通过中转服务器访问MySQL环境依赖python3myclisshtunnel安装依赖myclihttps://www.mycli.net/installpipinstallmyclisshtunnelhttps://github.com/pahaz/ssht。..pipinstallsshtunnelps:推荐使用pyenv或者其他python依赖管理工具来管理你的依赖配置文件[user-mysql]desc=userdatabaseremote_host=192.168.1.106remote_port=22remote_username=fengjxremote_password=remote_pkey=~/.ssh/id_rsaremote_pkey_password=mysql_host=192.168.1.106mysql_port=3passshword4user=rootmysql_2yes对于代理端口转发的服务器配置,remote_password、remote_pkey、remote_pkey_password可以根据实际情况选择使用密码或者秘钥登录。mysql是一种远程MySQL登录配置方式,只有一个可选参数,-c指定配置文件路径,不指定则使用当前目录下的mycli.ini配置文件。pythonmycli-helper.py-husage:myclihelper[-h][-cCONFIG]可选参数:-h,--help显示此帮助信息并退出-cCONFIG,--configCONFIG配置文件,默认当前目录mycli.ini示例pythonmycli-helper.py-cmycli.iniloadconfiguration:mycli.ini情况输入要连接的MySQL实例号[0]-user-mysql,userdatabase[1]-order-mysql,orderdatabase1connectorder-mysql,绑定本地端口64069myclimysql://root:1234@localhost:64069mysql5.7.28mycli1.20.1Chat:https://gitter.im/dbcli/mycliMail:https://groups.google.com/forum/#!forum/mycli-usersHome:http://mycli.net感谢贡献者-FredericAoustinmysqlroot@localhost:(none)>mysqlroot@localhost:(none)>showdatabases;+-------------------+|数据库|+--------------------+|信息模式||数据库||数据库||性能模式||系统||测试|+--------------------+6rowsinsetTime:0.026smysqlroot@localhost:(none)>usemydb;Youa现在以用户“root”身份连接到数据库“mydb”时间:0.004smysqlroot@localhost:mydb>showtables;+----------------+|Tables_in_mydb|+----------------+|t1|+----------------setTime中的+1行:0.050smysqlroot@localhost:mydb>mysqlroot@localhost:mydb>insertintot1(id,c)values(1,1024);QueryOK,1rowaffectedTime:0.013smysqlroot@localhost:mydb>select*fromt1;+----+------+|编号|c|+----+------+|1|1024|+----+-----+1行在setTime:0.033s