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.