Mysql适用于sudo,但没有。 (ubuntu 16.04,mysql 5.7.12-0ubuntu1.1)

时间:2016-06-29 11:40:34

标签: mysql database ubuntu mariadb sudo

我有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

4 个答案:

答案 0 :(得分:6)

此问题似乎主要是由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 :(得分:3)

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

$ mysqladmin -u root password
$ New password: 

或者如果你已经设置了root密码(我怀疑,否则你将无法通过sudo登录)那么它将是

$ mysqladmin -u root -p  password

与postgres不同,Mysql用户没有与unix用户链接。

答案 2 :(得分:1)

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

第1步:

 server@Server:~$ sudo -i

第2步:

 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.

第3步:

 mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Abc123123@';

输出:

 Query OK, 0 rows affected (0.00 sec)

答案 3 :(得分:0)

请尝试以下命令:

mysql -uroot -p[password]

[密码] - 输入安装mysql时设置的密码。

您也可以尝试:

sudo mysql -uroot -p[password]