Change the password of root in mysql version 8
mysqld_safe#
You maybe need sudo
in your case.
1# turn off mysqld
2/etc/init.d/mysql stop
3
4# use safe mode
5mysqld_safe --skip-grant-tables &
If it show the error message:
1mysqld_safe Logging to '/var/log/mysql/error.log'.
2mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
Try this before run the safe mode of mysqld:
1mkdir -p /var/run/mysqld
2chown mysql:mysql /var/run/mysqld
Change password#
In this mode, you can login root without password.
1mysql -u root
In the old version(MySQL version lower than 8
), you may use SET PASSWORD=PASSWORD('password');
to change password.
But in version 8
, you need to change the method:
1USE mysql;
2ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
3FLUSH PRIVILEGES;
4exit;
Start the service
1killall mysqld
2/etc/init.d/mysqld start
Try it
1mysql -u root -p
2# type password