• 用脚本完成mysql工作


    1. 用mysql -e在脚本中执行mysql的sql语句

    #!/bin/bash
    #simple mysql shell usage
    
    logtime=`date "+%Y-%m-%d"`
    LOG=call_sql_${logtime}.log
    echo "Start execute sql statement at `date`" >>${LOG}
    
    #execute sql stat
    mysql -u root -p19930309 -e "
    tee /tmp/temp.log
    use test
    drop table if exists stu;
    create table  stu(name varchar(20),age int);
    insert into stu values('wangkun',12),('amei',12),('Jack',14);
    select * from stu;
    notee
    quit
    "
    echo -e "
    " >> ${LOG}
    echo "below is output result :" >> ${LOG}
    cat /tmp/temp.log >> ${LOG}
    echo "scrtip execute successful." >> ${LOG}
    exit

      执行结果

      

    Start execute sql statement at Mon Apr 18 23:23:01 CST 2016
    
    
    below is output result :
    +---------+------+
    | name    | age  |
    +---------+------+
    | wangkun |   12 |
    | amei    |   12 |
    | Jack    |   14 |
    +---------+------+
    scrtip execute successful.

    2.通过管道符号

     select2.sql

    tee /home/hadoop_admin/mysql.log
    use test
    drop table if exists stu;
    create table  stu(name varchar(20),age int);
    insert into stu values('wangkun',12),('amei',12),('Jack',14);
    select * from stu;
    notee
    quit

      

    [hadoop_admin@master mysql_shell]$ mysql -u root -p19930309 < ./select2.sql
    Warning: Using a password on the command line interface can be insecure.
    Logging to file '/home/hadoop_admin/mysql.log'
    name    age
    wangkun 12
    amei    12
    Jack    14
    Outfile disabled.

    3.命令行单独调用sql文件

     select2.sql

    tee /home/hadoop_admin/mysql.log
    use test
    drop table if exists stu;
    create table  stu(name varchar(20),age int);
    insert into stu values('wangkun',12),('amei',12),('Jack',14);
    select * from stu;
    notee
    quit
    [hadoop_admin@master mysql_shell]$ mysql -u root -p19930309 -e "source select2.sql"
    Warning: Using a password on the command line interface can be insecure.
    Logging to file '/home/hadoop_admin/mysql.log'
    +---------+------+
    | name    | age  |
    +---------+------+
    | wangkun |   12 |
    | amei    |   12 |
    | Jack    |   14 |
    +---------+------+
    Outfile disabled.

    4. shell脚本中MySQL提示符下调用SQL , oracle 也可以这样干

      select3.sh

      

    #!/bin/bash
    mysql -u root -p19930309 <<EOF
    tee /home/hadoop_admin/mysql.log
    use test
    drop table if exists stu;
    create table  stu(name varchar(20),age int);
    insert into stu values('wangkun',12),('amei',12),('Jack',14);
    select * from stu;
    notee
    quit
    EOF
    exit

      执行情况

      

    [hadoop_admin@master mysql_shell]$ ./select3.sh
    Warning: Using a password on the command line interface can be insecure.
    Logging to file '/home/hadoop_admin/mysql.log'
    name    age
    wangkun 12
    amei    12
    Jack    14
    Outfile disabled.
  • 相关阅读:
    Myeclipse 找不到Convert to maven project选项
    myeclipse中disable maven nature
    运行Maven是报错:No goals have been specified for this build
    Maven 工程错误Failure to transfer org.codehaus.plexus:plexus-io:pom:1.0,Failure to transfer org.codehaus.plexus:plexus-archiver:jar:2.0.1
    -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.
    新建web工程Jdk怎么不是自己安装的, 是自带的
    AGC024B Backfront
    AGC037C Numbers on a Circle
    如何看待现在的教育?自己以后怎么做!!!!
    当局者迷
  • 原文地址:https://www.cnblogs.com/linux-wangkun/p/5426516.html
Copyright © 2020-2023  润新知