一般的安装mysql-server ,我们这么安装就好了
~$ sudo apt install mysql-server ~$ mysql -uroot -p Enter password: ERROR 1698 (28000): Access denied for user 'root'@'localhost'
这里不能登录的原因在于:初始情况下 root 账户没有密码,这时普通用户并不能直接使用 mysql 命令直接控制台登录,更不能使用 mysql-workbench 直接连接登录。主要是由于 mysql.user 这张表中 root 用户的 plugin 字段值为 auth_socket,改为 mysql_native_password 即可。同时为了方便之后使用,我们在接下来的操作中顺带给 root 账户设置密码。
那么怎么解决这个问题呢?
修复开始,毕竟phpmyadmin 用起来还是比较高效率的。贴一段我终端中的log吧
~$ sudo mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu) Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. 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命令行后 输入 查询命令 要用分号;结尾。我第一个查询表 就忘记了。 mysql> show databases -> ; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) 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, host,plugin,authentication_string from user; +------------------+--------------+-----------------------+ | user | host | plugin | authentication_string | +------+-----------+--------------+-----------------------+ | root | localhost | auth_socket | | +------+-----------+--------------+-----------------------+ 4 rows in set (0.00 sec) ## 这里把其它3行信息略过了,本次修改与其它3行没有关系 mysql> update user set plugin="mysql_native_password", authentication_string=PASSWORD("12345678") where user="root"; Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> exit; Bye ~$ sudo service mysql restart ~$ mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu) Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. 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> exit Bye ~$
这样 就修复好了,总结如下:
超级用户执行 mysql,更改 user表中root行的相关信息,重启mysql服务,再测试
sudo mysql
mysql> update user set plugin="mysql_native_password", authentication_string=PASSWORD("12345678") where user="root"; ~$ sudo service mysql restart ~$ mysql -uroot -p
需要注意的是 进入 mysql>后输入的命令记得用英文分号结束,密码不要照抄我的12345678,这是弱口令,只是演示用。