Solve MySQL error 1698 (28000): access denied for user ‘root’ @’localhost ‘

I believe that many users who just installed MySQL on Linux will encounter this problem, how to solve it?I found the answer on StackOverflow (I used method 1 and it worked), carried it over and translated it into Chinese.
The original address: https://stackoverflow.com/questions/39281594/error-1698-28000-access-denied-for-user-rootlocalhost

Problem description: When I log in to the MySQL database via root, I get an ERROR “ERROR 1698 (28000): Access deniedfor user ‘root’ @’ localhost ‘”.

Answer:
This is because MySQL USES the UNIX Auth_Socket Plugin by default in recent Ubuntu installations (and possibly others).
In simple terms, this means that when DB_users use the database, they will be authenticated through the system user authentication table. You can see if your root user is set to this by using the following command:

$ sudo mysql -u root # I had to use "sudo" since is new installation

mysql> USE mysql;
mysql> SELECT User, Host, plugin FROM mysql.user;

+------------------+-----------------------+
| User             | plugin                |
+------------------+-----------------------+
| root             | auth_socket           |
| mysql.sys        | mysql_native_password |
| debian-sys-maint | mysql_native_password |
+------------------+-----------------------+

As you can see from the query, root is using the AUTH_socket plug-in. There are two ways to solve this problem:
1. You can set your root user to use mysql_native_password plug-in 2. You can create a new database user that is consistent with your system user (recommended)
(Note: Method 2 meets the requirements of auth_Socket plug-in)
Option 1:

$ sudo mysql -u root # I had to use "sudo" since is new installation

mysql> USE mysql;
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;

$ service mysql restart

Option 2 (substitute your operating system username for YOUR_SYSTEM_USER) :

$ sudo mysql -u root # I had to use "sudo" since is new installation

mysql> USE mysql;
mysql> CREATE USER 'YOUR_SYSTEM_USER'@'localhost' IDENTIFIED BY '';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'YOUR_SYSTEM_USER'@'localhost';
mysql> UPDATE user SET plugin='auth_socket' WHERE User='YOUR_SYSTEM_USER';
mysql> FLUSH PRIVILEGES;
mysql> exit;

$ service mysql restart

Remember that if you choose to use Method 2, you should connect to MySQL by using your operating system user name (mysql-u YOUR_SYSTEM_USER).
Note: In some operating systems (such as Debian), the ‘auth_Socket’ plugin is called ‘unix_socket’, so the corresponding SQL command statement should be UPDATE User SET plugin= ‘unix_socket’ WHERE user = ‘YOUR_SYSTEM_USER’.

Read More: