mysql安装完毕并初始化之后,就可以启动了。连接进入mysql之后,可以操作mysql。对于初学者或者对命令不熟悉的人,需要一个命令提示的辅助工具。现在介绍两种工具:mysql自带的auto-rehash和印度人开发的mycli
一、auto-rehash
auto-rehash:读取表信息和列信息,可以在连上终端后开启tab补齐功能。
- 默认情况下,是不启动auto-rehash的
- 如果需要启动,可以有如下方法
- 写入到my.cnf文件中
[mysql]
auto-rehash
#如果不想开启,则可以使用下面的参数的任意一种
#no-auto-rehash
#skip-auto-rehash
- 客户端命令行加参数
# mysql -uroot -p -h 127.0.0.1 --auto-rehash
如果不想开启,可以按照如下方法中的一个
# mysql -uroot -p -h 127.0.0.1 --skip-auto-rehash
# mysql -uroot -p -h 127.0.0.1 --no-auto-rehash
- 在启动mysql服务器的时候,加上auto-rehash参数
mysqld_safe --user=mysql --auto-rehash &
如果不想使用auto-rehash,除了使用skip-auto-rehash和no-auto-rehash之外,还可以直接-A参数
不靠谱的auto-rehash
-
不去重
可以看到使用tab键,出现很多total,并没有去重
不靠谱的auto-rehash - 不相关
数据库中不存在某个表,却依然会给出提示
mysql> use test;
Database changed
mysql> select * from te
tee test
mysql> select * from test;
ERROR 1146 (42S02): Table 'test.test' doesn't exist
很显然,并不存在test或者tee这个表或者列
- 不及时
mysql> create table test(a int);
Query OK, 0 rows affected (0.06 sec)
mysql> create table test1(b int);
Query OK, 0 rows affected (0.06 sec)
mysql> select * from te
tee test
//////可以看到,此处并没有显示之前创建的两个表
mysql> exit
Bye
# mysql -uroot -p -h 127.0.0.1 --auto-rehash
Enter password:
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from te
tee test test.a test1 test1.b
可以看到,我在一个会话中创建了test1和test2表,但并没有立刻显示出来。而只有退出登录,再进入该数据库后,才“Reading table information for completion of table and column names”,并显示了之前创建的表
- 不准确
上面可以看到,在select * from te使用tab键后,除了显示test1和test2表之外,还显示了表的列,这个时候不应该只显示表吗?
有了上面几个原因,auto-rehash工具其实并不好用。这也难怪默认是不开启的
二、mycli工具
- Mycli 是一个 MySQL 命令行客户端工具,具有自动完成和语法突出显示功能。
- 它是由印度人基于 python 开发的一个工具,适合初学者或者对数据库熟悉但命令记不住的人群,能很好地克服记不住命令的困难。本段来自MySQL 命令行神器:mycli
- 它是使用 prompt_toolkit 库写的,需要 Python 2.7、3.3、3.4、3.5 和 3.6 的支持
- MyCLI 还支持通过 SSL 安全连接到 MySQL 服务器
安装mycli
直接使用pip install
# pip install mycli
如果没有pip,则需要先安装pip
如果遇到下面的错误
ERROR: Cannot uninstall 'configobj'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
则可以按照如下安装
# pip install --ignore-installed mycli
mycli初试
mycli初试
可以看到mycli是一个替代mysql客户端的另外一个终端,可以高亮,可以提示命令和表名、列名、历史语句,都比较精准。推荐使用。
mycli参数
# 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://my_user@my_host.com:3306/my_database
Options:
-h, --host TEXT Host address of the database.
-P, --port INTEGER Port number to use for connection. Honors
$MYSQL_TCP_PORT.
-u, --user TEXT User name to connect to the database.
-S, --socket TEXT The socket file to use for connection.
-p, --password TEXT Password to connect to the database.
--pass TEXT Password to connect to the database.
--ssh-user TEXT User name to connect to ssh server.
--ssh-host TEXT Host name to connect to ssh server.
--ssh-port INTEGER Port to connect to ssh server.
--ssh-password TEXT Password to connect to ssh server.
--ssh-key-filename TEXT Private key filename (identify file) for the
ssh connection.
--ssl-ca PATH CA file in PEM format.
--ssl-capath TEXT CA directory.
--ssl-cert PATH X509 cert in PEM format.
--ssl-key PATH X509 key in PEM format.
--ssl-cipher TEXT SSL cipher to use.
--ssl-verify-server-cert Verify server's "Common Name" in its cert
against hostname used when connecting. This
option is disabled by default.
-V, --version Output mycli's version.
-v, --verbose Verbose output.
-D, --database TEXT Database to use.
-d, --dsn TEXT Use DSN configured into the [alias_dsn]
section of myclirc file.
--list-dsn list of DSN configured into the [alias_dsn]
section of myclirc file.
-R, --prompt TEXT Prompt format (Default: "\t \u@\h:\d> ").
-l, --logfile FILENAME Log every query and its results to a file.
--defaults-group-suffix TEXT Read MySQL config groups with the specified
suffix.
--defaults-file PATH Only read MySQL options from the given file.
--myclirc PATH Location of myclirc file.
--auto-vertical-output Automatically switch to vertical output mode
if the result is wider than the terminal
width.
-t, --table Display batch output in table format.
--csv Display batch output in CSV format.
--warn / --no-warn Warn before running a destructive query.
--local-infile BOOLEAN Enable/disable LOAD DATA LOCAL INFILE.
--login-path TEXT Read this path from the login file.
-e, --execute TEXT Execute command and quit.
--help Show this message and exit.