作者:俊達(dá)
引言
MySQL是MySQL安裝包默認(rèn)的客戶端,該客戶端程序通常位于二進(jìn)制安裝包的bin目錄中,或者通過rpm安裝包安裝mysql-community-client,是數(shù)據(jù)庫管理系統(tǒng)的重要組成部分。MySQL客戶端不僅僅是一個(gè)簡單的軟件工具,更是連接用戶與數(shù)據(jù)庫之間的橋梁,對(duì)于有效地使用MySQL數(shù)據(jù)庫的功能和特性至關(guān)重要。熟練掌握MySQL客戶端的使用方法對(duì)于數(shù)據(jù)庫管理和數(shù)據(jù)操作具有重要意義,在接下來的內(nèi)容中,我們將介紹MySQL官方客戶端的使用方法。
1 使用mysql程序
linux終端下,輸入mysql命令登陸數(shù)據(jù)庫。如果提示mysql不存在,要看mysql程序是否在命令行的搜索路徑。
[root@box3 ~]# mysql
-bash: mysql: No such file or directory
### 測(cè)試環(huán)境中,mysql位于/usr/local/mysql, 設(shè)置PATH
[root@box3 ~]# cat ~/.bash_profile
...
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin
export PATH
[root@box1 ~]# mysql --version
mysql Ver 14.14 Distrib 5.7.32, for Linux (x86_64) using EditLine wrapper
2 mysql主要命令行參數(shù)說明
命令行參數(shù) | 說明 |
---|---|
--help | 查看幫助信息。mysql --help |
--version | 查看客戶端版本 |
-p, --password | 用戶密碼,小寫的p |
-P, --port | 服務(wù)端口,大寫的P |
-u, --user | 用戶名 |
-h, --host | 服務(wù)地址 |
-S, --socket | socket文件。和-S和 -h -P只能使用其中一種方式登陸。 |
-v, --verbose | 輸出更多提示信息,使用-vvv 輸出更多信息。在批量執(zhí)行腳本時(shí),我們可能會(huì)需要增加輸出信息,便于查看腳本執(zhí)行過程中的信息。 |
-s, --silent | 安靜模式,輸出更少的提示信息 |
-B, --batch | 批模式,輸出更少提示信息。會(huì)影響wait_timeout參數(shù)。 |
-e | 執(zhí)行sql語句。mysql -e 'select now()' |
-A, --no-auto-rehash | 登陸時(shí)不讀取mysql數(shù)據(jù)字典。在數(shù)據(jù)庫中表多的時(shí)候,使用-A選項(xiàng)能提高連接的速度。 |
--tee | 將命令和結(jié)果輸出到文件 |
--no-defaults | 不從默認(rèn)的配置文件中讀取選項(xiàng) |
--defaults-file | 從指定的配置文件中讀取選項(xiàng) |
--ssl-mode | ssl連接模式 |
required: 啟用ssl | |
verify_ca:驗(yàn)證服務(wù)端證書 | |
verify_identity:驗(yàn)證服務(wù)端身份 | |
disabled:不啟用ssl | |
--ssl-ca | ssl CA證書。服務(wù)端和客戶端使用同一個(gè)ca證書 |
--ssl-cert | 客戶端ssl證書 |
--ssl-key | 客戶端ssl私鑰 |
3 關(guān)于mysql客戶端空閑超時(shí)
mysql服務(wù)端會(huì)終止空閑時(shí)間超過一定時(shí)長的會(huì)話,有2個(gè)參數(shù)會(huì)影響超時(shí)時(shí)間。空閑時(shí)間是指客戶端沒有發(fā)送請(qǐng)求到服務(wù)端的連續(xù)時(shí)間。
- interactive-timeout
交互模式下的連接空閑超時(shí)時(shí)間。使用mysql client連接是,超時(shí)時(shí)間會(huì)設(shè)置為interactive-timeout指定的值。
- wait-timeout
非交互模式下的連接空閑超時(shí)時(shí)間。如果登陸時(shí)設(shè)置了交互模式,wait-timeout會(huì)設(shè)置為interactive-timeout指定的值
測(cè)試
1、先分別設(shè)置interactive-timeout和wait-timeout參數(shù)
mysql> set global wait_timeout=8000;
Query OK, 0 rows affected (0.00 sec)
mysql> set global interactive_timeout=1000;
Query OK, 0 rows affected (0.00 sec)
2、在交互式客戶端查看超時(shí)時(shí)間。wait_timeout被設(shè)置為interactive_timout的值。
[root@box3 ~]# mysql -udemo -h172.16.20.51 -pdemo -s -t
mysql> show variables where variable_name in ('wait_timeout', 'interactive_timeout');
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| interactive_timeout | 1000 |
| wait_timeout | 1000 |
+---------------------+-------+
3、使用非交互式客戶端連接,查看超時(shí)時(shí)間。使用客戶端參數(shù)-B指定當(dāng)前會(huì)話為非交互式會(huì)話。
[root@box3 ~]# mysql -udemo -h172.16.20.51 -pdemo -s -t -B
show variables where variable_name in ('wait_timeout', 'interactive_timeout');
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| interactive_timeout | 1000 |
| wait_timeout | 8000 |
wait_timeout的值還是8000。
4、每個(gè)會(huì)話可以設(shè)置各自的超時(shí)時(shí)間
mysql> show variables where variable_name in ('wait_timeout', 'interactive_timeout');
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| interactive_timeout | 1000 |
| wait_timeout | 1000 |
+---------------------+-------+
mysql> set session wait_timeout=256;
mysql> show variables where variable_name in ('wait_timeout', 'interactive_timeout');
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| interactive_timeout | 1000 |
| wait_timeout | 256 |
+---------------------+-------+
5、使用各種語言的api連接到mysql,需要連接后執(zhí)行命令查看會(huì)話的空閑超時(shí)時(shí)間。
4 關(guān)于mysql client的參數(shù)文件
在linux環(huán)境下,使用mysql client會(huì)從默認(rèn)的配置文件中讀取相關(guān)參數(shù)。使用strace可以看到mysql會(huì)訪問這些文件: /etc/my.cnf, /etc/mysql/my.cnf, /usr/etc/my.cnf, /root/.my.cnf
[root@box1 ~]# strace mysql 2>&1 | grep my.cnf
stat("/etc/my.cnf", {st_mode=S_IFREG|0644, st_size=1029, ...}) = 0
open("/etc/my.cnf", O_RDONLY) = 3
stat("/etc/mysql/my.cnf", 0x7ffc29961a00) = -1 ENOENT (沒有那個(gè)文件或目錄)
stat("/usr/etc/my.cnf", 0x7ffc29961a00) = -1 ENOENT (沒有那個(gè)文件或目錄)
stat("/root/.my.cnf", 0x7ffc29961a00) = -1 ENOENT (沒有那個(gè)文件或目錄)
測(cè)試在/root/.my.cnf中配置相關(guān)連接參數(shù),mysql client程序會(huì)自動(dòng)從文件中讀取相關(guān)選項(xiàng)。使用--no-defaults參數(shù)避免從默認(rèn)參數(shù)文件中讀取命令行選項(xiàng)。
[root@box1 ~]# cat /root/.my.cnf
[mysql]
user=demo
password=demo
[root@box1 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
...
mysql> show grants;
+-------------------------------------------+
| Grants for demo@% |
+-------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'demo'@'%' |
+-------------------------------------------+
1 row in set (0.00 sec)
mysql> ^DBye
[root@box1 ~]# mysql --no-defaults
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
5 mysql client程序使用場(chǎng)景
批量執(zhí)行保存在文件中的SQL
(1)以列的方式展現(xiàn)查詢數(shù)據(jù)
sql語句以\G結(jié)尾,將行格式的數(shù)據(jù)轉(zhuǎn)換成按列顯示。
mysql> select user, host from mysql.user limit 1;
+-------+------+
| user | host |
+-------+------+
| auser | % |
+-------+------+
1 row in set (0.01 sec)
mysql> select user, host from mysql.user limit 1\G
*************************** 1. row ***************************
user: auser
host: %
1 row in set (0.00 sec)
(2)使用show warnings查看warning信息
有時(shí)候執(zhí)行SQL會(huì)有warnings,執(zhí)行show warnings命令可以查看具體的信息文章來源:http://www.zghlxwxcb.cn/news/detail-780583.html
mysql> set sql_mode='';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show warnings\G
*************************** 1. row ***************************
Level: Warning
Code: 3090
Message: Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release.
1 row in set (0.00 sec)
(3)使用pager過濾查詢的數(shù)據(jù)
mysql客戶端中可以使用pager過濾數(shù)據(jù)。類似于通過管道將sql的輸出發(fā)送給pager指定的命令進(jìn)行處理。文章來源地址http://www.zghlxwxcb.cn/news/detail-780583.html
mysql> pager more
PAGER set to 'more'
mysql> select * from mysql.user\G
*************************** 1. row ***************************
Host: localhost
User: root
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
20 rows in set (0.00 sec)
mysql> pager
Default pager wasnt set, using stdout.
mysql> pager grep "inserts/s"
PAGER set to 'grep "inserts/s"'
mysql> show engine innodb status\G
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
1 row in set (0.00 sec)
mysql> pager
Default pager wasnt set, using stdout.
到了這里,關(guān)于MySQL運(yùn)維實(shí)戰(zhàn)(3.1) MySQL官方客戶端使用介紹的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!