Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Fix broken mysql meta tables.
  4. #
  5. # For some reason, `innodb_*_stats' tables seem to be corrupted (not existing
  6. # when being queried from mysql shell, but physical files do exist).
  7. # `slave_worker', `slave_relay_log_info' and `slave_master_info' tables
  8. # seem to have the same problem, which `mysql_install_db' will happily fix.
  9. #
  10. # This script performs the necessary steps to have that done.
  11.  
  12. echo "You do not want to run this accidentally, do you?"
  13. exit 1
  14.  
  15. do_sql () {
  16. local query="$1"
  17. echo "${query}" | mysql -u root mysql
  18. }
  19.  
  20. if [ $(whoami) != root ]; then
  21. echo "Can't run $0 without root permissions!"
  22. exit 1
  23. fi
  24.  
  25. tables="innodb_index_stats innodb_table_stats slave_master_info \
  26. slave_relay_log_info slave_worker_info"
  27.  
  28. for table in ${tables}; do
  29. do_sql "DROP TABLE ${table};"
  30. do_sql "ALTER TABLE ${table} DISCARD TABLESPACE;"
  31. done
  32.  
  33. mysql_pid=$(pidof mysqld)
  34.  
  35. if [ ! -z "${mysql_pid}" ] ; then
  36. echo "stopping mysql (${mysql_pid})"
  37. service mysql stop
  38. wait ${mysql_pid}
  39. else
  40. echo "mysql not running"
  41. fi
  42.  
  43. mysql_db_dir=/var/lib/mysql/mysql
  44. backup_dir=/root/backup-mysql
  45.  
  46. echo "moving bad table files in ${backup_dir}"
  47. mkdir "${backup_dir}" >/dev/null
  48.  
  49. for table in ${tables} ; do
  50. mv -f ${mysql_db_dir}/${table}.* ${backup_dir} >/dev/null
  51. done
  52.  
  53. cd /var/lib/mysql && mysql_install_db
  54.  
  55. service mysql start && echo "mysql restarted"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement