linux使用mysql命令行工具_我使用过的Linux命令之mysql - MySQL客户端命令行工具
我使用過的Linux命令之mysql - MySQL客戶端命令行工具
用途說明
mysql命令是用來連接MySQL服務器并執行用戶命令行的工具,如果使用MySQL作為數據庫,那這個命令就是經常需要用到的了。本文只簡單講述mysql命令行的使用,以及在shell腳本中的應用,不涉及mysql的安裝和SQL語法介紹。
常用參數
格式:mysql
使用mysql連接數據庫,只有在本機啟動了mysql服務器,訪問密碼還沒有設置的情況下才能連接成功。當然,還有一種情況就是在/etc/my.cnf的[mysql]節配置了user和password項的時候也可以做到。
格式:mysql -p
使用當前Linux登錄用戶連接mysql服務器,提示輸入密碼。
格式:mysql -pxxxxxx
使用當前Linux登錄用戶連接mysql服務器,密碼為xxxxxx。
格式:mysql -uxxx -pxxxxxx
使用用戶xxx,密碼xxxxxx來連接mysql服務器。
格式:mysql -uxxx -pxxxxxx -hhostname
使用用戶xxx,密碼xxxxxx來連接運行在由hostname指定的主機上的mysql服務器。
參數: -s
安靜模式,減少輸出,比如表頭(Silent mode. Produce less output.)。
參數:-r
輸出的信息不進行轉義,如果沒有此參數,某些特殊字符將會被轉義(Newline, tab, NUL, and backslash are written as \n, \t, \0, and \\.)
參數:-t
輸出為表格形式(Display output in table format),在命令行方式默認輸出為表格形式。但是作為腳本時如果要輸出為表格形式那么就必須加上此參數。
參數:-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數據庫的腳本
有時候覺得訪問mysql時總是要輸入用戶和密碼、主機之類的很煩,索性就寫一個簡單的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的查詢結果輸出為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的查詢結果輸出為表格形式,這個可以用在shell腳本中。注:在shell腳本中要輸出表格形式,必須加上-t參數。
[root@web exam_server]# ./db.sh -t <
>
select count(*) as "未評分數量", count(distinct question_seq) as "未評分題數"
>
from exam_user_answer
>
where degrees is null;
>
EOF
+------------+------------+
| 未評分數量 | 未評分題數 |
+------------+------------+
|????????? 0 |????????? 0 |
+------------+------------+
[root@web exam_server]#
問題思考
相關資料
1
頂
2
踩
分享到:
2011-03-25 21:36
瀏覽 17969
評論
1 樓
107x
2014-11-03
不錯,謝謝!
總結
以上是生活随笔為你收集整理的linux使用mysql命令行工具_我使用过的Linux命令之mysql - MySQL客户端命令行工具的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VMware配置Ubuntu 编写c程序
- 下一篇: 在ubuntu系统下使用gcc和make