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

Tables_in_exam
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

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2011-03-25 21:36

浏览 17969

评论

1 楼

107x

2014-11-03

不错,谢谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值