返回顶部

收藏

Shell MySql Tool

更多

[Shell/批处理]代码

#!/bin/bash
#
# Version 0.02 - JF Nutbroek 2007
#

# Path to MySQL binary
MYSQL="/opt/lampp/bin/mysql"

# Code starts here

check() {
 OUT=$?
 if [ ! $OUT -eq 0 ]; then
  echo "Error!"
  exit 2
 fi
}

getinfo() {
 echo "##### WARNING - Expert only! #####"
 echo ""
 echo "Please enter your MySQL username [enter=root]"
 read USERNAME
 if [ "$USERNAME" = "" ]; then
  USERNAME="root"
 fi
 echo "Please enter your MySQL password"
 stty -echo
 read PASSWORD
 stty echo
 echo ""
 DBNAMES="`$MYSQL --user=$USERNAME --password=$PASSWORD --host=localhost --batch --skip-column-names -e "show databases" | sed 's/ /%/g'`"
 PS3='Please select the database:'
 select DB in $DBNAMES ; do
  break
 done
 echo ""
}

optimize() {
 clear
 echo "Optimizing database..."
 echo ""
 getinfo
 TABLENAMES="`$MYSQL --user=$USERNAME --password=$PASSWORD --host=localhost -D $DB -e "show tables\G;" | grep 'Tables_in_' | sed -n 's/.*Tables_in_.*: \([_0-9A-Za-z]*\).*/\1/p'`"
 for TABLENAME in $TABLENAMES
 do
  $MYSQL --user=$USERNAME --password=$PASSWORD --host=localhost -s -D $DB -e "optimize table $TABLENAME;"
 done
 echo ""
 echo "Completed"
}

repair() {
 clear
 echo "Repairing database..."
 echo ""
 getinfo
 TABLENAMES="`$MYSQL --user=$USERNAME --password=$PASSWORD --host=localhost -D $DB -e "show tables\G;" | grep 'Tables_in_' | sed -n 's/.*Tables_in_.*: \([_0-9A-Za-z]*\).*/\1/p'`"
 for TABLENAME in $TABLENAMES
 do
  $MYSQL --user=$USERNAME --password=$PASSWORD --host=localhost -s -D $DB -e "repair table $TABLENAME;"
 done
 echo ""
 echo "Completed"
}

case "$1" in 
 'optimize')
  setterm -background blue
  clear
  optimize 
  setterm -background black
  ;;
 'repair')
  setterm -background blue
  clear
  repair
  setterm -background black
  ;; 
 *)
  echo "Usage: mysqltool optimize | repair"
esac

exit 0

标签:MySQL

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. admin 发表 2018-07-18 07:20:30 大神教你DIY高性能运维堡垒体系
  2. love冥天 发表 2018-07-17 02:07:24 mysql 去除不可见字符
  3. 仙宇 发表 2018-07-14 08:19:49 【Python搞搞轻量博客】第五发 关系型数据库Mysql的Py交易
  4. xinlu 发表 2018-07-12 23:30:17 统计表数量及记录数量
  5. 尹少爷 发表 2018-07-12 06:26:10 mysql 远程链接问题 connected refused
  6. admin 发表 2018-07-11 06:13:30 理解MySQL的SSL连接
  7. 老王 发表 2018-07-09 11:35:22 通过ProxySQL提升PHP/MySQL的性能
  8. phpor 发表 2018-07-09 02:47:42 关于Mysql 索引
  9. phpor 发表 2018-07-09 07:21:28 SQL案例分析
  10. 博主 发表 2018-07-09 00:23:00 mysql - 设置独立的服务器.并且指定访问范围
  11. coder4 发表 2018-07-05 02:29:09 如何将utf8的表转为utf8mb4
  12. admin 发表 2018-07-05 06:03:20 MySQL如何利用ibd文件恢复数据?

发表评论