Shantanu's Blog

Database Consultant

October 12, 2009

 

Installing standard procedures for MySQL

** 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: ,


Comments: Post a Comment

<< Home

Archives

June 2001   July 2001   January 2003   May 2003   September 2003   October 2003   December 2003   January 2004   February 2004   March 2004   April 2004   May 2004   June 2004   July 2004   August 2004   September 2004   October 2004   November 2004   December 2004   January 2005   February 2005   March 2005   April 2005   May 2005   June 2005   July 2005   August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006   January 2007   February 2007   March 2007   April 2007   June 2007   July 2007   August 2007   September 2007   October 2007   November 2007   December 2007   January 2008   February 2008   March 2008   April 2008   July 2008   August 2008   September 2008   October 2008   November 2008   December 2008   January 2009   February 2009   March 2009   April 2009   May 2009   June 2009   July 2009   August 2009   September 2009   October 2009   November 2009   December 2009   January 2010   February 2010   March 2010   April 2010   May 2010   June 2010   July 2010   August 2010   September 2010   October 2010   November 2010   December 2010   January 2011   February 2011   March 2011   April 2011   May 2011   June 2011   July 2011   August 2011   September 2011   October 2011   November 2011   December 2011   January 2012   February 2012   March 2012   April 2012   May 2012   June 2012   July 2012   August 2012   October 2012   November 2012   December 2012   January 2013   February 2013   March 2013   April 2013   May 2013   June 2013   July 2013   September 2013   October 2013   January 2014   March 2014   April 2014   May 2014   July 2014   August 2014   September 2014   October 2014   November 2014   December 2014   January 2015   February 2015   March 2015   April 2015   May 2015   June 2015   July 2015   August 2015   September 2015   January 2016   February 2016   March 2016   April 2016   May 2016   June 2016   July 2016   August 2016   September 2016   October 2016   November 2016   December 2016   January 2017   February 2017   April 2017   May 2017   June 2017   July 2017   August 2017   September 2017   October 2017   November 2017   December 2017   February 2018   March 2018   April 2018   May 2018   June 2018   July 2018   August 2018   September 2018   October 2018   November 2018   December 2018   January 2019   February 2019   March 2019   April 2019   May 2019   July 2019   August 2019   September 2019   October 2019   November 2019   December 2019   January 2020   February 2020   March 2020   April 2020   May 2020   July 2020   August 2020   September 2020   October 2020   December 2020   January 2021   April 2021   May 2021   July 2021   September 2021   March 2022   October 2022   November 2022   March 2023   April 2023   July 2023   September 2023   October 2023   November 2023  

This page is powered by Blogger. Isn't yours?