linux使用mysql命令行工具_我使用过的Linux命令之mysql - MySQL客户端命令行工具
我使用過的Linux命令之mysql - MySQL客戶端命令行工具
用途說明
mysql命令是用來連接MySQL服務(wù)器并執(zhí)行用戶命令行的工具,如果使用MySQL作為數(shù)據(jù)庫,那這個命令就是經(jīng)常需要用到的了。本文只簡單講述mysql命令行的使用,以及在shell腳本中的應(yīng)用,不涉及mysql的安裝和SQL語法介紹。
常用參數(shù)
格式:mysql
使用mysql連接數(shù)據(jù)庫,只有在本機(jī)啟動了mysql服務(wù)器,訪問密碼還沒有設(shè)置的情況下才能連接成功。當(dāng)然,還有一種情況就是在/etc/my.cnf的[mysql]節(jié)配置了user和password項的時候也可以做到。
格式:mysql -p
使用當(dāng)前Linux登錄用戶連接mysql服務(wù)器,提示輸入密碼。
格式:mysql -pxxxxxx
使用當(dāng)前Linux登錄用戶連接mysql服務(wù)器,密碼為xxxxxx。
格式:mysql -uxxx -pxxxxxx
使用用戶xxx,密碼xxxxxx來連接mysql服務(wù)器。
格式:mysql -uxxx -pxxxxxx -hhostname
使用用戶xxx,密碼xxxxxx來連接運(yùn)行在由hostname指定的主機(jī)上的mysql服務(wù)器。
參數(shù): -s
安靜模式,減少輸出,比如表頭(Silent mode. Produce less output.)。
參數(shù):-r
輸出的信息不進(jìn)行轉(zhuǎn)義,如果沒有此參數(shù),某些特殊字符將會被轉(zhuǎn)義(Newline, tab, NUL, and backslash are written as \n, \t, \0, and \\.)
參數(shù):-t
輸出為表格形式(Display output in table format),在命令行方式默認(rèn)輸出為表格形式。但是作為腳本時如果要輸出為表格形式那么就必須加上此參數(shù)。
參數(shù):-H
輸出為HTML形式(Produce HTML output.)。
使用示例
示例一
[root@node34 root]# mysql
Welcome to the MySQL monitor.? Commands end with ; or \g.
Your MySQL connection id is 8 to server version: 3.23.58-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> quit
Bye
[root@node34 root]#
示例二 訪問MySQL數(shù)據(jù)庫的腳本
有時候覺得訪問mysql時總是要輸入用戶和密碼、主機(jī)之類的很煩,索性就寫一個簡單的shell腳本來訪問它。
文件:db.sh
#!/bin/sh
mysql -pxxxxxx -uroot -h192.168.6.xx exam "$@"
[root@web exam_server]# ./db.sh
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor.? Commands end with ; or \g.
Your MySQL connection id is 14687
Server version: 5.1.48-community-log MySQL Community Server (GPL)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> quit
Bye
[root@web exam_server]#
下面的命令將mysql的查詢結(jié)果輸出為HTML文本,這個可以用在shell腳本中。
[root@web exam_server]# ./db.sh -H <
show tables;
EOF
| exam_paper_info |
| exam_paper_question |
| exam_question_info |
| exam_user_answer |
| exam_user_info |
| exam_user_paper |
下面的命令將mysql的查詢結(jié)果輸出為表格形式,這個可以用在shell腳本中。注:在shell腳本中要輸出表格形式,必須加上-t參數(shù)。
[root@web exam_server]# ./db.sh -t <
>
select count(*) as "未評分?jǐn)?shù)量", count(distinct question_seq) as "未評分題數(shù)"
>
from exam_user_answer
>
where degrees is null;
>
EOF
+------------+------------+
| 未評分?jǐn)?shù)量 | 未評分題數(shù) |
+------------+------------+
|????????? 0 |????????? 0 |
+------------+------------+
[root@web exam_server]#
問題思考
相關(guān)資料
1
頂
2
踩
分享到:
2011-03-25 21:36
瀏覽 17969
評論
1 樓
107x
2014-11-03
不錯,謝謝!
總結(jié)
以上是生活随笔為你收集整理的linux使用mysql命令行工具_我使用过的Linux命令之mysql - MySQL客户端命令行工具的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VMware配置Ubuntu 编写c程序
- 下一篇: mysql怎么看端口号_mysql端口号