MySQL: Difference between revisions
Jump to navigation
Jump to search
Line 34: | Line 34: | ||
<source lang="bash"> |
<source lang="bash"> |
||
sudo |
sudo service mysql stop # Stop MySQL server |
||
sudo mysqld_safe --skip-grant-tables & # Restart it with option not to ask for passwords |
sudo mysqld_safe --skip-grant-tables & # Restart it with option not to ask for passwords |
||
sudo su |
sudo su |
||
Line 41: | Line 41: | ||
Apply the MySQL script above: |
Apply the MySQL script above: |
||
<source lang="mysql"> |
<source lang="mysql"> |
||
update user set password=PASSWORD("NEWPWD") where User=' |
update user set password=PASSWORD("NEWPWD") where User='root'; |
||
flush privileges; |
flush privileges; |
||
quit |
quit |
||
Line 47: | Line 47: | ||
Then restart the server: |
Then restart the server: |
||
<source lang="bash"> |
<source lang="bash"> |
||
sudo /etc/init.d/mysql |
sudo /etc/init.d/mysql stop |
||
sudo service mysql start |
|||
</source> |
</source> |
Revision as of 06:58, 24 June 2015
References
- http://www.cyberciti.biz/faq/mysql-change-root-password/
- http://www.cyberciti.biz/tips/recover-mysql-root-password.html
Passwords
Change
Using mysqladmin:
unset HISTFILE # <-- DO NOT FORGET IT, OR PWD WILL APPEAR IN ~/.bash_history
mysqladmin -u USERNAME password NEWPWD # Assumes no password set - use user=root for admin pwd
mysqladmin -u USERNAME -p'OLDPWD' password NEWPWD
If you forget to unset HISTFILE, delete your history file immediately:
rm ~/.bash_history
Using MySQL commands. First we connect to MySQL server and select table mysql (don't forget to DISABLE HISTORY FILE !!!):
MYSQL_HISTFILE=/dev/null mysql -u root -p mysql
Here the script:
update user set password=PASSWORD("NEWPWD") where User='USERNAME';
flush privileges;
quit
Recover root password
If the MySQL root password is lost, the same script can be used to define a new password, but it requires to restart the MySQL server with option --skip-grant-tables:
sudo service mysql stop # Stop MySQL server
sudo mysqld_safe --skip-grant-tables & # Restart it with option not to ask for passwords
sudo su
MYSQL_HISTFILE=/dev/null mysql -u root mysql # Connect to MySQL, table mysql
Apply the MySQL script above:
update user set password=PASSWORD("NEWPWD") where User='root';
flush privileges;
quit
Then restart the server:
sudo /etc/init.d/mysql stop
sudo service mysql start