P粉3065239692023-08-21 00:54:32
The only way that worked for me was the one described here (I'm running ubuntu 14.04). For clarity, here are the steps I followed:
sudo vim /etc/mysql/my.cnf
Add the following lines at the end:
[mysqld] skip-grant-tables
sudo service mysql restart
mysql -u root
use mysql
select * from mysql.user where user = 'root';
- look at the top to determine if the password column is called password or authentication_string
UPDATE mysql.user set *password_field from above* = PASSWORD('your_new_password') where user = 'root' and host = 'localhost';
- Use the correct password field from above
FLUSH PRIVILEGES;
exit
sudo vim /etc/mysql/my.cnf
If you want to maintain security standards, delete the line added in step 2.
sudo service mysql restart
Reference: https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
P粉1357999492023-08-21 00:52:22
Set/change/reset MySQL's root password on Ubuntu Linux. Enter the following command in the terminal.
sudo /etc/init.d/mysql stop
/var/run/mysqld
does not exist, you need to 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 < 8.0
UPDATE mysql.user SET Password = PASSWORD('YOURNEWPASSWORD') WHERE User = 'root'; FLUSH PRIVILEGES;
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 works.
For MySQL >= 8.0
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YOURNEWPASSWORD';
FLUSH PRIVILEGES;
As mentioned 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: