mysql客戶端是最常用的管理數(shù)據(jù)庫的工具。當(dāng)然除了這個(gè),還有一些圖形化的工具,比如navicat等,他們本身都非常好用。但還是建議熟悉mysql客戶端,因?yàn)閳D形化的工具離開了自己熟悉的環(huán)境,不一定有,這時(shí)候就只能依賴mysql客戶端這個(gè)黑乎乎的東西了。
一、登錄服務(wù)器方法
# mysql -uroot -p -h 127.0.0.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 8.0.17-debug Source distribution
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
以下方式都可以登錄到test庫中,數(shù)據(jù)庫名的位置非常靈活
# mysql -utest -p test
# mysql test -utest -p
# mysql -utest test -p
# mysql -utest test -p -h localhost
# mysql -utest -p -h localhost test
參數(shù)類型 | 可選項(xiàng)語法格式 | 默認(rèn)值 |
---|---|---|
Hostname | -h hostname <P> --host = hos tname | localhost |
Username | -U username Your <P>--user = username | login name |
Password | -P <P>--password | None |
- MySQL對于127.0.0.1和localhost表現(xiàn)不同
- 依照慣例,它們特殊對待主機(jī)名localhost, 這時(shí)它們會嘗試使用一個(gè)Unix domain socket文件來連接本地服務(wù)器。
- 要強(qiáng)制使用TCP/IP連接到本地服務(wù)器,那就使用IP地址127.0.0.1而不是主機(jī)名localhost。
- 可選的,也可以通過指定--protocol=tcp選項(xiàng)來強(qiáng)制使用TCP/IP進(jìn)行連接
以上可以通過tcpdump抓包驗(yàn)證。localhost是不能通過tcpdump -i lo port 3306 抓到包的,而127.0.0.1可以
不論查看權(quán)限還是刪除用戶,都需要帶登錄主機(jī)選項(xiàng)。原因在于MySQL判斷的時(shí)候是加上該字符串一起的,即root與root@localhost這兩個(gè)在mysql中含義是不一樣的。
- socket登錄
- Unix domain Socket的路徑名常常變化,通常情況為/tmp/mysql.sock
- 為顯式指定套接字文件路徑名,使用-s file_ name或--socket= file_ name選項(xiàng)
# mysql -S /data/mysql8017/datadir/mysql.sock
二、mysql客戶端參數(shù)
Usage: mysql [OPTIONS] [database] //命令方式
-?, --help //顯示幫助信息并退出
-I, --help //顯示幫助信息并退出
--auto-rehash //自動補(bǔ)全功能
-A, --no-auto-rehash //默認(rèn)狀態(tài)是沒有自動補(bǔ)全功能的。
-B, --batch //sql不使用歷史文件,禁用交互
(Enables --silent)
--character-sets-dir=name //字體集的安裝目錄
--default-character-set=name //設(shè)置數(shù)據(jù)庫的默認(rèn)字符集
-C, --compress //在客戶端和服務(wù)器端傳遞信息時(shí)使用壓縮
-#, --debug[=#] //bug調(diào)用功能
-D, --database=name //使用哪個(gè)數(shù)據(jù)庫
--delimiter=name //mysql默認(rèn)命令結(jié)束符是分號,下面有例子
-e, --execute=name //執(zhí)行mysql的sql語句
-E, --vertical //垂直打印查詢輸出
-f, --force //如果有錯(cuò)誤跳過去,繼續(xù)執(zhí)行下面的
-G, --named-commands
/*Enable named commands. Named commands mean this program's
internal commands; see mysql> help . When enabled, the
named commands can be used from any line of the query,
otherwise only from the first line, before an enter.
Disable with --disable-named-commands. This option is
disabled by default.*/
-g, --no-named-commands
/*Named commands are disabled. Use \* form only, or use
named commands only in the beginning of a line ending
with a semicolon (;) Since version 10.9 the client now
starts with this option ENABLED by default! Disable with
'-G'. Long format commands still work from the first
line. WARNING: option deprecated; use
--disable-named-commands instead.*/
-i, --ignore-spaces //忽視函數(shù)名后面的空格.
--local-infile //啟動/禁用 LOAD DATA LOCAL INFILE.
-b, --no-beep //sql錯(cuò)誤時(shí),禁止嘟的一聲
-h, --host=name //設(shè)置連接的服務(wù)器名或者Ip
-H, --html //以html的方式輸出
-X, --xml //以xml的方式輸出
--line-numbers //顯示錯(cuò)誤的行號
-L, --skip-line-numbers //忽略錯(cuò)誤的行號
-n, --unbuffered //每執(zhí)行一次sql后,刷新緩存
--column-names //查尋時(shí)顯示列信息,默認(rèn)是加上的
-N, --skip-column-names //不顯示列信息
-O, --set-variable=name //設(shè)置變量用法是--set-variable=var_name=var_value
--sigint-ignore //忽視SIGINT符號(登錄退出時(shí)Control-C的結(jié)果)
-o, --one-database //忽視除了為命令行中命名的默認(rèn)數(shù)據(jù)庫的語句。可以幫跳過日志中的其它數(shù)據(jù)庫的更新。
--pager[=name] //使用分頁器來顯示查詢輸出,這個(gè)要在linux可以用more,less等。
--no-pager //不使用分頁器來顯示查詢輸出。
-p, --password[=name] //輸入密碼
-P, --port=# //設(shè)置端口
--prompt=name //設(shè)置mysql提示符
--protocol=name //使用什么協(xié)議
-q, --quick //不緩存查詢的結(jié)果,順序打印每一行。如果輸出被掛起,服務(wù)器會慢下來,mysql不使用歷史文件。
-r, --raw //寫列的值而不轉(zhuǎn)義轉(zhuǎn)換。通常結(jié)合--batch選項(xiàng)使用。
--reconnect //如果與服務(wù)器之間的連接斷開,自動嘗試重新連接。禁止重新連接,使用--disable-reconnect。
-s, --silent //一行一行輸出,中間有tab分隔
-S, --socket=name //連接服務(wù)器的sockey文件
--ssl //激活ssl連接,不激活--skip-ssl
--ssl-ca=name //CA證書
--ssl-capath=name //CA路徑
--ssl-cert=name //X509 證書
--ssl-cipher=name //SSL cipher to use (implies --ssl).
--ssl-key=name //X509 密鑰名
--ssl-verify-server-cert //連接時(shí)審核服務(wù)器的證書
-t, --table //以表格的形勢輸出
--tee=name //將輸出拷貝添加到給定的文件中,禁時(shí)用--disable-tee
--no-tee //根--disable-tee功能一樣
-u, --user=name //用戶名
-U, --safe-updates //Only allow UPDATE and DELETE that uses keys.
-U, --i-am-a-dummy //Synonym for option --safe-updates, -U.
-v, --verbose //輸出mysql執(zhí)行的語句
-V, --version //版本信息
-w, --wait //服務(wù)器down后,等待到重起的時(shí)間
--connect_timeout=# //連接前要等待的時(shí)間
--max_allowed_packet=# //服務(wù)器接收/發(fā)送包的最大長度
--net_buffer_length=# //TCP / IP和套接字通信緩沖區(qū)大小。
--select_limit=# //使用--safe-updates時(shí)SELECT語句的自動限制
--max_join_size=# //使用--safe-updates時(shí)聯(lián)接中的行的自動限制
--secure-auth //拒絕用(pre-4.1.1)的方式連接到數(shù)據(jù)庫
--server-arg=name //Send embedded server this as a parameter.
--show-warnings //顯示警告
--column-type-info #結(jié)果集返回時(shí),同時(shí)顯示字段的類型等相關(guān)信息
-C, --compress #在客戶端和服務(wù)器端傳遞信息時(shí)使用壓縮
-D, --database=name #使用那個(gè)數(shù)據(jù)庫
-e, --execute=name #執(zhí)行 MySQL 的 SQL 語句
三、定制mysql客戶端
在my.cnf中,參數(shù)是分區(qū)塊的,比如服務(wù)器的參數(shù)放在[mysqld]下面,而客戶端的參數(shù)放在[mysql]下。那么放在[mysql]下的參數(shù),有哪些呢?各是做什么用的?
用戶名和密碼參數(shù)
[mysql]
host = localhost
user = cbuser
password = cbpasswd
如果寫在my.cnf中,下次登錄則只需要輸入mysql即可,連用戶名和密碼都不需要了。但這可能存在一些安全問題
分頁與過濾
- 分頁
默認(rèn)的情況下,mysql是不分頁的,也就是如果查詢到一個(gè)表有數(shù)千行數(shù)據(jù),會一次性顯示完全。如果想要類似于linux中的more或者less的效果,可以在my.cnf中如下配置
pager="/usr/bin/more -100"
#pager="/usr/bin/less -E"
#pager=grep sync; //后續(xù)執(zhí)行命令時(shí),后面相當(dāng)于都加上了 |grep sync;
#nopager;
也可以直接在mysql命令行中如下使用
mysql> select * from test ;\P /usr/bin/more
- 過濾
同樣,可以在my.cnf中加上grep選項(xiàng),就可以grep特定字符串了。不過該功能不是很實(shí)用
提示符
使用mysql,有時(shí)候很羨慕mariadb的客戶端,因?yàn)閙ariadb可以在提示符顯示當(dāng)前數(shù)據(jù)庫,而不必時(shí)常去select database()來查看自己所處的數(shù)據(jù)庫。其實(shí)mysql也可以做到,只需要稍微配置一下即可。有三種方法:
- 在操作系統(tǒng)命令行中輸入
# mysql --prompt '\u@mysql \r:\m:\s->'
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 31
Server version: 8.0.17-debug Source distribution
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
root@mysql 06:36:43->
- 在mysql命令行中輸入
mysql> prompt \u@mysql \r:\m:\s->
PROMPT set to '\u@mysql \r:\m:\s->'
root@mysql 06:40:42->
- 在my.cnf中配置
[mysql]
prompt=\\u@username \\r:\\m:\\s
不過一般我喜歡將提示符設(shè)置成
prompt=\\u@\d \\R:\\m >\\_
這樣可以看到當(dāng)前用戶,所處數(shù)據(jù)庫,以及執(zhí)行命令的時(shí)間。如下:
root@test 06:42 > select * from test limit 1;
這種方法是長效的,不會隨著退出mysql客戶端而失效。
- 提示符參數(shù)介紹
參數(shù) | 說明 |
---|---|
\c | 執(zhí)行的sql的序號 |
\D | 當(dāng)前日期的詳細(xì)值 |
\d | 當(dāng)前所處數(shù)據(jù)庫 |
\h | 服務(wù)器主機(jī) |
\l | 當(dāng)前分隔符 |
\m | 當(dāng)前時(shí)間的分鐘 |
\n | 換行符 |
\O | 當(dāng)前月份的三字母表示法 (Jan, Feb, …) |
\o | 當(dāng)前月份的數(shù)字表示 |
\P | am/pm |
\p | 當(dāng)前TCP/IP端口或者socket文件 |
\R | 當(dāng)前小時(shí),24小時(shí)制 |
\r | 當(dāng)前小時(shí),12小時(shí)制 |
\S | 分號 |
\s | 當(dāng)前時(shí)間的秒數(shù) |
\t | 制表符 |
\U | 包含了主機(jī)的用戶名 |
\u | 用戶名 |
\v | 服務(wù)器版本 |
\w | 當(dāng)前星期,三字符表示 |
\Y | 當(dāng)前年,4位數(shù) |
\y | 當(dāng)前年,2位數(shù) |
_ | 空格 |
\ | 空格(斜線后有一個(gè)空格) |
' | 單引號 |
" | 雙引號 |
\ | 轉(zhuǎn)義后的右斜線 |
四、mysql客戶端使用方法
本部分很多來自mysql cookbook
查看登錄選項(xiàng)
# mysql --print-defaults
或者
# my_print_defaults client mysql
注意:這就是執(zhí)行命令,不需要加其他的參數(shù)(比如-u -p之類)
取消尚未完成的sql
select test from \c
改變顯示格式
select * from test \G // 這時(shí)候不需要加分號,否則\G會單獨(dú)作為一個(gè)語句執(zhí)行
sql輸入操作技巧
; //分號
\g //結(jié)束
\c //取消
Ctrl + A //行首
Ctrl + E //行尾
>
->
\P //pager分頁
\P /usr/bin/less
\n //nopager
文件中執(zhí)行語句
# mysql -utest -p123456 test < filename.sql
> source '$file_path'
# cat file.sql | mysql test
# mysql -utest -p123456 test -e "select * from test;" //執(zhí)行單行sql
# mysql -utest -p123456 test < infile.sql > outfile.txt //執(zhí)行命令到文件中
輸出控制
替換輸出的tab成其他字符
% mysql -utest-p123456 test < infile.sql | sed -e "s/\t/:/g"
% mysql -utest-p123456 test < infile.sql | sed -e "s/\t/:/g"
以html格式輸出
% mysql -utest-p123456 test -e "select * from url" -H > 1.txt
% mysql -utest-p123456 test -e "select * from url" --html > 1.txt
以xml格式輸出 -X也可以放前面
% mysql -utest-p123456 test -e "select * from url" -X > 1.txt
% mysql -utest-p123456 test -e "select * from url" --xml > 1.txt
跳過頭部
% mysql -utest-p123456 test -e "select * from url" --skip-column-names
% mysql -utest-p123456 test -e "select * from url" -ss
控制垂直輸出(三者效果一樣)
mysql > select * from test \G
% mysql -utest-p123456 test -e "select * from url" -E
% mysql -utest-p123456 test -e "select * from url" --vertical
控制信息詳細(xì)程度,詳細(xì)輸出
% mysql -utest-p123456 test -e "select * from url" -v
% mysql -utest-p123456 test -e "select * from url" -vv
記錄交互式mysql
mysql > \T temp.out
mysql > \t
歷史命令
類似于linux的歷史命令記錄在.bash_history中,mysql的歷史命令記錄在~/.mysql_history中