批量清除过期的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' 进行全局替换,然后将该文件内容输出到标准输出。