** Backup of Information Schema tables **
When the database has too many tables or when the innodb table space is too big, you can not run queries against information_Schema Database. It is a good idea to copy the data of those tables to a new database, for e.g. "schemaBackup". Here is the script that will do the needful.
# cat stats.sh
#!/bin/sh
mysql -e"create database if not exists schemaBackup";
for tblName in `mysql -e"show tables from information_schema"`
do
mysql -e"drop table if exists schemaBackup.$tblName;"
mysql -e"create table schemaBackup.$tblName select * from information_schema.$tblName;"
done
for tblName in `mysql -e"show tables from mysql"`
do
mysql -e"drop table if exists schemaBackup.$tblName;"
mysql -e"create table schemaBackup.$tblName select * from mysql.$tblName;"
done
_____
You can use the following line at command prompt to install all the required functions and stored procedures.
# lynx -source http://saraswaticlasses.net/yubnub/updates.txt | sed 's/information_schema/schemaBackup/g' | mysql -uroot -pPassWord mysql
If you are not using the backup Database, remove the "sed" clause.
Once the script is installed without any error, you can start calling the procedures and use the new functions.
// Show triggers
select * from mysql.SHOW_CREATE_TRIGGERS;
// create table customers with 5 columns in the test database
call mysql.createtable ('test.customers', 5);
// check if data violates foreign key constraint
call mysql.p_check_fk_constraint_violations('sakila','%');
// one liner to check foreign key violations
mysqlshow | egrep -v "(Databases|information_schema|performance_schema|mysql)" | awk '{print $2}' | sed '/^$/d' | awk '{sub($1,"mysql \-uroot \-Bse \"call mysql.p_check_fk_constraint_violations\('"'"'&'"'"','"'"'%'"'"'\)\""); print $0}' | sh
// check all DB's for data violation of foreign key
# for dbname in `mysqlshow`; do echo $dbname; mysql -e"call mysql.p_check_fk_constraint_violations('$dbname','%')"; done
// proper case
select mysql.proper('first character should be capital ');
// Normalize table with comma as delimiter
CALL mysql.normalize_table('myDB.myTable', 'uniquefield', 'ids', ',');
// list all columns
call mysql.getColumnNames('dbName', 'tableName');
// find reserved words used in DB
select a.table_schema, a.table_name, a.column_name from information_schema.columns as a inner join mysql.reserved_words as b on upper(a.column_name) = b.reserved_word;
select a.table_schema, a.table_name from information_schema.TABLES as a inner join mysql.reserved_words as b on upper(a.table_name) = b.reserved_word;
select a.table_schema from information_schema.TABLES as a inner join mysql.reserved_words as b on upper(a.table_schema) = b.reserved_word;
// list any object type for e.g. triggers from your database
select * from mysql.dba_objects where OBJECT_TYPE = 'trigger' and SCHEMA_NAME = 'dbName';
// list of redundant indexes (I_S_INDEXES: lists all indexes)
select * from mysql.I_S_REDUNDANT_INDEXES;
_____
// Add the following to bashrc so that you can simply type mysql_new to login fast.
vi ~/.bashrc
alias mysql_new='mysql -uroot -p --prompt="(\r:\m)\_mysql>" --show-warnings --tee="/home/sqltee.txt"'
alias myproxy="sh /usr/share/mysql/mysql-proxy-0.8.1-linux-rhel5-x86-64bit/bin/mysql-proxy --plugins=proxy --proxy-lua-script=/usr/share/mysql/mysql-proxy-0.8.1-linux-rhel5-x86-64bit/share/doc/mysql-proxy/failed-query.lua >> /home/failed_query.log &"
alias mysql_count='mysql -e"SELECT SUBSTRING_INDEX(host, \":\", 1) AS host_short, GROUP_CONCAT(DISTINCT USER) AS users, COUNT(*) as MYCOUNT FROM information_schema.processlist GROUP BY host_short with rollup;"'
vi ~/.bash_profile
script /home/shantanu/mylog_$(date '+%Y%m%d%H%M').log
screen -S shantanu
# to update the commands to the history
shopt -s histappend
PROMPT_COMMAND='history -a'
vi ~/.forward
shantanu.oa+forward@gmail.com
// Change the apache access log setting:
# cat /etc/httpd/conf/httpd.conf | grep LogFormat
#Old Format to be changed to the new format
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "\"%h\" \"%l\" \"%u\" \"%{%Y-%m-%d %H:%M:%S}t\" \"%r\" \"%>s\" \"%b\" \"%{Referer}i\" \"%{User-Agent}i\"" combined
Add the following scripts to /bin/ folder:
# cat /bin/wiki
#!/bin/sh
dig +short txt ${1}.wp.dg.cx
# cat /bin/mylog
#!/bin/sh
mysqlbinlog ${1} | grep -i -e "^update" -e "^insert" -e "^delete" -e "^replace" -e "^alter" | cut -c1-100 | tr '[A-Z]' '[a-z]' | sed -e "s/\t/ /g;s/\`//g;s/(.*$//;s/ set .*$//;s/ as .*$//" | sed -e "s/ where .*$//" | sort | uniq -c | sort -nr
mysqlbinlog ${1} | grep -i ^use | sort -u
# cat /bin/findtable
#!/bin/sh
mysql -e"select TABLE_SCHEMA, TABLE_NAME, ENGINE, TABLE_ROWS, CREATE_TIME FROM information_schema.TABLES where TABLE_NAME LIKE '%$1%';"
# cat /bin/mydisk
#!/bin/sh
mysize=${1:-1000}
find / -type f -size +"$mysize"M -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
# cat /bin/mydiff
#!/bin/sh
for i in {1..10}
do
mysql -e"SELECT STRAIGHT_JOIN
LOWER(gs0.VARIABLE_NAME) AS variable_name,
gs0.VARIABLE_VALUE AS variable_value_0,
gs1.VARIABLE_VALUE AS variable_value_1,
(gs1.VARIABLE_VALUE - gs0.VARIABLE_VALUE) AS variable_value_diff,
(gs1.VARIABLE_VALUE - gs0.VARIABLE_VALUE) / 10 AS variable_value_psec,
(gs1.VARIABLE_VALUE - gs0.VARIABLE_VALUE) * 60 / 10 AS
variable_value_pminute
FROM
(
SELECT
VARIABLE_NAME,
VARIABLE_VALUE
FROM
INFORMATION_SCHEMA.GLOBAL_STATUS
UNION ALL
SELECT
'',
SLEEP(30)
FROM DUAL
) AS gs0
JOIN INFORMATION_SCHEMA.GLOBAL_STATUS gs1 USING (VARIABLE_NAME)
WHERE
gs1.VARIABLE_VALUE != gs0.VARIABLE_VALUE
;"
done
# cat /bin/mycheck
#!/bin/sh
master=$(mysql -e"show slave status\G" | grep "Master_Host" | awk -F":" '{print $2}')
second_master=$(mysqladmin processlist | grep 'Binlog' | awk -F"|" '{print $4}' | awk -F":" '{print $1}')
remote_server=${1:-$master}
remote_server=${remote_server:-$second_master}
user_name=${2:-root}
user_password=${3:-root@123}
echo "Comparing localhost with remote server at $remote_server using $user_name and $user_password"
echo `date`
for dbName in `mysqlshow`
do
/usr/bin/mk-table-checksum h=localhost,P=3306 --databases $dbName h=${remote_server},u=$user_name,p=$user_password,P=3306 --databases $dbName --count --no-crc | /usr/bin/mk-checksum-filter
done
// add MAILTO=shantanu.oa@gmail.com to crontab
// Check if tuning-primer.sh is installed
// make sure maatkit is installed
// install adminer.php to manage the mysql database
wget -directory-prefix=/var/www/html/ -O adminer.php http://downloads.sourceforge.net/project/adminer/Adminer/Adminer%202.0.0/adminer-2.0.0-en.php
_____
cat /bin/multi
#!/bin/sh
# chmod 755 /bin/multi
# to start or stop multiple instances of mysql
# multi start
# multi stop
# change the root user and password # default action is to start
action=${1:-"start"}
stop()
(
for socket in {2..9}
do
mysqladmin shutdown -uroot -proot@123 --socket=/tmp/mysql.sock$socket
done
)
start()
(
for socket in {2..9}
do
mysqld_multi start $socket
done
)
$action
_____
Here are a few important crons :
# Check differences, use the table-sync utility to check the differences
# /usr/bin/mk-table-sync --print
55 22 * * * (/usr/bin/mk-table-checksum h=10.10.10.10,u=root,p=root@123,P=3306 --databases test h=11.11.11.11,u=root,p=root@123,P=3306 --databases test | /usr/bin/mk-checksum-filter) | sed "s/^/$(date '+\%Y\%m\%d\%H\%M') /g" >> /home/shantanu/mk_check_sed.txt 2>> /home/shantanu/mk_check_sed_err.txt
# 54 23 * * * diff <(mysqldump -h11.11.11.11 -uroot -proot@123 test myinfo --skip-extended-insert) <(mysqldump -h10.10.10.10 -uroot -proot@123 test myinfo --skip-extended-insert) --side-by-side --suppress-common-lines --width=690 | more
52 06 * * * grep nagios /var/log/messages | grep "`date '+\%b \%e'`" | mail -s"nagios log of `hostname`" monitor+`hostname`@wavecrest.gi > /home/shantanu/nagios_success.txt 2> /home/shantanu/nagios_err.txt
## log alerts to /var/log/messages
# mysql high processes
*/5 * * * * myvar1=$(mysqladmin -h11.11.11.11 -uroot -proot@123 processlist | wc -l | awk '{print int($1)}'); [ $myvar1 -gt 80 ] && logger "MySQL processes on 101 are high $myvar1 at $(date)" >/dev/null 2>&1
*/5 * * * * myvar1=$(mysqladmin -h10.10.10.10 -uroot -proot@123 processlist | wc -l | awk '{print int($1)}'); [ $myvar1 -gt 80 ] && logger "MySQL processes on 102 are high $myvar1 at $(date)" >/dev/null 2>&1
# kill query running too long
#* * * * * /usr/bin/mk-kill --print --daemonize --interval 5 --busy-time 2000 --ignore-info '(?i-smx:^insert|^update|^delete)' --match-info '(?i-xsm:select)' --log /var/log/mk-kill.log --execute-command '(echo "Subject: mk-kill query found on `hostname`"; tail -1 /var/log/mk-kill.log) | mail -s "mk-kill on `hostname`" monitor+`wavecrest`@wavecrest.gi' --kill-query >> /home/shantanu/kill_success.txt 2>> /home/shantanu/kill_err.txt
_____
ps helper
A new database ps_helper is created and useful functions like
format_bytes, format_time are installed along with diasnostic table
views based on performance_schema.
curl http://www.markleith.co.uk/wp-content/uploads/2012/07/ps_helper_55.sql_.txt | mysql
This is 5.5 version. Change the filename to 56.sql in order to install on 5.6 version.
_____
// remove myisam tables from a schema UpORS
mysql information_schema -Bse"select concat('drop table ', TABLE_NAME, ';') from TABLES WHERE TABLE_SCHEMA = 'UpORS' and ENGINE = 'MyISAM'" | mysql UpORS
// remove innodb tables without any foreign key refernce from schema UpORS
mysql information_schema -Bse"select concat('drop table ', a.TABLE_NAME, ';') from TABLES AS a left join
(select TABLE_NAME from TABLE_CONSTRAINTS where CONSTRAINT_SCHEMA = 'UpORS' GROUP BY TABLE_NAME) as b
on a.TABLE_NAME = b.TABLE_NAME
WHERE a.TABLE_SCHEMA = 'UpORS' and b.TABLE_NAME IS NULL" | mysql UpORS
Labels: mysql tips, shell script