MySQL 命令行工具mycli

MyCli 简介:

MyCli 是一个 MySQL 命令行工具,支持自动补全和语法高亮。也可用于 MariaDB 和 Percona。
参考地址如下:
https://github.com/dbcli/mycli
http://www.cnblogs.com/Jing-420-/p/5883595.html

pgcli 是一个postgresql的命令行工具,支持自动补全和语法高亮
官网地址:
https://www.pgcli.com/
https://github.com/dbcli/pgcli

mycli 使用 prompt_toolkit 编写
当你输入 SQL 关键字,数据库的表格和列时,自动补全。
语法高亮使用 Pygments.
智能补全(默认启用),会提示文本感应的(context-sensitive)补全。
SELECT FROM <tab> 只显示表格名称
SELECT
FROM users WHERE <tab> 只显示列名称。
配置文件在第一次启动时,自动创建在 ~/.myclirc
漂亮的打印表格数据。

演示环境:

[root[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
MySQL5.7.24 二进制安装

安装mycli命令:

首先安装python-pip
使用yum安装python-pip时找不到该软件包,所以我们先安装拓展源。
sudo yum -y install epel-release
然后安装python-pip
sudo yum -y install python-pip python-devel
清除一下cache
sudo yum clean all
现在安装完python-pip了,可以安装mycli了。
sudo pip install mycli

[[email protected] ~]# pip install mycli

Collecting mycli
  Downloading https://files.pythonhosted.org/packages/f9/8a/9f96f12b482ff2e1bbb1d4252e14b4a24ed37daa5b1dcd14c3d990eca9dd/mycli-1.19.0-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 61kB 288kB/s
Collecting Pygments>=1.6 (from mycli)
  Downloading https://files.pythonhosted.org/packages/6e/00/c5cb5fc7c047da4af049005d0146b3a961b1a25d9cefbbe24bf0882a11ad/Pygments-2.4.0-py2.py3-none-any.whl (882kB)
    100% |████████████████████████████████| 890kB 438kB/s
Collecting cli-helpers[styles]>=1.0.1 (from mycli)
  Downloading https://files.pythonhosted.org/packages/e6/6a/5e0e8011a9ae7807de2ea455da57cec381d04f8219f9a2a8f3b171ac3885/cli_helpers-1.2.0.tar.gz
Collecting prompt-toolkit>=2.0.6 (from mycli)
  Downloading https://files.pythonhosted.org/packages/c8/ab/10d2c114828bd20ca0d757acee37ab5a7bae588139739b24d6cd3f45f8de/prompt_toolkit-2.0.9-py2-none-any.whl (337kB)
    100% |████████████████████████████████| 337kB 410kB/s
Collecting click>=4.1 (from mycli)
  Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
    100% |████████████████████████████████| 81kB 1.8MB/s
Collecting configobj>=5.0.5 (from mycli)
  Downloading https://files.pythonhosted.org/packages/64/61/079eb60459c44929e684fa7d9e2fdca403f67d64dd9dbac27296be2e0fab/configobj-5.0.6.tar.gz
Collecting PyMySQL>=0.9.2 (from mycli)
  Downloading https://files.pythonhosted.org/packages/ed/39/15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL-0.9.3-py2.py3-none-any.whl (47kB)
    100% |████████████████████████████████| 51kB 3.4MB/s
Collecting cryptography>=1.0.0 (from mycli)
  Downloading https://files.pythonhosted.org/packages/c3/c1/cf8665c955c9393e9ff0872ba6cd3dc6f46ef915e94afcf6e0410508ca69/cryptography-2.6.1-cp27-cp27mu-manylinux1_x86_64.whl (2.3MB)
    100% |████████████████████████████████| 2.3MB 287kB/s
Collecting sqlparse<0.3.0,>=0.2.2 (from mycli)
  Downloading https://files.pythonhosted.org/packages/65/85/20bdd72f4537cf2c4d5d005368d502b2f464ede22982e724a82c86268eda/sqlparse-0.2.4-py2.py3-none-any.whl
Collecting tabulate[widechars]>=0.8.2 (from cli-helpers[styles]>=1.0.1->mycli)
  Downloading https://files.pythonhosted.org/packages/c2/fd/202954b3f0eb896c53b7b6f07390851b1fd2ca84aa95880d7ae4f434c4ac/tabulate-0.8.3.tar.gz (46kB)
    100% |████████████████████████████████| 51kB 3.1MB/s
Collecting terminaltables>=3.0.0 (from cli-helpers[styles]>=1.0.1->mycli)
  Downloading https://files.pythonhosted.org/packages/9b/c4/4a21174f32f8a7e1104798c445dacdc1d4df86f2f26722767034e4de4bff/terminaltables-3.1.0.tar.gz
Collecting backports.csv>=1.0.0 (from cli-helpers[styles]>=1.0.1->mycli)
  Downloading https://files.pythonhosted.org/packages/8e/26/a6bd68f13e0f38fbb643d6e497fc3462be83a0b6c4d43425c78bb51a7291/backports.csv-1.0.7-py2.py3-none-any.whl
Collecting wcwidth (from prompt-toolkit>=2.0.6->mycli)
  Downloading https://files.pythonhosted.org/packages/7e/9f/526a6947247599b084ee5232e4f9190a38f398d7300d866af3ab571a5bfe/wcwidth-0.1.7-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): six>=1.9.0 in /usr/lib/python2.7/site-packages (from prompt-toolkit>=2.0.6->mycli)
Collecting cffi!=1.11.3,>=1.8 (from cryptography>=1.0.0->mycli)
  Downloading https://files.pythonhosted.org/packages/8d/e9/0c8afd1579e5cf7bc0f06fbcd7cdb954cbc0baadd505973949a99337da1c/cffi-1.12.3-cp27-cp27mu-manylinux1_x86_64.whl (415kB)
    100% |████████████████████████████████| 419kB 990kB/s
Requirement already satisfied (use --upgrade to upgrade): ipaddress; python_version < "3" in /usr/lib/python2.7/site-packages (from cryptography>=1.0.0->mycli)
Collecting asn1crypto>=0.21.0 (from cryptography>=1.0.0->mycli)
  Downloading https://files.pythonhosted.org/packages/ea/cd/35485615f45f30a510576f1a56d1e0a7ad7bd8ab5ed7cdc600ef7cd06222/asn1crypto-0.24.0-py2.py3-none-any.whl (101kB)
    100% |████████████████████████████████| 102kB 1.7MB/s
Collecting enum34; python_version < "3" (from cryptography>=1.0.0->mycli)
  Downloading https://files.pythonhosted.org/packages/c5/db/e56e6b4bbac7c4a06de1c50de6fe1ef3810018ae11732a50f15f62c7d050/enum34-1.1.6-py2-none-any.whl
Collecting pycparser (from cffi!=1.11.3,>=1.8->cryptography>=1.0.0->mycli)
  Downloading https://files.pythonhosted.org/packages/68/9e/49196946aee219aead1290e00d1e7fdeab8567783e83e1b9ab5585e6206a/pycparser-2.19.tar.gz (158kB)
    100% |████████████████████████████████| 163kB 1.3MB/s
Installing collected packages: Pygments, configobj, wcwidth, tabulate, terminaltables, backports.csv, cli-helpers, prompt-toolkit, click, PyMySQL, pycparser, cffi, asn1crypto, enum34, cryptography, sqlparse, mycli
  Found existing installation: configobj 4.7.2
    DEPRECATION: Uninstalling a distutils installed project (configobj) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
    Uninstalling configobj-4.7.2:
      Successfully uninstalled configobj-4.7.2
  Running setup.py install for configobj ... done
  Running setup.py install for tabulate ... done
  Running setup.py install for terminaltables ... done
  Running setup.py install for cli-helpers ... done
  Running setup.py install for pycparser ... done
Successfully installed PyMySQL-0.9.3 Pygments-2.4.0 asn1crypto-0.24.0 backports.csv-1.0.7 cffi-1.12.3 cli-helpers-1.2.0 click-7.0 configobj-5.0.6 cryptography-2.6.1 enum34-1.1.6 mycli-1.19.0 prompt-toolkit-2.0.9 pycparser-2.19 sqlparse-0.2.4 tabulate-0.8.3 terminaltables-3.1.0 wcwidth-0.1.7
You are using pip version 8.1.2, however version 19.1.1 is available.
You should consider upgrading via the ‘pip install --upgrade pip‘ command.
[[email protected] ~]# 

显示如下表示mycl客户单命令安装成功:

[[email protected] ~]# mycli --help
Usage: mycli [OPTIONS] [DATABASE]
  A MySQL terminal client with auto-completion and syntax highlighting.
  Examples:
    - mycli my_database
    - mycli -u my_user -h my_host.com my_database
    - mycli mysql://[email protected]_host.com:3306/my_database

[[email protected] ~]# cat .my.cnf
[mysql]
user=root
password=‘654321‘
socket=/tmp/mysql.sock

[email protected] [(none)]>select user,host from mysql.user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
6 rows in set (0.01 sec)

[[email protected] ~]# mycli
(1130, u"Host ‘::1‘ is not allowed to connect to this MySQL server")

直接mycli登录报错,应该是没有给数据库授权登录账户

进行登录账户授权如下:

[email protected] [(none)]>grant all on *.* to [email protected]‘127.0.0.1‘ identified by ‘654321‘ with grant option;
Query OK, 0 rows affected, 1 warning (0.02 sec)

[email protected] [(none)]>\q

演示4种mycli登录mysql的方式:

第一种:

[[email protected] ~]# mycli -h127.0.0.1 -uroot -p‘654321‘ -P3306 test01
mysql 5.7.24-log
mycli 1.19.0
Chat: https://gitter.im/dbcli/mycli
Mail: https://groups.google.com/forum/#!forum/mycli-users
Home: http://mycli.net
Thanks to the contributor - Anonymous
mysql [email protected]:test01> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test01             |
| test02             |
+--------------------+
6 rows in set
Time: 0.025s
mysql [email protected]:test01> 

第二种:

[[email protected] ~]# mycli -p‘654321‘ mysql://[email protected]:3306/test01
mysql 5.7.24-log
mycli 1.19.0
Chat: https://gitter.im/dbcli/mycli
Mail: https://groups.google.com/forum/#!forum/mycli-users
Home: http://mycli.net
Thanks to the contributor - Zhongyang Guan
mysql [email protected]:test01> show tables;
+------------------+
| Tables_in_test01 |
+------------------+
| tutorials_tbl    |
+------------------+
1 row in set
Time: 0.026s
mysql [email protected]:test01>  

第三种:

[[email protected] ~]# mycli  mysql://root:[email protected]:3306/test01
mysql 5.7.24-log
mycli 1.19.0
Chat: https://gitter.im/dbcli/mycli
Mail: https://groups.google.com/forum/#!forum/mycli-users
Home: http://mycli.net
Thanks to the contributor - Magnus udd
mysql [email protected]:test01> \q
Goodbye!

第四种:

[[email protected] ~]#  mycli -h127.0.0.1 -uroot -p‘654321‘
mysql 5.7.24-log
mycli 1.19.0
Chat: https://gitter.im/dbcli/mycli
Mail: https://groups.google.com/forum/#!forum/mycli-users
Home: http://mycli.net
Thanks to the contributor - Ryan Smith
mysql [email protected]:(none)>  

简单安装登录演示完成

原文地址:https://blog.51cto.com/wujianwei/2393313

时间: 2024-08-25 20:43:20

MySQL 命令行工具mycli的相关文章

MySQL 命令行工具之 mysqldump

mysqldump 是MySQL的一个命令行工具,用于逻辑备份.可以将数据库和表的结构,以及表中的数据分别导出成:create database, create table, insert into的sql语句.当然还可以导出 存储过程,触发器,函数,调度事件(events).不管是程序员,还是DBA都会经常使用的一个工具. 1. mysqldump --help mysqldump 的选项很多,我们看一下他的帮助: mysqldump Ver 10.13 Distrib 5.6.26, for

MySQL命令行工具

shell> mysql --user=user_name --password=your_password db_name shell> mysql --help shell> mysql -? 选项 --protocol={TCP | SOCKET | PIPE | MEMORY} mysql 命令 有些命令mysql可以自己解释.要查看这些命令,在mysql>提示下输入help或\h 默认mysql>可以重新配置,怎么配置有哪几种方法? 1. 使用环境变量 shell&

MySQL命令行登陆

环境介绍 OS:CentOS6.X & Win2003 & Win2008 MySQL版本号:5.5.x 除了经常使用的MySQL管理工具,还有MySQLclient命令行工具经常被用到 1.Linux下安装命令行工具 1.Linuxclient安装 (1).启用epel源: (2).安装MySQL命令行工具 ## yum -y install mysql 2.命令行经常使用命令: #mysql --help 3.怎样在MySQLclient输出,主机名.数据库名, #vi /etc/my

MySql命令行命令和SQL语句

一.常用mysql命令行命令 1.启动MYSQL服务 net start mysql 停止MYSQL服务 net stop mysql 2.netstat -na|findstr 3306 查看被监听的端口,findstr用于查找后面的端口是否存在 3.在命令行中登录MYSQL控制台,即使用MYSQL COMMEND LINE TOOL 语法格式 mysql -user=root  -password=123456  db_name 或者简写格式 mysql -uroot -p123456 db

MySQL命令行登录

环境:MySQL Sever 5.1 + MySQL命令行工具 问题:MySQL命令行登录 解决: 命令行登录语法: mysql –u用户名 [–h主机名或者IP地址] –p密码 说明:用户名是你登录的用户,主机名或者IP地址为可选项,如果是本地连接则不需要,远程连接需要填写,密码是对应用户的密码. 注意:  www.2cto.com 1. 该命令是在Windows命令行窗口下执行,而不是MySQL的命令行: 2.输入-p后可以直接跟上密码,也可以按回车,会提示你输入密码,二者都是相同的效果:

MySQL命令行登录的例子

环境:MySQL Sever 5.1 + MySQL命令行工具 问题:MySQL命令行登录 解决: 命令 行登录语法: mysql –u用户名 [–h主机名或者IP地址] –p密码 说明:用户名是你登录的用 户,主机名或者IP地址为可选项,如果是本地连接则不需要,远程连接需要填写,密码是对应用户的密码. 注意: 1. 该命令是在Windows命令行窗口下执行,而不是MySQL的命令行: 2.输入-p 后可以直接跟上密码,也可以按回车,会提示你输入密码,二者都是相同的效果: 3. –p密码选项不

mysql mysqldmin 命令行工具 GUI工具

连入MySQL服务器 mysql client <--mysql protocol--> mysqld mysqld接收连接请求: 本地通信:客户端与服务器端位于同一主机,而且还要基于127.0.0.1(localhost)地址或lo接口进行通信: Linux OR Unix: Unix Sock, /tmp/mysql.sock, /var/lib/mysql/mysql.sock Windows: memory, pipe 远程通信:客户端与服务器位于不同的主机,或在同一主机便使用非回环地

mysql命令行自动补全

今天想着实现mysql命令行的自动补全功能,随便搜索就出现了使用 mysql自带的 --auto--rehash参数 mysql -uroot -p --auto--rehash 但是mariadb好像不买账,继续搜索,出现了下面的工具 mycli官网 嗯,还有mysql相关的命令 mysqladmin,一起学起来吧! 20 MySQL (Mysqladmin) Commands for Database Administration in Linux 4 Useful Commandline

mysql命令行导入sql脚本中文变问号问题

之前一直用工具连接mysql虽然小问题不断也都无伤大雅,最近做金融云项目,只能通过服务器的内网访问数据库,也就是说只能在linux下通过命令行访问,在导入中文的时候发现都变成问号了,经过查询资料解决,特此记录一下思路. 1.检查sql脚本文件是否unicode编码,打开查看是脚本文件中中文是否能正常显示. 2.检查所建好的数据库是否采用utf8编码. 3.在mysql命令行下执行status检查Server characterset和Client characterset编码是否一致(我就是这一