MySQL: How to reset or change the MySQL root password?
P粉254077747
2023-08-23 17:20:27
<p>How to change MySQL root password and username in ubuntu server? Do I need to stop the mysql service before making any changes? </p>
<p>I also have phpmyadmin setup, will phpmyadmin update automatically? </p>
The only method that worked for me was the one described here (I'm running ubuntu 14.04). For clarity, I'm following these steps:
sudo vim /etc/mysql/my.cnf
Add the following lines at the end:
sudo service mysql restart
mysql -u root
Use mysql
select * from mysql.user where user = 'root';
- Look at the top to determine whether the password column is called Password or authentication_stringUPDATE mysql.user set *password_field from above* = PASSWORD('your_new_password') where user = 'root' and host = 'localhost';
- Use the correct password from aboveRefresh permissions;
quit
sudo vim /etc/mysql/my.cnf
If you want to maintain security standards, delete the line added in step 2.
sudo service mysql restart
For reference: https://dev.mysql.com /doc/refman/5.7/en/resetting-permissions.html
Set/change/reset MySQL root password on Ubuntu Linux. Enter the following lines in the terminal.
sudo /etc/init.d/mysql stop
/var/run/mysqld
does not exist, you must create it first:sudo mkdir -v /var/run/ mysqld && sudo chown mysql /var/run/mysqld
mysqld
Configuration:sudo mysqld --skip-grant-tables &
mysql -u root mysql
YOURNEWPASSWORD
with your new password:For MySQL
If your MySQL uses the new authentication plugin, you need to use:
update user set plugin="mysql_native_password" where User='root';
before refreshing permissions.Note: This method is not considered the most secure way to reset your password, but it does work.
For MySQL >= 8.0
last step:
As pointed out in @lambart's comment, you may need to kill the temporary passwordless mysql process you started, i.e.
sudo Killall -9 mysqld
, and then start the normal daemon:sudo Service mysql start
references: