Mysql需要使用sudo才能运行,否则无法启动。(Ubuntu 16.04,Mysql 5.7.12-0ubuntu1.1)

14

我有Ubuntu 16.04Mysql 5.7.12-0ubuntu1.1。当我输入:

sudo mysql -u root

我可以登录MySQL控制台,但当我不使用 sudo 时,会出现以下情况:

mysql -u root

我遇到了错误:

ERROR 1698 (28000): Access denied for user 'root'@'localhost'

在我安装和卸载 MariaDB 时出现了问题。我记得在 PostgreSQL 中,登录到数据库的 Unix 用户非常重要,但在 MySQL 中如何处理呢?


我按照以下步骤解决了这个问题:

https://askubuntu.com/questions/766334/cant-login-as-mysql-user-root-from-normal-user-account-in-ubuntu-16-04


Ubuntu 18.04 -- 我发现 https://askubuntu.com/questions/766334/cant-login-as-mysql-user-root-from-normal-user-account-in-ubuntu-16-04 对我有用。 - Eric
4个回答

9
这个问题似乎主要是由于auth_socket插件引起的,默认情况下,如果root用户没有密码,则会使用该插件。(以前,apt-get安装过程会要求输入root密码,但现在似乎不再需要,因此启用了auth_socket。)
对于任何一个查询,首先使用sudo mysql登录为root用户。
对于MySQL或MariaDB >= 10.2:
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'test';

对于那些使用 MariaDB < 10.2(不支持 ALTER USER)的用户,您需要运行此查询:

    SET PASSWORD = PASSWORD('test');
    update mysql.user set plugin = 'mysql_native_password' where User='root';
    FLUSH PRIVILEGES;

1
这是正确的操作方式,你只需要刷新权限表,这样MySQL就不会出问题。不需要进行任何密码重置。 - Mike

4

解决方案是为root mysql账户提供密码(如果您尚未这样做)。您收到的错误消息是因为需要密码,而您尚未提供。使用以下命令重置root密码:

$ mysqladmin -u root password
$ New password: 

如果你已经设置了 root 密码(我认为可能性不大,否则你就无法通过 sudo 登录),那么密码应该是:
$ mysqladmin -u root -p  password

与Postgres不同,Mysql用户与Unix用户没有关联。


1
OP使用并且对我也起作用的解决方案是:ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'test'; 仅供其他路过此处的人参考。 - thelogix
使用 auth_socket 插件,可以将 MySQL 用户链接到 Unix 用户。 - Loren

2

服务器版本: 5.7.18-0ubuntu0.16.04.1 (Ubuntu)

步骤1:

 server@Server:~$ sudo -i

步骤二:

 root@Server:~# mysql

输出结果如下:
 server@Server:~$ sudo -i
 [sudo] password for server: 
 root@Server:~# mysql
 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 16
 Server version: 5.7.18-0ubuntu0.16.04.1 (Ubuntu)

 Copyright (c) 2000, 2017, 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> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Abc123123@';

输出:

 Query OK, 0 rows affected (0.00 sec)

1
请尝试以下命令:

mysql -uroot -p[password]

[密码] - 输入您在安装MySQL时设置的密码。

您也可以尝试:

sudo mysql -uroot -p[password]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接