首次安装mysql8后,不知道root密码是什么?可以通过以下方式设置root密码

1.修改配置文件my.cnf

[root@localhost ~]# vi /etc/my.cnf
找到mysqld在之后添加
skip-grant-tables
保存退出

2. 重启mysql服务(必须重启后才能不需要密码进入)

systemctl restart mysqld

3.直接登陆mysql而不需要密码

mysql -u root (直接点击回车)

4. 先将authentication_string设置为空

update user set authentication_string = '' where user = 'root';

5. 然后退出mysql,删除/etc/my.cnf的skip-grant-tables,重启mysqld服务

6. 接着尝试登录mysql

mysql -uroot -p

直接回车登录mysql,再使用alter修改用户密码

alter user 'root'@'localhost' identified by '你的密码';

提示成功!!!

接着退出mysql后,重新登陆root,就可以输入密码了