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

    
    
  • 相关阅读:
    python 读取excel表格中的数据
    python 安装 pip 报错解决方案
    HDU-1150-MachineSchedule(二分图匹配)
    POJ-3177-RedundantPaths(边联通分量,缩点)
    POJ-3352-RoadConstruction(边双联通分量,缩点)
    F-三生三世
    D-温暖的签到题
    C-晾衣服
    A-坐飞机
    POJ-2777-CountColor(线段树,位运算)
  • 原文地址:https://www.cnblogs.com/reblue520/p/11017216.html
Copyright © 2011-2022 走看看