Solution:
sudo vim /etc/mysql/my.cnf
Add to:
[mysqld]
skip-grant-tables
Save and exit.
Then execute the following two commands to restart the mysql service:
service mysqld stop
service mysqld start
Or directly execute the command to restart the service:
service mysqld restart
After the restart, the modification of the above configuration will take effect. At this time, you do not need to add sudo to log in.