在Ubuntu上重新获得对MySQL服务器的root访问权限

我试图重新获得在Ubuntu实例(LTS 16.04.2)上运行的MySQL数据库的root访问权限。 问题情况是:我可以使用/etc/init.d/mysql启动和停止MySQL但我无法登录

 mysql -u root mysql -u root -p 

后者用空密码。 我的密码存储区中有一条空记录。 我相信要记住,没有密码就可以登录,但也许我忘了保存我的密码存储。 也许系统也受到了损害。 但是,由于我具有root访问权限,因此应该可以重置密码。

我决定使用启动MySQL服务器

 sudo -u mysql /usr/sbin/mysqld --skip-grant-tables --skip-networking 

命令,现在可以与客户端连接。 根据教程 ,我必须发出这两个命令

 FLUSH PRIVILEGES; SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass'); 

设置密码。 发出第二个命令时,我在启动服务器的控制台上收到以下警告:

 [Warning] 'user' entry 'root@localhost' has both a password and an authentication plugin specified. The password will be ignored. 

我不记得曾经设置任何插件。 这个Ubuntu是默认的吗? 那么我的MySQL密码存储在哪里,我该如何更改呢? 或者我如何禁用我不知道的插件。

 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass'); 

……是错误的命令。 在2015年,该列已弃用,并替换为“authentication_string”列。

它应该是

 UPDATE user SET authentication_string=PASSWORD('MyNewPass') where USER='root'; 

skip-grant-tables之后需要做的是连接到数据库并使用mysql数据库。

完全如此:

 sudo systemctl stop mysql.service sudo mysqld_safe --skip-grant-tables --skip-networking & mysql -u root use mysql; FLUSH PRIVILEGES; UPDATE user SET authentication_string = PASSWORD('newpass') WHERE User = 'root' AND Host = 'localhost'; \q sudo killall mysqld sudo systemctl start mysql.service