Mysql works with sudo, but without not. (ubuntu 16.04, mysql 5.7.12-0ubuntu1.1)
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;
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.
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)