Have you ever wanted to generate a random string in MySQL, say for assigning a random password to a list of users? Well here is a useful tip!
SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 6) AS password
This creates a 6 character (easy to change, as you can see) string where the characters are from the MD5 command (and therefore in the range a-z and 0-9).
3 Comments
Excellent tip - this would
Excellent tip - this would allow you to have a function for generating md5 keys for sessions instead of perl code... put it all in the db.
Thank for MYSQL random string
Thank for MYSQL random string generator help.
Really nice
avoiding accidental bad words
Great tip!
For randomly assigned codes & passwords, we use a restricted alphabet that does not include vowels (or the digits 0 and 1 because they can resemble vowels). This is to avoid accidentally assigning a bad word, since virtually all bad words have at least one vowel.
Furthermore, our defaults are all uppercase.
Luckily we can slightly modify your tip with the mysql REPLACE and UPPER functions. Unfortunately, REPLACE doesn't seem to support regex and UPPER requires a call to CONVERT before it will work properly. So the code is a little convoluted, but it does seem to work fine...
Post new comment