Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've created a user ... but forgotten the password

mysql> create user 'blayo'@'%' identified by 'right';

Which Linux command line tool can encrypt the password the same way mysql 5.5 does ?

mysql> select Password,User from mysql.user
------------------------------------------+-------+
*920018161824B14A1067A69626595E68CB8284CB | blayo |

...to be sure I use the right one

$ tool right
*920018161824B14A1067A69626595E68CB8284CB
share|improve this question
2  
Why can't you just login as an administrator and define a new password for blayo? That's definitively faster than running through trillions of possible character combinations to find the right one. –  patrix Aug 6 '12 at 20:10
    
Please do not post the same question to multiple Stack Exchange sites. The DBA version has been answered as well, which is a waste of effort. –  Gilles Aug 6 '12 at 23:57

5 Answers 5

up vote 7 down vote accepted

Well, the trivial (perhaps cheating) way would be to run:

mysql -NBe "select password('right')"

This will produce a password using whatever password hashing scheme your version of mysql uses. [EDIT: added -NB, which gets rid of the column names and ascii table art.]

share|improve this answer
    
And, handily, this will store your shiny new password in clear text both in your MySQL log and in your bash account history log. –  Craig Sep 14 at 18:16

If you crated a user and forgotten the password this is what you should do and it's easier.

This also happened to me a dozen times while I was doing 10 billion things at once. The best way that I recovered my password was:

stopped the mysql server completely first

issued mysqld_safe --skip-grant-tables &

then I logged in as root without issuing a password, just mysql -u mysql-user (it lets you in after the mysqld_safe)

then go to the mysql > user table and update the password for the user

then go back and restart mysql

see if that works and let us know.

share|improve this answer

It still kind of blows my mind that MySQL doesn't bother obfuscating passwords on the command line and in logs. And that's the only reason I'm adding an answer rather than just commenting on @Gilles answer.

So, of course, you could just log into MySQL as an admin and set a new password for your blayo user, as @patrix suggested.

However, the standard way to do that is by using MySQL's password() function, which takes a plaintext password as an argument (seriously?).

If you do that, you leave the plaintext version of your MySQL user's password sitting around in your bash history and in your MySQL logs, for easy retrieval later on by whoever manages to get access to those log files.

Wouldn't it be better to have a little utility that would prompt for the password, without echoing it to the screen or to your logs, then provide you with the resulting MySQL-compatible hash?

So, modifying @Gilles's answer just a bit, how about a little shell script that uses Python, like the following. You could easily modify this to run a SQL statement against your MySQL database to set the password all at once. But even without going that far, just copy and paste the resulting hash into a SQL statement to update the users table:

#!/bin/bash

mysqlpwd=$(/usr/bin/python -c 'from hashlib import sha1; import getpass; print "*" + sha1(sha1(getpass.getpass("New MySQL Password:")).digest()).hexdigest()')

echo $mysqlpwd
share|improve this answer
    
@HalosGhost; what was the edit about? It didn't change anything? ;-) –  Craig Sep 15 at 2:42
    
It enforced syntax highlighting (as noted in the edit summary) and made obvious by the actual text of the edit. –  HalosGhost Sep 15 at 3:06
2  
Ah, so. Honestly didn't realize what you'd done (you obviously added the <!-- language: lang-bsh --> line). Handy! I like it. Thanks! –  Craig Sep 15 at 4:29

The hash is sha1(sha1(password)). Since there is no salt (which is a grave security flaw), you can look up the hash in a table.

With just POSIX tools plus sha1sum from GNU coreutils or BusyBox, sha1(sha1(password)) is annoying to compute because the sha1sum command prints out the digest in hexadecimal and there is no standard tool to convert to binary.

awk "$(printf %s 'right' | sha1sum |
       sed -e 's/ .*//' -e 's/../, 0x&/g' \
           -e 's/^/BEGIN {printf "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c"/' \
           -e 's/$/; exit}/')" | sha1sum

Python has standard digests in its standard libraries, so it's a simple one-liner.

printf %s 'right' |
python -c 'from hashlib import sha1; import sys; print sha1(sha1(sys.stdin.read()).digest()).hexdigest()'

Or with the password inside the one-liner:

python -c 'from hashlib import sha1; print sha1(sha1("right").digest()).hexdigest()'
share|improve this answer
    
I love this answer in spirit, but it isn't quite working. When I hit the enter key, it still wants to keep reading input instead of moving on. Also, the input echos back to the screen, which might not be desirable. How about something like this, though? unix.stackexchange.com/a/155583/37401 –  Craig Sep 14 at 19:33
    
@Craig How to read interactive input is beyond the scopre of the question. I assume that the script already has the password; usually it's stored in a file, it's rare to type a database password interactively. If you want to read interactive input, you can use the read shell builtin to read a line; run stty echo first if you want to turn off echo and ssty +echo to turn it back on. In Python, you can use the getpass module. –  Gilles Sep 14 at 19:49
    
Yeah, I know... and fair enough! I just hate horrible security the further down the road I go, and MySQL security is pretty horrible in general, so I have this knee-jerk tendency to want to inject a little security insight wherever I get a chance. ;-) –  Craig Sep 14 at 20:42

MySQL 4.1+ uses double SHA1

> SELECT PASSWORD("right")
*920018161824B14A1067A69626595E68CB8284CB

> SELECT SHA1(UNHEX(SHA1("right")))
920018161824B14A1067A69626595E68CB8284CB

sh-3.2# php -r 'echo "*" . sha1(sha1("right", TRUE)). "\n";'
*920018161824b14a1067a69626595e68cb8284cb

This algorithm can be easily ported to other languages​​. A difference is that password hashes in the "select password" always begin with a “*” character.

share|improve this answer

Your Answer

 
discard

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.