Kevin van Zonneveld

On Development and Internet System Engineering

Access MySQL without Password

| Comments

If you want to do command-line MySQL administration like restoring databases or dumping statistics, you need the root account and it’s password. Or do you?

On Ubuntu / Debian, you don’t. This is usefull if you

  • Have lost your password (reset it this way)
  • Want to automate tasks (it’s a bad idea to pass around root passwords)

So how does this work

Well normally you would access MySQL like this right?

1
2
mysql -h localhost -u root -p
# Enter password: XYZ

Or you can automate it by supplying it in the same command:

1
mysql -h localhost -u root -pXYZ

Pretty messy if you ask me.

What a lot of people don’t know is that there is an account that’s used by debian related systems to restart the service, perform checks, etc.

The account is called debian-sys-maint, it’s credentials are stored in /etc/mysql/debian.cnf and if you have root access on a database server’s shell you’re in business. You can either look up the account, or tell mysql to do it for you:

1
mysql --defaults-file=/etc/mysql/debian.cnf

And Bob’s your uncle.

Imported comments

These were imported from my old blog. Please use disqus below for new comments

laptop guide on 2010-11-21 17:43:42
Fine information, many thanks to the author

Jerry Michael on 2010-06-17 13:12:26
I got many tips on MySQL from your blog. Thanks Kevin

Kevin on 2010-03-29 14:38:48
@ CrazyCat: Fair enough :)

CrazyCat on 2010-03-28 11:35:11
@kevin : ok, it's better :) But I really prefer the password=\& quot;mypass\& quot; which is more explicit, imho :)

Kevin on 2010-03-24 10:02:42
@ Tim: Thanks for sharing!

@ CrazyCat: You are right: -p xyz doesn't work. -pxyz does however. I've updated the article accordingly.

CrazyCat on 2010-03-23 21:48:53
I'm not sure that the \& quot;-p\& quot; option is followed by password, but –password=\& quot;pass\& quot; is ok.
-p means \& quot;ask for password\& quot;, and -p xyz means \& quot;ask password and select xyz base\& quot;.

Tim on 2010-03-22 08:53:35
ps. don't forget to chmod 600 ~/.my.cnf ;-)

Tim on 2010-03-22 08:51:54
or you can store a file in your homedir named .my.cnf (or /root if you want automated backups) that looks something like this:
[mysql]
user=yourmysqlusername
password=xyz

[mysqldump]
user=root
password=xyz

this is not Debian specific.

Comments