Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

We are running a couple of shared host linux servers and a private one. We are running cPanel/WHM licenses under them.

The predicament I am facing is :

How can I run the following command automatically ?

mysqlcheck -u root -p --auto-repair --check --optimize --all-databases

The problem is that I am prompted for the root password, but I would like to run this command automatically and the password prompt intrerupts this process.

Please help me.

share|improve this question
up vote 1 down vote accepted

Store your root credentials in a file that only the root user can access. Then simply pass the credentials filename.

mysqlcheck --defaults-file=/etc/mysql/root.cnf  --auto-repair --check --optimize --all-databases


host     = servername.domain.tld
user     = backup
password = strongpassword

This doesn't create a huge security vulnerability so long as /etc/mysql/root.cnf is only readable by root since the root user could easily reset the mysql passwords without needing any mysql credentials at all.

share|improve this answer
Thank you, Zore. – andrei.troll Oct 25 '12 at 7:09

You could put the required password either in the [client] section of your ~/.my.cnf or an additional config file you specify with --defaults-extra-file=path/to/yourconfig.cnf

For more details, please refer to the option-files segment of the MySQL documentation.

share|improve this answer
Thank you so much, Sven. – andrei.troll Oct 25 '12 at 7:08

You could just supply the password on the commandline. Of course this has some privacy/security issues.

mysqlcheck -uroot -pPASSWORD --auto-repair --check --optimize --all-databases
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.