Tuesday, April 13, 2010

Mysql - flush-hosts

My organization's mysql server always needed to execute mysqladmin -flush-hosts command (1 week interval). I don't want to do that everytime our web server could't contact its database server. So what am I to do? Well, I tried using cron but the mysql server need to be authenticate to run mysqladmin command. At last, I found one solution. EXPECT! How?

Make sure you install expect in your mysql server (Centos Linux). Just issue command

yum install expect

Then, make a script file like this:

#!/usr/bin/expect
set timeout 20
spawn /usr/bin/mysqladmin -p flush-hosts

expect "Enter password: "
send "yourmysqlrootpasswd\r\n"

save this script and add this script to run in cronjob.

That's it. Done. You might want to chmod this script to 700 first.

1 comment:

kamarul said...

Sir,
I add one more line at the bottom, without 'expect eof' the script did not finish correctly.


#!/usr/bin/expect
set timeout 20
spawn /usr/bin/mysqladmin -p flush-hosts

expect "Enter password: "
send "mysqlpassword\r\n"
expect eof


Thank You.