Shell Script Programming Learning notes (iv) Shell operations database

Source: Internet
Author: User
Tags apache log pkill

First, the basic operation of the database

1) Log in to MySQL server: mysql-u root-p password

2) View database: Show databases

3) View table: Show Tales from DB;

4) View table structure: DESC table;

5) Creating Table: Create table table ();

6) View Table data: SELECT * from table;

7) Insert data: INSERT into table (F1,F2) VALUES (' v1 ', ' v2 ');

8) Delete data: Delete from table where id=8;

9) Modify table data: Update table T1 set f1= ' 123 ', f2= ' abc ' where id=8;

10) Delete tables: drop table T1;

11) Delete databases: drop database D1;

Second, shell operation MySQL increase, delete, change, check

1) Connect to the database

#!/bin/bash

#mysql. Sh

MySQL='mysql-uroot-proot'

SQL="show Databases"

$mysql- e "$sql"

2) Shell operation MySQL Add, delete, change, check

        

#!/bin/bash

#mysql1. Sh

Conn="mysql-uroot-proot"

Case $ in

Select)

SQL="query statement "

;;

Delete)

SQL="DELETE statement "

;;

Insert)

SQL="INSERT statement "

;;

Update)

SQL="UPDATE statement "

;;

Esac

$conn- e "$sql"

Iii. Apache log segmentation and related scheduled tasks

#!/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 re-read log file

CRONTAB-E//Mission plan

XX * * */mnt/logcut.sh

CRONTAB-L//View task schedule

Four, Apache log statistics of the MySQL database

Combine log splitting and log statistics into the database

#!/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 $}'| Sort|uniq -C |awk '{print $ ': "$ $}' > a.txt//The directory and filename here can be $dstlog

MySQL="mysql-uroot-proot"

For i in ' cat a.txt '

Do

PV='echo $i |awk-f: '{print '}'

IP='echo $i |awk-f: '{print $}'

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//Mission plan

XX * * */mnt/logcut.sh

CRONTAB-L//View task schedule

Shell Script Programming Learning notes (iv) Shell operations database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.