刚安装好的mysql需要对用户进行管理,要不就会出现root不需要密码就可以登录,和不输入用户名和密码就可以直接登录;
第一步:进入mysql库查看user表,发现root没密码和两个空白用户
mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select User,Password from user; +------+----------+ | User | Password | +------+----------+ | root | | | root | | | root | | | root | | | | | | | | +------+----------+ 6 rows in set (0.00 sec)
第二步:删除空白用户
mysql> update user set Password="kongzhi123456" where User="root"; Query OK, 4 rows affected (0.05 sec) Rows matched: 4 Changed: 4 Warnings: 0 mysql> delete from user where User=‘‘; Query OK, 2 rows affected (0.00 sec)
第三步:修改root密码
方法1: 用SET PASSWORD命令 首先登录MySQL。 格式:mysql> set password for 用户名@localhost = password(‘新密码‘); 例子:mysql> set password for root@localhost = password(‘123‘); 方法2:用UPDATE直接编辑user表 首先登录MySQL。 mysql> use mysql; mysql> update user set password=password(‘123‘) where user=‘root‘ and host=‘localhost‘; mysql> flush privileges;
第四步:重置权限表,或者重新启动
mysql> flush privileges; Query OK, 0 rows affected (0.05 sec)
#重新启动 root 23:51:22 ~$ service mysqld restart Redirecting to /bin/systemctl restart mysqld.service