Converting Between SQLServer, Oracle, PostgreSQL, MySQL, Sybase and others…

Hi all!
I was asked to make a conversion from T-SQL (MSSQL) Procedure to PL/PGSQL. Regarding how boring is this task, the follow link helped me:

I highly recommend it. The site has a commercial solution to convert all database, but some code can be converted online for free. 🙂
The conversion not fixed at all, but make a good part of the work… And all help is helpful…

Continue reading

Monitoring MySQL with Nagios – Quick View

Hi all!
As you know, we have some commercial solutions to monitoring/alerting MySQL, like MySQL Enterprise Monitor or Oracle Grid/Cloud Control.

But, regarding we are using MySQL instead of Oracle Database, we can assume it’s probably a decision taken based on cost. So, considering Open Source solutions, we basically have Nagios, Zabbix, OpenNMS…



Thinking on Nagios, in my opinion the “supra sumo” is
Below whitepaper and presentation:
White Paper
Good one by Sheeri Cabral and posted here!

Any way, with theese two we can make lots of magic:

– Check status of MySql server (slow queries, etc)
– Queries per second graph

– Allowes to run SQL Queries and setting thresholds for warning e critical. Ex: -d database -q query [-w warn] [-c crit] [-C conn_file] [-p placeholder]

Ex for Nagios call:

define command{
command_name    check_db_entries
command_line    /usr/local/bin/perl $USER1$/ -d "$ARG1$" -q "$ARG2$" $ARG3$

So, now it’s just make your queries and implement your free monitoring on MySQL! 🙂

Optimize fragmented tables in MySQL

It happens on MySQL, as you know. Run an Optimize Table solve the question.
BUT, be careful! During the optimize the table stay locked (writing is not possible).

(Fragmented Table)

So what?
To not cause a lock in every table, the script below shows and runs (if you want to list but not run, comment the line) only for tables that have fragmentation.

It was very useful to me! 🙂

echo -n "MySQL username: " ; read username
echo -n "MySQL password: " ; stty -echo ; read password ; stty echo ; echo
mysql -u $username -p"$password" -NBe "SHOW DATABASES;" | grep -v 'lost+found' | while read database ; do
mysql -u $username -p"$password" -NBe "SHOW TABLE STATUS;" $database | while read name engine version rowformat rows avgrowlength datalength maxdatalength indexlength datafree autoincrement createtime updatetime checktime collation checksum createoptions comment ; do
if [ "$datafree" -gt 0 ] ; then
fragmentation=$(($datafree * 100 / $datalength))
echo "$database.$name is $fragmentation% fragmented."
mysql -u "$username" -p"$password" -NBe "OPTIMIZE TABLE $name;" "$database"

The resul will be like:

MySQL username: root
MySQL password:
mysql.db is 12% fragmented.
mysql.db optimize status OK
mysql.user is 9% fragmented.
mysql.db optimize status OK

This script is a full copy from this post by Robert de Bock.
Thanks, Robert!


MySQL: Difference Between current_date(), sysdate() and now()

Do you know the difference?

current_date(): Only give you the date.
now(): Datetime when the statement,procedure etc… started.
sysdate(): Current datetime.

Take a look between the functions now() and sysdate() after executing sleep of 5 seconds…:

SQL> select current_date(),now(),sysdate(),SLEEP(5),now(),sysdate();
"2016-03-24";"2016-03-24 16:00:43";"2016-03-24 16:00:43";"2016-03-24 16:00:43";"2016-03-24 16:00:48"