1.首先通过find / - name my.cnf找到这个文件。在[mysqld]下面加上skip-grant-tables,此时你登录mysql就不需要密码了。
[mysqld]
default_password_lifetime=0
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
skip-grant-tables
[mysqld_safe]
log-error=/usr/local/mysql/logs/mysqld.log
pid-file=/usr/local/mysql/pids/mysqld.pid
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[client]
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
[mysql]
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
2.改完之后,你需要重启一下mysql。命令:service mysql restart
3.启动之后,登录mysql。命令 mysql -uroot -p
密码随便输入就行,进入之后输入 :use mysql,选择数据库。
4. 然后执行 update mysql.user set authentication_string = password('root') where user='root';
5.然后把my.cnf改回来重启一下mysql就好了。