先要跳过密码登录
[root@VM-0-9-centos ~]# cd /etc/
[root@VM-0-9-centos etc]# cat my.cnf
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
[mysqld]
skip-grant-tables #mysqld下面添加这行
port = 3306
user = mysql
datadir = /data/mysql
socket = /var/run/mysqld/mysqld.sock
log-bin = /data/mysql/mysql-bin
pid-file = /var/run/mysqld/mysqld.pid
log_error = /var/log/mysql/mysql-error.log
slow_query_log_file = /var/log/mysql/mysql-slow.log
server-id = 1
skip-name-resolve
旧密码置空
//将旧密码置空
mysql -u root -p //提示输入密码时直接敲回车。
//选择数据库
use mysql
//将密码置空
update user set authentication_string = '' where user = 'root';
//刷新
FLUSH PRIVILEGES;
删除跳过密码
://去除免密码登陆
删掉my.cnf skip-grant-tables
重启服务
[root@VM-0-9-centos etc]# systemctl restart mysqld.service
[root@VM-0-9-centos etc]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2021-05-17 11:29:39 CST; 4s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 27008 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 27034 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/mysqld.service
└─27034 /usr/sbin/mysqld
May 17 11:29:38 VM-0-9-centos systemd[1]: Stopped MySQL Server.
May 17 11:29:38 VM-0-9-centos systemd[1]: Starting MySQL Server...
May 17 11:29:39 VM-0-9-centos systemd[1]: Started MySQL Server.
登录mysql设置密码
[root@VM-0-9-centos etc]# mysql -u root -p
密码置空了 直接回车登录
mysql> SELECT Host, User, plugin from user;
+-----------+------------------+-----------------------+
| Host | User | plugin |
+-----------+------------------+-----------------------+
| % | root | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
+-----------+------------------+-----------------------+
4 rows in set (0.00 sec)
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '自己的密码';
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT Host, User, plugin from user;
+-----------+------------------+-----------------------+
| Host | User | plugin |
+-----------+------------------+-----------------------+
| % | root | mysql_native_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
+-----------+------------------+-----------------------+
4 rows in set (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye
然后就可以用navicat登录了