博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

shell

Posted on 2016-09-23 14:10  moss_tan_jun  阅读(123)  评论(0编辑  收藏  举报

批量清除过期的binlog释放磁盘空间

clearbinlog.sh脚本如下:

 

  1. for masterdb in `cat master.db.full`;do  
  2. #1 echo get the binlog position infomation  
  3.     str_log_files=`ssh $masterdb "/opt/mysql/product/5.5.25a/bin/mysql -uroot --password="" -e \"show slave status\G;\" |grep -i master_Log_File "`  
  4.     echo $str_log_files;  
  5.     log_file=`echo $str_log_files | awk '{print $2}'`;   
  6.     echo $log_file;  
  7. #2 echo get the master ip address or master hostname  
  8.     db01tmp=`ssh $masterdb "  /opt/mysql/product/5.5.25a/bin/mysql  -uroot  --password=""  -e \"show slave status\G;\" |grep -i Master_Host  "`;    
  9.     db01=`echo $db01tmp | awk '{print $2}'`  
  10. #3 begin to clear the old binlog   
  11.     ssh $db01 "/opt/mysql/product/5.5.25a/bin/mysql  -uroot  --password="" -e \"purge master logs to '$log_file';\""  
  12. #4 check the disk space for master  
  13.     ssh $db01 "df -h"  
  14.     echo " "  
  15.     echo " -- -- -- ";  
  16. done; 

最后再次check disk space,执行check_disk.sh脚本,脚本内容如下:

  1. for masterdb in `master.db.full`;do  
  2.   ssh  $masterdb "df -h" |grep -i mysqldatadir;  
  3. done;

 

 

写了一个脚本,run这个脚本,就可以kill掉MySQL中所有sleep的client线程
vim killsleep.sh

#It is used to kill processlist of mysql sleep

#!/bin/sh
while :


do
  n=`mysqladmin processlist -uadmin -pxxxxx|grep -i sleep |wc -l`
  date=`date +%Y%m%d\[%H:%M:%S]`
  echo $n


  if [ "$n" -gt 10 ]
  then
  for i in `mysqladmin processlist -uadmin -pxxxxxx|grep -i sleep |awk '{print $2}'`
  do
     mysqladmin -uadmin -pxxxx kill $i
  done
  echo "sleep is too many I killed it " >> /tmp/sleep.log
  echo "$date : $n" >> /tmp/sleep.log
  fi               
  sleep 1
done

sed替换扩展部分

(1)在每一行的末尾加分号

  1. sed 's/.*/&;/' g.sql >g1.sql  
  2. awk '{print $0,"xxxx"}' file  



(2)去掉第一行记录

  1. sed -i '1d' g1.log  


(3)在每行的头添加字符,比如"HEAD",命令如下:

  1. sed 's/^/HEAD&/g' test.file  


(4)在每行的行尾添加字符,比如“TAIL”,命令如下:
sed 's/$/&TAIL/g' test.file


(5)删除检索到的行,-i在源文件上面修改

  1. sed -i '/grant fors/d' t.file  


(6)sed替换

    1. sed -e 's/foo/bar/' myfile.txt    
    2.  此命令将 myfile.txt 中每行第一次出现的 'foo'(如果有的话)用字符串 'bar' 替换,然后将该文件内容输出到标准输出。  
    3.  sed -e 's/foo/bar/g' myfile.txt   
    4.  此命令将 myfile.txt 中每行出现的 'foo'(如果有的话)用字符串 'bar' 进行全局替换,然后将该文件内容输出到标准输出。