- vim /etc/my.cnf
注意不同的系統(tǒng)路徑不太一樣,也有可能是 /etc/my.cnf.d/mysql-server.cnf,根據(jù)自己的實(shí)際情況來(lái)
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid
# 但都是要在[myqld]這個(gè)節(jié)點(diǎn)下添加
skip-grant-tables
- 重啟mysql服務(wù):service mysqld restart
- 免密進(jìn)入mysql
[root@centos8.2 dir]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.26 Source distribution
Copyright (c) 2000, 2021, 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> 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
- 查詢r(jià)oot用戶
mysql> select user,authentication_string,host,plugin from user;

image.png
- 將root的密碼置為空
mysql> update user set authentication_string='' where user='root';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
- 然后將第一步文件中的skip-grant-tables去掉,重啟mysql服務(wù): service mysqld restart
- 現(xiàn)在就可以了無(wú)密碼進(jìn)入msql了
mysql
//或者
mysql -u root -p
password直接回車(chē)
- 設(shè)置mysql的新密碼
# 執(zhí)行這個(gè)
ALTER user 'root'@'localhost' IDENTIFIED with mysql_native_password BY '123456'
# 如果上面的報(bào)錯(cuò),改為
ALTER user 'root'@'%' IDENTIFIED with mysql_native_password BY '123456'