Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
MySQL root password recovery script
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Wilhelm
Tux's lil' helper
Tux's lil' helper


Joined: 27 May 2003
Posts: 149

PostPosted: Thu Mar 25, 2004 6:19 pm    Post subject: MySQL root password recovery script Reply with quote

Hi i made a booboo a while back and figured out how to reset the root password on my mysql database. This is all common knowledge but even so i built a script so i can recover my mysql nice easy and quick.

Code:

#!/bin/bash
#
# resetRootPass script
#
# Recover lost root password of mysql database.
#
# By Willem Bermon
#

echo
echo "Mysql password recovery utility"
echo

# Stop the mysql server
/etc/init.d/mysql stop
/etc/init.d/mysql zap > /dev/null
/bin/killall mysqld > /dev/null

# Run mysqld in permissionless mode
/sbin/start-stop-daemon --start --quiet --exec /usr/bin/mysqld_safe \
        --background -- --skip-grant-tables >/dev/null 2>&1

sleep 1

# Execute queries
mysql -u root mysql -e "UPDATE user SET Password=PASSWORD('$1') WHERE \
                        user='root'; \
                        FLUSH PRIVILEGES;"
if [[ $? -eq 0 ]]
then
        echo " ** SQL root password updated"
else
        echo " ** SQL root password update unsuccesful"
fi

# Restart the mysql server
/bin/killall mysqld > /dev/null
/etc/init.d/mysql start

echo "Succesfully updated password!!"
echo
echo
exit 0


As you can see i did a little overkill on stopping mysql. It was done for testing however i never bothered taking it out.

Hopefully some people will find this a useful script for in your root user home directory.

Have fun
Back to top
View user's profile Send private message
froke
Apprentice
Apprentice


Joined: 16 Dec 2003
Posts: 174
Location: Westside USA

PostPosted: Thu Mar 25, 2004 11:47 pm    Post subject: Reply with quote

And then your mysql root password is stored in plain text in your .bash_history
_________________
Top notch image hosting. Great for screen shots! http://www.imagelink.org
Back to top
View user's profile Send private message
kronon
Apprentice
Apprentice


Joined: 11 Aug 2003
Posts: 212
Location: NL

PostPosted: Fri Mar 26, 2004 8:41 am    Post subject: Reply with quote

If you are THAT paranoid then you could delete the bash history.
Back to top
View user's profile Send private message
Wilhelm
Tux's lil' helper
Tux's lil' helper


Joined: 27 May 2003
Posts: 149

PostPosted: Tue Mar 30, 2004 6:43 pm    Post subject: Reply with quote

I'm no linux guru but your bash history is in your root directory. If they have access to your history they can already trash your DB anyway since they probably have root access.

This is a recovery script. You can always go in manually and change it so it doesn't show up in your history.
Back to top
View user's profile Send private message
froke
Apprentice
Apprentice


Joined: 16 Dec 2003
Posts: 174
Location: Westside USA

PostPosted: Tue Mar 30, 2004 10:50 pm    Post subject: Reply with quote

Yes, i understand that the bash history can be edited and is stored in /~

I was just pointing out that the password would be saved in clear text, which is never a good idea for any password. The script should remove it from bash history or use another method that doesn't echo the input password in plaintext.
_________________
Top notch image hosting. Great for screen shots! http://www.imagelink.org
Back to top
View user's profile Send private message
Wilhelm
Tux's lil' helper
Tux's lil' helper


Joined: 27 May 2003
Posts: 149

PostPosted: Mon Apr 12, 2004 1:00 am    Post subject: Reply with quote

froke wrote:
Yes, i understand that the bash history can be edited and is stored in /~

I was just pointing out that the password would be saved in clear text, which is never a good idea for any password. The script should remove it from bash history or use another method that doesn't echo the input password in plaintext.


Your right but this was like my second bash script ever and my first year of hardcore linuxing. I simply wasn't that paranoid when writing the script.

I take it, it would simply be a 'sed -e "s/<password>/xxxxxxxx/g' of the bash history to fix this problem. However the sed would come up in your history so you will need to counter that as well by copying bash beforehand and sedding the copy and making it your curretn history.
Would you be kind enough to show how it is done by you? I think it's pretty interresting tool I might be able to use in the future.
Back to top
View user's profile Send private message
froke
Apprentice
Apprentice


Joined: 16 Dec 2003
Posts: 174
Location: Westside USA

PostPosted: Mon Apr 12, 2004 2:35 am    Post subject: Reply with quote

I'm sorry, I don't know how to make the script remove the password. I had to reset a mysql root password a while back and there were instructions on how to do so in the mysql documentation. Of course, there are many ways to do this. There was a procedure that had a mysql prompt ask you for a password, so your password wouldn't be stored in plaintext. I don't remember the specifics, but it should be in the mysql documention at www.mysql.com
_________________
Top notch image hosting. Great for screen shots! http://www.imagelink.org
Back to top
View user's profile Send private message
verbatim
Apprentice
Apprentice


Joined: 13 Mar 2003
Posts: 223

PostPosted: Mon Apr 12, 2004 3:30 am    Post subject: Reply with quote

Just a nitpick but password recovery and resetting a password are entirely different things. Don't confuse people by making them think this does something other than what it actually does.

Other than that, good job, thanks for the script. :)
Back to top
View user's profile Send private message
linuxkrn
Tux's lil' helper
Tux's lil' helper


Joined: 13 Oct 2003
Posts: 140
Location: Denver,Colorado

PostPosted: Tue Apr 13, 2004 6:39 pm    Post subject: Reply with quote

Few tips,

As noted, don't require the password to be put on the command line.

This can be done with a simple "bash read" command to read it from stdin instead of an arg from command line.

Code:

#!/bin/bash

read -p "New password: " PASSWORD
echo "The password you typed was: ${PASSWORD}"


Second, if you don't want bash to "save" your current history (~/.bash_history) you can always term your bash PID.

Easy way is

Code:

kill -9 $$


This will keep bash from saving the file on exit.

And last, if you enter a blank password, that should "remove" the password from the login.
Back to top
View user's profile Send private message
sburnett
Tux's lil' helper
Tux's lil' helper


Joined: 10 Jun 2002
Posts: 79
Location: USA

PostPosted: Fri Apr 16, 2004 9:35 pm    Post subject: Reply with quote

Thanks for this tip! I used it just now and it works fine. My solution to the password problem was to read the password into a file using backticks directly in the command, like this:
Code:
mysql -u root mysql -e "UPDATE user SET Password=PASSWORD('`cat /tmp/pass`') WHERE user='root'; FLUSH PRIVILEGES;"

But I think that Wilhelm's solution is better for using it as a full script.
Back to top
View user's profile Send private message
tilos
n00b
n00b


Joined: 03 Sep 2004
Posts: 1

PostPosted: Fri Sep 03, 2004 6:20 am    Post subject: Reply with quote

I'd just fought with MySQL for an hour trying to figure out what the root password was when I found your script. THANK YOU
Back to top
View user's profile Send private message
beandog
Developer
Developer


Joined: 04 May 2003
Posts: 2007
Location: /usa/utah

PostPosted: Sat Oct 16, 2004 6:04 am    Post subject: Reply with quote

If youre really screwed, you could just copy all the data from /var/lib/mysql to a fresh mysql install. Works between windows <--> linux too.

edit: more specifically

Code:
$ rm -fr /var/lib/mysql/mysql
$ ebuild /var/db/pkg/dev-db/mysql-4.0.20/mysql-4.0.20.ebuild config
and you're done.
_________________
If it ain't broke, tweak it. packages | dvds | blurays | blog | home
Back to top
View user's profile Send private message
garris0n
n00b
n00b


Joined: 10 Dec 2003
Posts: 61
Location: Brooklyn, NY

PostPosted: Thu Apr 07, 2005 4:05 pm    Post subject: Reply with quote

froke wrote:
And then your mysql root password is stored in plain text in your .bash_history


export HISTFILE=/dev/null will prevent your current session's history from being saved.
Back to top
View user's profile Send private message
beatryder
Veteran
Veteran


Joined: 08 Apr 2005
Posts: 1138

PostPosted: Mon May 08, 2006 9:28 pm    Post subject: Reply with quote

This is pure gold!
_________________
Dont make it idiot proof, make it work.
Neucode.org
<suppressed key>
Back to top
View user's profile Send private message
umrguy76
n00b
n00b


Joined: 19 Jun 2003
Posts: 2

PostPosted: Wed Jun 07, 2006 4:34 pm    Post subject: Reply with quote

garris0n wrote:
froke wrote:
And then your mysql root password is stored in plain text in your .bash_history


export HISTFILE=/dev/null will prevent your current session's history from being saved.


FYI, history -c will clear your current bash history.
Back to top
View user's profile Send private message
vitec
n00b
n00b


Joined: 19 Jan 2004
Posts: 15

PostPosted: Tue Oct 03, 2006 5:02 am    Post subject: My root password is lost too Reply with quote

I have the same problem. I'm trying to reset my mysql's root password and he keeps saying that user ''@'localhost not found or something. Anyway, I'm re-emerging it to see if it fixes. But anyone knows a way to like "RESET" every sql based stuph? I can afford to loose the tables and the data for now! Actually that's not my concearn. I just need to add new tables and cannot remeber the pass. :cry:

OK I'm editing this because I've re-emerged mysql to install a MTA and got this answer:

mail etc # mysqladmin -u root -p create mailsql
Enter password:
mysqladmin: CREATE DATABASE failed; error: 'Access denied for user ''@'localhost' to database 'mailsql''
mail etc #

I'm following this guide http://www.gentoo.org/doc/en/virt-mail-howto.xml?style=printable
and on page 6 I stop because of that mysql problem.

I've used this guide on this machine before and it worked. There's something very wrong with my mysql server! :( Please HELP!
_________________
vitec
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum