Mysql 5.7 设置root账号密码并可以远程登录


1.跳过密码验证
如果首次安装没有密码,修改 /etc/my.cnf,在 [mysqld] 小节下添加一行:
skip-grant-tables=1
这行代码意思就是跳过跳过授权表,即是可以跳过密码验证直接进入数据库
找到 bind-address = 127.0.0.1 #注释
2.重置密码
重启 mysqld 服务:#service mysqld restart或者systemctl restart mysqld
mysql -uroot -p //此时直接回车,既可以进入数据库。
出现mysql>就说明你已经进入到mysql数据库里了。
update mysql.user set authentication_string=password(‘new password’) where user=’root’ ;
update mysql.user set authentication_string=PASSWORD('newPwd'), plugin='mysql_native_password',Host='%' where user='root';
在之前的版本中,密码字段的字段名是 password,5.7版本改为了 authentication_string
退出 mysql,编辑 /etc/my.cnf 文件,删除 skip-grant-tables=1 的内容
重启 mysqld 服务,再用新密码登录即可
另外,MySQL 5.7 在初始安装后(CentOS7 操作系统)会生成随机初始密码,并在 /var/log/mysqld.log 中有记录,
可以通过 cat 命令查看,找 password 关键字 ,找到密码后,在本机以初始密码登录,并且(也只能)通过
alter user ‘root’@’localhost’ identified by ‘root’ 命令,
修改 root 用户的密码为 root,然后退出,重新以root用户和刚设置的密码进行登录即可。
3.root配置远程登录
mysql> select host,user from user where user=’root’;//查看配置
mysql> grant all privileges on *.* to root@'%' identified by '123456' with grant option;//进行授权
mysql> flush privileges; //刷新生效
