service mysql status //查看mysql状态
service mysql start //启动MySQL
mysql -uroot -p
当输入第三行命令按下回车发现出现ERROR1698(28000):Access denied for user root@localhost时,应该采取办法:
1.在终端继续输入
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
按i进入编辑模式,在[mysqld]这一块中加入skip-grant-tables这句话,按ESC退出编辑,按“:”+wq保存退出。
2.在终端重新输入
mysql -uroot -p
回车进入mysql界面,在该界面输入命令
use mysql;
update user set authentication_string=password("此处填你设置的密码") where user="root";
flush privileges;
quit //退出mysql
3.重新进入到mysqld.cnf文件中去把刚开始加的skip-grant-tables这条语句给注释掉。命令为:
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
进入该文件按i进入编辑模式
#skip-grant-tables
按ESC退出编辑,按“:”+wq保存退出
4.重启MySQL,重新进入mysql
service mysql stop
service mysql start
mysql -uroot -p
如果输入上面三行命令仍出现ERROR1698(28000):Access denied for user root@localhost,此时需重新进入mysqld.cnf,将#skip-grant-tables前面的#删除,保存退出。
5.重新输入mysql -uroot -p,回车进入mysql,在该界面输入:
use mysql;
select user,plugin from user;
此时如果表格中root对应的plugin root的字段是auth_socket,那我们需要将它为下面的mysql_native_password就行了。命令为:
update user set authentication_string=password("此处为你设置的密码"),plugin='mysql_native_password' where user='root';
quit //退出mysql
6.重新进入到mysqld.cnf文件中去把刚开始加的skip-grant-tables这条语句给注释掉。
7.再次关闭mysql重新启动即可。
service mysql stop
service mysql start
mysql -uroot -p
按回车输入你设置的密码即可进入mysql.
参考文章:
(1)mysql出现ERROR1698(28000):Access denied for user root@localhost错误解决方法:https://www.itdaan.com/blog/2018/09/04/402f99d0ecd2c3aa8ed2d652d58d202c.html