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文件中
常见操作
[[email protected] 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"
[[email protected] 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
[[email protected] shell]# vim operate_mysql.sh
[[email protected] shell]# sh operate_mysql.sh school "insert into score values('1020','1002','100');"
[[email protected] 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
