kvz.io
Published on

Access MySQL Without Password

Authors
  • avatar
    Name
    Kevin van Zonneveld
    Twitter
    @kvz

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

On Ubuntu / Debian, you don't. This is useful 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?

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

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

$ 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, its 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:

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

And Bob's your uncle.

Legacy Comments (8)

These comments were imported from the previous blog system (Disqus).

Tim
Tim·

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.

Tim
Tim·

ps. don\'t forget to chmod 600 ~/.my.cnf ;-)

CrazyCat
CrazyCat·

I\'m not sure that the \"-p\" option is followed by password, but --password=\"pass\" is ok.
-p means \"ask for password\", and -p xyz means \"ask password and select xyz base\".

Kev van Zonneveld
Kev van Zonneveld·

@ Tim: Thanks for sharing!

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

CrazyCat
CrazyCat·

@kevin : ok, it\'s better :) But I really prefer the password=\"mypass\" which is more explicit, imho :)

Kev van Zonneveld
Kev van Zonneveld·

@ CrazyCat: Fair enough :)

Jerry Michael
Jerry Michael·

I got many tips on MySQL from your blog. Thanks Kevin

laptop guide
laptop guide·

Fine information, many thanks to the author