I've seen various methods to set passwords in MySQL, e.g, things like:
GRANT USAGE ON db.* to 'dave'@'localhost' IDENTIFIED BY 'supersecretpassword');
SET PASSWORD [FOR 'dave'@'localhost'] = PASSWORD('reallysecretpassword');
UPDATE mysql.user SET PASSWORD=PASSWORD('confidentialpassword') WHERE user='dave' AND host='localhost';
But they seem to involve typing the password in the clear as part of an SQL command, which raises paranoia about things like whether it ends up in my SQL command history, or about whether anyone is looking over my shoulder or can peek at my terminal scrollback.
Is there anyway I can get MySQL to prompt me to give the password without echoing it to the screen (the same way Unix does with passwd
)?
The password-hashing algorithm is fairly simple, and can be replicated in other programmling languages.
According to https://www.pythian.com/blog/hashing-algorithm-in-mysql-password/, in MySQL 4.1 and up, the
PASSWORD
function takes the (binary) SHA1 of the password string, twice, then returns that as a hex string preceded by an asterisk.For example, in SQL:
Using MySQL code on the console defeats the purpose, but here's a fairly simple Python script that prompts for a password and generates a MySQL-compatible password string from it.
This can then be assigned directly to a user in MySQL:
So it is only the password hash that is ever seen on the console/MySQL history. (It's no more than can be seen by anyone with access to the
mysql.user
table.)As Federico Sierra points out in the comments, you can use
mysqladmin password
command to change the password for any user you can log in as.Formerly the password had to be supplied on the command line, but as of 5.7, if the password is omitted it prompts for one.
From the mysqladmin documentation:
So, if you can log in as the user (i.e. you know a user's password - and you are on a host you are permitted to log in from) - then you can change their password with:
e.g.
If course, if you have root privileges, you can reset the user's password (and change their host if necessary) to enable you to log in to do this. But that comes with its own risks, so it's not ideal.
A wrapper script in some language (I'd use php and run via cli since it would be a quick copy/paste from stuff I already have) that is executed, then deleted, or at least edited again to change all passwords to "changeme" or similar.