5.6版本:
update mysql.user set password=password('123456') where User="root" and Host = "localhost";
set password for root@localhost = password('123456');
5.7及以上版本
update mysql.user set authentication_string=password('123456') where User="root" and Host="localhost";
8.0以上版本:
alter user 'root'@'localhost' identified by 'root';
set password for root@localhost = '123456';
如果初始化后无法用密码登录,可以在/etc/my.cnf文件的[mysqld]下添加一行内容
skip-grant-tables
然后重启mysql服务
systemctl restart mysqld
再用上面的修改密码方法来修改密码
修改完后将之前在/etc/my.cnf中添加的内容注释掉,重启mysql服务后用修改后的密码来登录