zoukankan      html  css  js  c++  java
  • 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

    
    
  • 相关阅读:
    (转)CMD指令大全
    [转]测试人员要像医生一样把要测试的程序当自己的病人一样看待一样检测!
    robotFramework学习笔记
    mysql数据库转换成数据字典的方法(整理)
    【转】PHP SQL防注入的一些经验
    性能测试基础知识(概念)
    iOS--MJRefresh的使用 上拉刷新和下拉加载
    IOS----UIScrollerView的使用
    iOS -- UILabel的高度自适应
    第二章 图像形成
  • 原文地址:https://www.cnblogs.com/reblue520/p/11017216.html
Copyright © 2011-2022 走看看