How To Reset MySQL Password
If you are like me who tend to forget passwords almost every time, then this tip might help you reset your MySQL password just in case.
To begin, you must have access to the server, one that can stop and start the MySQL process, such as a root account. Sudo account can also be used, just as long that the user can control the MySQL service.
Begin the process by stopping the MySQL process. This can be done by executing /etc/init.d/mysqld stop command, but this can vary depending on how you setup your server. Also, be careful of users who might be using the MySQL service as stopping the service might disrupt their work.
Once the MySQL process is killed, execute the following command:
mysqld-safe --skip-grant-tables
This will run the MySQL process so you need to open another terminal to be able to reset the password. On the new terminal, you will now be able to login to MySQL to reset the password.
On the shell prompt of the new terminal, open the MySQL console:
mysql
mysql> use mysql;
mysql> UPDATE user SET Password=PASSWORD(‘YOUR_NEW_PASSWORD’) WHERE Host=’localhost’ AND User=’root’;
mysql> exit
This will change the password for the user ‘root’. Kill the MySQL process running in the first terminal, either by killing the PID or executing CTRL+C. Then start the process again, this time with the proper procedure:
/etc/init.d/mysqld start
Try logging in to the MySQL console using the new password:
mysql –uroot -p
You should now be able to use the new password for your MySQL.
Tip: Add User and Generate Password Script
Hey guys!
Finally, I found time to update this lovely blog of mine. And luckily, I have one super cool simple script to share. This script will help a lot of system administrators in adding user accounts, very useful if you have about a hundred servers to login to and execute useradd and passwd again and again. This is a bash script, so no need to install packages and easy to understand.
Here goes the code.
#!/bin/bash
for USER in $(cat /tmp/users.txt) do
$(useradd $USER)
###Remove the space between 8 and )
PASSWORD=$(openssl rand -base64 8 )
###passwd [double dash] stdin
echo $PASSWORD | $(passwd –stdin $USER)
echo “$USER $PASSWORD” | mail -s “Your Account Info” $USER@company.com
sleep 3
done
Here is a quick summary of the code per line:
for USER in $(cat /tmp/users.txt) do
This line begins the for loop and /tmp/users.txt should be a text file containing the list of usernames to be added one username per line. The value of $USER
$(useradd $USER)
This line will begin by executing the command useradd. If the script fails at this point because the username already exists, it will proceed with changing the password of the username.
###Remove the space between 8 and )
###passwd [double dash] stdin
PASSWORD=$(openssl rand -base64 8 )
echo $PASSWORD | $(passwd --stdin $USER)
This line generates an 8-byte random password using openssl and pipes it to command passwd using –stdin option so the generated password can be passed.
echo "$USER $PASSWORD" | mail -s "Your Account Info" $USER@company.com
This line sends an email to the user containing the username and password, assuming that the email address is the same as the username.
This script can be further enhanced (imagination is limitless!) and feel free to share your script here.
Search PinoyTux
Subscribe to Email Feeds
Blog Lounge
Popular Posts
Recent Posts
Drop your Card Here
Recent Comments
- smeaferrepove on Howto: Install yum On RHEL 4
- Anidich1 on Tip: Add User and Generate Password Script
- Tom S on Cebu Pacific Sucks
- kadersardar on PinoyTux Spreads Some CommentLuv
- Steve on Creative Labs Threatens Third Party Driver Modder








