Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Discussion & Documentation Documentation, Tips & Tricks
  • Search

MySQL root password recovery script

Unofficial documentation for various parts of Gentoo Linux. Note: This is not a support forum.
Post Reply
Advanced search
16 posts • Page 1 of 1
Author
Message
Wilhelm
Tux's lil' helper
Tux's lil' helper
Posts: 149
Joined: Tue May 27, 2003 9:48 am

MySQL root password recovery script

  • Quote

Post by Wilhelm » Thu Mar 25, 2004 6:19 pm

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: Select all

#!/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
Top
froke
Apprentice
Apprentice
User avatar
Posts: 174
Joined: Tue Dec 16, 2003 9:37 pm
Location: Westside USA

  • Quote

Post by froke » Thu Mar 25, 2004 11:47 pm

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
Top
kronon
Apprentice
Apprentice
User avatar
Posts: 212
Joined: Mon Aug 11, 2003 5:46 pm
Location: NL

  • Quote

Post by kronon » Fri Mar 26, 2004 8:41 am

If you are THAT paranoid then you could delete the bash history.
Top
Wilhelm
Tux's lil' helper
Tux's lil' helper
Posts: 149
Joined: Tue May 27, 2003 9:48 am

  • Quote

Post by Wilhelm » Tue Mar 30, 2004 6:43 pm

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.
Top
froke
Apprentice
Apprentice
User avatar
Posts: 174
Joined: Tue Dec 16, 2003 9:37 pm
Location: Westside USA

  • Quote

Post by froke » Tue Mar 30, 2004 10:50 pm

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
Top
Wilhelm
Tux's lil' helper
Tux's lil' helper
Posts: 149
Joined: Tue May 27, 2003 9:48 am

  • Quote

Post by Wilhelm » Mon Apr 12, 2004 1:00 am

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.
Top
froke
Apprentice
Apprentice
User avatar
Posts: 174
Joined: Tue Dec 16, 2003 9:37 pm
Location: Westside USA

  • Quote

Post by froke » Mon Apr 12, 2004 2:35 am

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
Top
verbatim
Apprentice
Apprentice
Posts: 223
Joined: Thu Mar 13, 2003 6:16 am

  • Quote

Post by verbatim » Mon Apr 12, 2004 3:30 am

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. :)
Top
linuxkrn
Tux's lil' helper
Tux's lil' helper
User avatar
Posts: 140
Joined: Mon Oct 13, 2003 3:08 am
Location: Denver,Colorado
Contact:
Contact linuxkrn
Website

  • Quote

Post by linuxkrn » Tue Apr 13, 2004 6:39 pm

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: Select all

#!/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: Select all

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.
Top
sburnett
Tux's lil' helper
Tux's lil' helper
Posts: 79
Joined: Mon Jun 10, 2002 11:00 pm
Location: USA

  • Quote

Post by sburnett » Fri Apr 16, 2004 9:35 pm

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: Select all

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.
Top
tilos
n00b
n00b
Posts: 1
Joined: Fri Sep 03, 2004 6:15 am

  • Quote

Post by tilos » Fri Sep 03, 2004 6:20 am

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
Top
beandog
Bodhisattva
Bodhisattva
User avatar
Posts: 2074
Joined: Sun May 04, 2003 11:53 pm
Location: /usa/utah
Contact:
Contact beandog
Website

  • Quote

Post by beandog » Sat Oct 16, 2004 6:04 am

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: Select all

$ 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. dvds | blurays | blog | wiki
Top
garris0n
n00b
n00b
User avatar
Posts: 61
Joined: Wed Dec 10, 2003 6:14 am
Location: Brooklyn, NY
Contact:
Contact garris0n
Website

  • Quote

Post by garris0n » Thu Apr 07, 2005 4:05 pm

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.
Top
beatryder
Veteran
Veteran
User avatar
Posts: 1138
Joined: Fri Apr 08, 2005 12:27 am
Contact:
Contact beatryder
Website

  • Quote

Post by beatryder » Mon May 08, 2006 9:28 pm

This is pure gold!
Dont make it idiot proof, make it work.
Neucode.org
<suppressed key>
Top
umrguy76
n00b
n00b
Posts: 2
Joined: Thu Jun 19, 2003 11:14 pm

  • Quote

Post by umrguy76 » Wed Jun 07, 2006 4:34 pm

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.
Top
vitec
n00b
n00b
Posts: 15
Joined: Mon Jan 19, 2004 12:09 am

My root password is lost too

  • Quote

Post by vitec » Tue Oct 03, 2006 5:02 am

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- ... =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
Top
Post Reply

16 posts • Page 1 of 1

Return to “Documentation, Tips & Tricks”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy