MySQL root access with sudo
August 28th, 2009
No comments
Normally, the system’s root user does not automatically have root access to the MySQL (mysql in short) databases on the system. However, sometimes we’d like to have root access on our mysql databases via sudo. This is possible when performing the following steps as root:
$ touch /root/.my.cnf$ chmod 600 /root/.my.cnf- Put the following text into /root/.my.cnf:
[client] password = <mysql root password> port = 3306 host = localhost socket = path/to/your/mysql.sock
When having a .my.cnf file in the home-directory, executing mysql starts the mysql-client under the currently logged-in user and by using the password provided in the ~/.my.cnf file. This also means, that
$ sudo -H mysql
starts the mysql-client under mysql-”root.” The option “-H” is necessary because sudo has to change the home-directory to the system’s root user, i.e., /root/. Otherwise, mysql uses the ~/.my.cnf file of the user executing sudo.

