Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql works with sudo, but without not. (ubuntu 16.04, mysql 5.7.12-0ubuntu1.1)

I have Ubuntu 16.04, and Mysql 5.7.12-0ubuntu1.1. When I type:

sudo mysql -u root

I can login into mysql console, but when I type it without sudo:

mysql -u root

I obtain error:

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

My problem occurred when I installed and removed MariaDB. I remember that in PostgreSQL there is important which unix user login to database, but how to handle with this in Mysql?


I solved this problem following by:

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

like image 530
Daniel Avatar asked Jun 29 '16 11:06

Daniel


3 Answers

This problem seems to be primarily caused by the auth_socket plugin which is now used by default if the root user doesn't have a password. (Formerly, the apt-get install process asked for a password for root, but it doesn't seem to do that anymore so auth_socket gets enabled.)

For either query, first login as root by using sudo mysql

For MySQL or MariaDB >= 10.2:

    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'test';

For others who may be using MariaDB < 10.2 (which doesn't support ALTER USER), you'll want to run this query:

    SET PASSWORD = PASSWORD('test');
    update mysql.user set plugin = 'mysql_native_password' where User='root';
    FLUSH PRIVILEGES;
like image 129
Loren Avatar answered Oct 16 '22 14:10

Loren


The solution is to provide a password for the root mysql account (if you've not done so already). The error message you're receiving is because a password is required, and you have not provided it. Reset root password with:

$ mysqladmin -u root password
$ New password: 

or if you'd already set a root password (which I doubt, otherwise you wouldn't be able to log in via sudo) then it would be

$ mysqladmin -u root -p  password

Mysql users are not linked with unix users, unlike postgres.

like image 4
Simon Woolf Avatar answered Oct 16 '22 15:10

Simon Woolf


Server version: 5.7.18-0ubuntu0.16.04.1 (Ubuntu)

Step 1:

 server@Server:~$ sudo -i

Step 2:

 root@Server:~# mysql

Output looks like this:

 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.

Step 3:

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

Output:

 Query OK, 0 rows affected (0.00 sec)
like image 2
Sam Avatar answered Oct 16 '22 14:10

Sam