目录
一、文字叙述操作步骤
-
修改配置文件
vi /etc/my.cnf
-
在 [mysqld] 下 skip-grant-tables # 添加该语句:忽略mysql权限问题,直接登录
-
-
重启mysql
service mysql restart
-
免密登录mysql
mysql -u root -p # 直接回车
-
使用mysql表
use mysql;
-
执行如下语句
-
select host, user, authentication_string, plugin from user; # 查看一下是否可以执行成功
-
update user set host='%',plugin='mysql_native_password',authentication_string='' where user='root';
-
-
再次编辑配置文件
vi /etc/my.cnf
-
在 [mysqld] 下注释掉刚才添加的语句 #skip-grant-tables
-
-
再次重启mysql
service mysql restart
-
免密登录mysql
mysql -u root -p # 直接回车
-
执行如下语句设置你的密码
ALTER user'root'@'%' IDENTIFIED BY '你要设置的密码';
-
设置成功,切记要刷新一下
flush privileges;
-
再次执行登录mysql命令,输入密码,可以发现登录成功!
mysql -u root -p # 输入你刚才设置的密码
二、linux实际操作演示
[root@hadoop01 /]# vi /etc/my.cnf # 修改配置文件
[mysqld]
skip-grant-tables # 添加该语句:忽略mysql权限问题,直接登录
[root@hadoop01 /]# cd /export/servers/mysql-8.0.33/ # 进到我安装的mysql目录,进入mysql
[root@hadoop01 mysql-8.0.33]# mysql -u root -p
Enter password:
mysql> use mysql
mysql> select host, user, authentication_string, plugin from user;
mysql> update user set host='%',plugin='mysql_native_password',authentication_string='' where user='root';
mysql> quit
[root@hadoop01 mysql-8.0.33]# vi /etc/my.cnf
[mysqld]
# skip-grant-tables # 把该语句注释掉,或删除
[root@hadoop01 mysql-8.0.33]# service mysql restart # 重启mysql
[root@hadoop01 mysql-8.0.33]# mysql -u root -p
mysql> ALTER user'root'@'%' IDENTIFIED BY '你要设置的密码'; # 设置密码
mysql> flush privileges; # 切记一定要刷新一下
mysql> exit
[root@hadoop01 mysql-8.0.33]# mysql -u root -p # 再次进入mysql,输入密码进去了!!
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.33 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>