shell编程系列23--shell操作数据库实战之mysql命令参数详解

shell编程系列23--shell操作数据库实战之mysql命令参数详解

shell编程系列23--shell操作数据库实战之mysql命令参数详解

    mysql命令参数详解

    -u    用户名
    -p    用户密码
    -h    服务器ip地址
    -D    连接的数据库
    -N    不输出列信息
    -B    使用tab键代替默认交互分隔符
    -e    执行sql语句

    其他选项
    -E    垂直输出
    -H    以HTML格式输出
    -X    以XML格式输出

1、写一个脚本,该脚本可以接收一个参数,参数为需要执行的SQL语句

2、查询MYSQL任意表的数据,并将查询到的结果保存到HTML文件中

3、查询MYSQL任意表的数据,并将查询到的结果保存到XML文件中

常见操作
[root@localhost shell]# cat operate_mysql.sh 
#!/bin/bash
#

user="dbuser"
password="123456"
host="10.11.0.215"
db_name="$1"

SQL="$2"

mysql -h"$host" -u"$user" -p"$password" -D"$1" -B -e "$SQL"

[root@localhost shell]# sh operate_mysql.sh school "select * from score"
s_id    c_id    s_score
1001    1001    80
1001    1002    90
1001    1003    99
1002    1001    70
1002    1002    60
1002    1003    80
1003    1001    80
1003    1002    80
1003    1003    80
1004    1001    50
1004    1002    30
1004    1003    20
1005    1001    76
1005    1002    87
1006    1001    31
1006    1002    34
1007    1001    58
1007    1002    88
[root@localhost shell]# vim operate_mysql.sh
[root@localhost shell]# sh operate_mysql.sh school "insert into score values('1020','1002','100');"
[root@localhost shell]# sh operate_mysql.sh school "select * from score"
s_id    c_id    s_score
1001    1001    80
1001    1002    90
1001    1003    99
1002    1001    70
1002    1002    60
1002    1003    80
1003    1001    80
1003    1002    80
1003    1003    80
1004    1001    50
1004    1002    30
1004    1003    20
1005    1001    76
1005    1002    87
1006    1001    31
1006    1002    34
1007    1001    58
1007    1002    88
1020    1002    100

# 导出txt文本,-B去掉多余的符号可以导入到excel表格中
# sh operate_mysql.sh school "select * from score" > result.txt

 
   
posted @ 2019-06-13 16:14 reblue520 阅读( ...) 评论( ...) 编辑 收藏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值