批量清除過期的binlog釋放磁盤空間
clearbinlog.sh腳本如下:
for masterdb in `cat master.db.full`;do
#1 echo get the binlog position infomation
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 "`
echo $str_log_files;
log_file=`echo $str_log_files | awk '{print $2}'`;
echo $log_file;
#2 echo get the master ip address or master hostname
db01tmp=`ssh $masterdb " /opt/mysql/product/5.5.25a/bin/mysql -uroot --password="" -e \"show slave status\G;\" |grep -i Master_Host "`;
db01=`echo $db01tmp | awk '{print $2}'`
#3 begin to clear the old binlog
ssh $db01 "/opt/mysql/product/5.5.25a/bin/mysql -uroot --password="" -e \"purge master logs to '$log_file';\""
#4 check the disk space for master
ssh $db01 "df -h"
echo " "
echo " -- -- -- ";
done;
最後再次check disk space,執行check_disk.sh腳本,腳本内容如下:
for masterdb in `master.db.full`;do
ssh $masterdb "df -h" |grep -i mysqldatadir;
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)在每一行的末尾加分号
sed 's/.*/&;/' g.sql >g1.sql
awk '{print $0,"xxxx"}' file
(2)去掉第一行記錄
sed -i '1d' g1.log
(3)在每行的頭添加字元,比如"HEAD",指令如下:
sed 's/^/HEAD&/g' test.file
(4)在每行的行尾添加字元,比如“TAIL”,指令如下:
sed 's/$/&TAIL/g' test.file
(5)删除檢索到的行,-i在源檔案上面修改
sed -i '/grant fors/d' t.file
(6)sed替換
sed -e 's/foo/bar/' myfile.txt
此指令将 myfile.txt 中每行第一次出現的 'foo'(如果有的話)用字元串 'bar' 替換,然後将該檔案内容輸出到标準輸出。
sed -e 's/foo/bar/g' myfile.txt
此指令将 myfile.txt 中每行出現的 'foo'(如果有的話)用字元串 'bar' 進行全局替換,然後将該檔案内容輸出到标準輸出。