分享一個MySQL命令列自動完成和補全的小工具

chenfeng發表於2019-06-26

MySQL的命令列客戶端工具mycli,可以實現命令列自動完成補全和語法突出顯示。


主頁:

文件: /docs


安裝方法:

# pip install -U mycli

Collecting mycli

  Downloading (56kB)

    100% |████████████████████████████████| 61kB 27kB/s 

Collecting PyMySQL>=0.9.2 (from mycli)

  Downloading (47kB)

    100% |████████████████████████████████| 51kB 11kB/s 

Collecting sqlparse<0.3.0,>=0.2.2 (from mycli)

  Downloading

Collecting configobj>=5.0.5 (from mycli)

  Downloading

Collecting cryptography>=1.0.0 (from mycli)

  Downloading (2.3MB)

    100% |████████████████████████████████| 2.3MB 1.3MB/s 

Collecting cli-helpers[styles]>=1.0.1 (from mycli)

  Downloading

Collecting Pygments>=1.6 (from mycli)

  Downloading (883kB)

    100% |████████████████████████████████| 890kB 1.5MB/s 

Collecting prompt-toolkit>=2.0.6 (from mycli)

  Downloading (337kB)

    100% |████████████████████████████████| 337kB 1.5MB/s 

Collecting click>=4.1 (from mycli)

  Using cached

Collecting six (from configobj>=5.0.5->mycli)

  Downloading

Collecting cffi!=1.11.3,>=1.8 (from cryptography>=1.0.0->mycli)

  Downloading (430kB)

    100% |████████████████████████████████| 440kB 1.5MB/s 

Collecting asn1crypto>=0.21.0 (from cryptography>=1.0.0->mycli)

  Using cached

Collecting tabulate[widechars]>=0.8.2 (from cli-helpers[styles]>=1.0.1->mycli)

  Downloading (46kB)

    100% |████████████████████████████████| 51kB 5.7MB/s 

Collecting terminaltables>=3.0.0 (from cli-helpers[styles]>=1.0.1->mycli)

  Downloading

Collecting wcwidth (from prompt-toolkit>=2.0.6->mycli)

  Downloading

Collecting pycparser (from cffi!=1.11.3,>=1.8->cryptography>=1.0.0->mycli)

  Using cached

Installing collected packages: PyMySQL, sqlparse, six, configobj, pycparser, cffi, asn1crypto, cryptography, wcwidth, tabulate, terminaltables, Pygments, cli-helpers, prompt-toolkit, click, mycli

  Running setup.py install for configobj ... done

  Running setup.py install for pycparser ... done

  Running setup.py install for tabulate ... done

  Running setup.py install for terminaltables ... done

Successfully installed PyMySQL-0.9.3 Pygments-2.4.2 asn1crypto-0.24.0 cffi-1.12.3 cli-helpers-1.2.1 click-7.0 configobj-5.0.6 cryptography-2.7 mycli-1.19.0 prompt-toolkit-2.0.9 pycparser-2.19 six-1.12.0 sqlparse-0.2.4 tabulate-0.8.3 terminaltables-3.1.0 wcwidth-0.1.7

You are using pip version 18.1, however version 19.1.1 is available.

You should consider upgrading via the 'pip install --upgrade pip' command.

[root@test1 20190626]# 


使用舉例:

Examples:

    - mycli my_database

    - mycli -u my_user -h my_host.com my_database

    - mycli mysql://my_user@my_host.com:3306/my_database



# mycli -uroot -p -D test  -h 192.168.16.150

Password: 

mysql 5.7.24-log

mycli 1.19.0

Chat:

Mail: !forum/mycli-users

Home:

Thanks to the contributor - Dick Marinus

mysql root@192.168.16.150:test> select * from  

                                               aaa                  

                                               dsf                  

                                               dsf_old              

                                               peihy                

                                               sq_prebycollecttime  

                                               t                    

                                               t1          


mysql> select * from dsf order by rand() limit 5 \G                                                                      

***************************[ 1. row ]***************************

id   | 281730

name | <null>

***************************[ 2. row ]***************************

id   | 448833

name | <null>

***************************[ 3. row ]***************************

id   | 441833

name | <null>

***************************[ 4. row ]***************************

id   | 686905

name | <null>

***************************[ 5. row ]***************************

id   | 392884

name | <null>

5 rows in set

Time: 1.311s

mysql>  



說明:

mycli命令後面必須用-D跟資料庫名,否則會報錯:

(1049, "Unknown database


  

命令幫助資訊:

# 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.


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2648798/,如需轉載,請註明出處,否則將追究法律責任。

相關文章