Questions are as follows:
wangju@wangju-HP-348-G4:~$ mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
The solution: two steps
- Mysql configuration can log on without a password
- Reconfigure mysql user name and password
First of all: mysql configuration can log on without a password
step1:
Use find global search mysql configuration file
root@wangju-HP-348-G4:/home/wangju# find / -name mysqld.cnf
/etc/mysql/mysql.conf.d/mysqld.cnf
step2:
In mysql configuration file [mysqld] was added in the following sentence:
skip-grant-tables <-- add here
Role: is to let you log into mysql password can not.
step3:
Save changes, restart mysql
service mysql restart
step4:
Mysql input on a terminal -u root -p, met directly enter prompted to enter a password to enter mysql
Then: Reconfigure mysql username and password
step1:
Perform the following three sentences are configured root user password after entering mysql
use MySQL; then hit Enter to
Update the User the SET authentication_string = password ( "your password") the WHERE the User = "root"; then hit the Enter
flush privileges ; then hit Enter
The results are as follows:
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
mysql> update user set authentication_string=password("admin123456") where user="root";
Query OK, 1 row affected, 1 warning (0.02 sec)
Rows matched: 2 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
Then enter quit, quit mysql.
step2:
Mysqld.cnf to re-enter the beginning of the file to add the skip-grant-tables to comment out this statement.
Back input terminal MySQL -u -p the root , given:
root@wangju-HP-348-G4:/home/wangju# mysql -u root -p
Enter password:
ERROR 1524 (HY000): Plugin 'auth_socket' is not loaded
再把刚才注释掉的skip-external-locking取消注释,然后重启mysql
执行下面的命令:
use mysql;
#切换数据库 select user,plugin from user; #查询user表 user列,plugin列的值 #从查询结果可以看出plugin root的字段是auth_socket,把它改为mysql_native_password update user set authentication_string=password("admin123456"),plugin='mysql_native_password' where user='root'; #更新user表plugin root
执行结果:
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
mysql> select user,plugin from user;
+------------------+-----------------------+
| user | plugin |
+------------------+-----------------------+
| root | auth_socket |
| mysql.session | mysql_native_password |
| mysql.sys | mysql_native_password |
| debian-sys-maint | mysql_native_password |
| root | mysql_native_password |
| tester | mysql_native_password |
+------------------+-----------------------+
6 rows in set (0.00 sec)
mysql> update user set authentication_string=password("admin123456"),plugin='mysql_native_password' where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 2 Changed: 1 Warnings: 1
再输入select user,plugin from user;回车,我们能看到root用户的字段改成功了。
mysql> select user,plugin from user;
+------------------+-----------------------+
| user | plugin |
+------------------+-----------------------+
| root | mysql_native_password |
| mysql.session | mysql_native_password |
| mysql.sys | mysql_native_password |
| debian-sys-maint | mysql_native_password |
| root | mysql_native_password |
| tester | mysql_native_password |
+------------------+-----------------------+
6 rows in set (0.00 sec)
step3:
quit退出,再执行step3,重启mysql
结果:
再用mysql -u root -p,回车,输入密码之后,就可以登录成功了
参考文档: