问题产生背景:
安装完 最新版的 mysql8.0.13后忘记了密码,向重置root密码;找了网上好多资料都不尽相同,根据自己的问题总结如下:
第一步:修改配置文件免密码登录mysql
vi /etc/my.cnf
1.2 在 [mysqld]最后加上如下语句 并保持退出文件;
skip-grant-tables
1.3 重启mysql服务:
systemctl restart mysqld.service 重启
systemctl start mysqld.service 启动
systemctl stop mysqld.service 关闭
service mysqld restart 重启
第二步免密码登录到mysql上;直接在命令行上输入:
mysql
//或者
mysql -u root -p
//password直接回车
第三步: 给root用户重置密码;
1.4 首先查看当前root用户相关信息,在mysql数据库的user表中;
select host, user, authentication_string, plugin from user;
host: 允许用户登录的ip‘位置’%表示可以远程;
user:当前数据库的用户名;
authentication_string: 用户密码;在mysql 5.7.9以后废弃了password字段和password()函数;
plugin: 密码加密方式;
1.5 如果当前root用户authentication_string字段下有内容,先将其设置为空;
use mysql;
update user set authentication_string='' "where user='root';
1.6退出mysql, 删除/etc/my.cnf文件最后的 skip-grant-tables 重启mysql服务;
1.7 使用root用户进行登录,因为上面设置了authentication_string为空,所以可以免密码登录;
mysql -u root -p
passwrod:直接回车;
1.8修改密码策略
set global validate_password.policy=0; 修改策略允许使用简易密码
set global validate_password.length=6; 修改密码长度为6位
1.9使用ALTER修改root用户密码;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
至此修改成功; 从新使用用户名密码登录即可
远程连接
修改root 远程权限 update user set host = "%" where user='root';
查看用户权限 select host, user, authentication_string, plugin from user;
linnx关闭防火墙:
1.安装iptables:yum install -y iptables-services
2.修改配置文件 vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
3.关闭 防火墙
service iptables stop
systemctl stop firewalld
sudo systemctl stop firewalld.service
sudo systemctl disable firewalld.service