一、数据库基本操作

    1)登录mysql服务器:mysql -u root -p 密码

    2)查看数据库:show databases

    3)查看表:show tales from db;

    4)查看表结构:desc table;

    5)创建表:create table table();

    6)查看表数据:select * from table;

    7)插入数据:insert into table(f1,f2) values('v1','v2');

    8)删除数据:delete from table where id=8;

    9)修改表数据:update table t1 set f1='123',f2='abc' where id=8;

    10)删除表:drop table t1;

    11)删除数据库:drop database d1;

二、shell操作mysql之增、删、改、查

    1)连接数据库

        #!/bin/bash

        #mysql.sh

        mysql='mysql -uroot -proot'

        sql="show databases"

        $mysql -e "$sql"

      2)shell操作mysql之增、删、改、查

        

        #!/bin/bash

        #mysql1.sh

        conn="mysql -uroot -proot"

        case $1 in

        select)

        sql="查询语句"

        ;;

        delete)

        sql="删除语句"

        ;;

        insert)

        sql="插入语句"

        ;;

        update)

        sql="更新语句"

        ;;

        esac

        $conn -e "$sql"

三、apache日志分割及相关计划任务      

        #!/bin/bash

        #logcut.sh

        yesterday=`date -d yesterday +%Y%m%d`

        srclog="/usr/local/apache2.4/logs/access_log"

        dstlog="/usr/local/apache2.4/logsbak/access_${yesterday}.log"

        mv $srclog $dstlog

        pkill -HUP httpd        //pkill -1 httpd  重新读取日志文件

      crontab -e        //任务计划

        00 00 *** /mnt/logcut.sh

      crontab -l        //查看任务计划

四、apache日志统计之mysql数据库

       结合日志分割同时对日志进行统计存入数据库

        #!/bin/bash

        #logpv.sh

        yesterday=`date -d yesterday +%Y%m%d`

        srclog="/usr/local/apache2.4/logs/access_log"

        dstlog="/usr/local/apache2.4/logsbak/access_${yesterday}.log"

        mv $srclog $dstlog

        pkill -1 httpd

        cat /usr/local/apache2.4/logsbak/access_20171125.log|awk '{print $1}'|sort|uniq -c|awk '{print $1":"$2}' > a.txt    //这里的目录及文件名可以是$dstlog

        mysql="mysql -uroot -proot"

        for i in `cat a.txt`  

        do

        pv=`echo $i|awk -F: '{print $1}'`

        ip=`echo $i|awk -F: '{print $2}'`

        sql="insert into text.countab(date,ip,num) value('$yesterday','$ip','$pv')"

        $mysql -e "$sql"

        done

        rm -rf a.txt

        sql="select * from text.countab order by id"

        $mysql -e "$sql"

      crontab -e        //任务计划

        00 00 *** /mnt/logcut.sh

      crontab -l        //查看任务计划


shell脚本编程学习笔记(四)shell操作数据库的更多相关文章

  1. Linux Shell脚本编程学习笔记和实战

    http://www.1987.name/141.html shell基础 终端打印.算术运算.经常使用变量 Linux下搜索指定文件夹下特定字符串并高亮显示匹配关键词 从键盘或文件里获取标准输入 [ ...

  2. 【Linux_Shell 脚本编程学习笔记四、监控系统内存并报警企业案例脚本】

    前置知识:awk 参考学习博客:https://www.cnblogs.com/bugingcode/p/8287914.html awk 'BEGIN{ commands } pattern{ co ...

  3. shell脚本编程学习笔记(一)

    一.脚本格式 vim shell.sh #!/bin/bash //声明脚本解释器,这个‘#’号不是注释,其余是注释 #Program: //程序内容说明 #History: //时间和作者 二.sh ...

  4. shell脚本编程学习笔记(三)编写邮件报警脚本

    一.shell编写邮件报警脚本 1.POSTFIX邮件服务器准备 a.首先卸载服务器上自带的sendmail rpm -qa sendmail* //查看安装的sendmail rpm -e send ...

  5. shell脚本编程学习笔记(二)linux服务器启动流程

    一.linux服务器启动流程 1.bios找到磁盘上的mbr主引导扇区 2.进入grub洁面选择相应的启动内核 3.读取kernel内核文件-/boot/vmlinuz-* 4.读取init的镜像文件 ...

  6. Shell脚本编程学习入门 02

    Shell脚本编程学习入门是本文要介绍的内容,我们可以使用任意一种文字编辑器,比如gedit.kedit.emacs.vi等来编写shell脚本,它必须以如下行开始(必须放在文件的第一行):   #! ...

  7. Bash脚本编程学习笔记07:循环结构体

    本篇中涉及到算术运算,使用了$[]这种我未在官方手册中见到的用法,但是确实可用的,在此前的博文<Bash脚本编程学习笔记03:算术运算>中我有说明不要使用,不过自己忘记了.大家还是尽量使用 ...

  8. Bash脚本编程学习笔记08:函数

    官方资料:Shell Functions (Bash Reference Manual) 简介 正如我们在<Bash脚本编程学习笔记06:条件结构体>中最后所说的,我们应该把一些可能反复执 ...

  9. Bash脚本编程学习笔记06:条件结构体

    简介 在bash脚本编程中,条件结构体使用if语句和case语句两种句式. if语句 单分支if语句 if TEST; then CMD fi TEST:条件判断,多数情况下可使用test命令来实现, ...

随机推荐

  1. 【Unity|C#】基础篇(14)——预处理指令(#)

    [学习资料] <C#图解教程>(第23章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu. ...

  2. linux 配置compoer

    配置默认php 删除 rm -f /usr/bin/php 改到php7.3版本的composer /bin/php /usr/bin/php 多版本支持 配置php7专用composer70 cd ...

  3. 551-学生出勤记录 I

    551-学生出勤记录 I 给定一个字符串来代表一个学生的出勤记录,这个记录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : Present,到场 如果一个学 ...

  4. springboot整合PageHelper

    1.在pom文件中引入Pagehelper分页插件 <!-- 分页插件 --> <dependency> <groupId>com.github.pagehelpe ...

  5. 手动运行jar包,指定配置文件

    打包程序 mvn package cd target 指定参数运行 java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar --spring.profile.act ...

  6. 数组的concat连接

    let arr1 = [1,3,5],arr2 = [2,32,78],arr3 = [];arr3 = arr1.concat(arr2);// arr1 = [1,3,5] arr2 = [2,3 ...

  7. 07 部署fastDFS文件数据库

    安装fastDFS前必须准备好两个版本匹配的文件: libfastcommon_V1.0.7.tar.gz:基础库文件 FastDFS_V5.05.tar.gz:文件数据库文件 注:这两个文件版本要匹 ...

  8. Codeforces Round #600 (Div. 2) A. Single Push

    #include<iostream> #include<cstdio> #include<cstdlib> using namespace std; int T,n ...

  9. 算法竞赛入门经典第二版 蛇形填数 P40

    #include<bits/stdc++.h> using namespace std; #define maxn 20 int a[maxn][maxn]; int main(){ ; ...

  10. 记录 shell学习过程(8)函数

    start () { echo "Apache start ...... [OK]" #return 0 可以写一个返回值,比如执行成功返回 0 } stop () { echo ...