Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mysql

I've installed MySQL with the following version:

kurt@kurt-ThinkPad:~$ mysql -V
mysql  Ver 14.14 Distrib 5.7.15, for Linux (x86_64) using  EditLine wrapper

I recall I did not set a password for the root user during setup. However, if I try to simply start it using the mysql command, I get the following error:

kurt@kurt-ThinkPad:~$ mysql
ERROR 1045 (28000): Access denied for user 'kurt'@'localhost' (using password: NO)

Similarly, as a root user,

kurt@kurt-ThinkPad:~$ mysql -u root
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

I tried following the instructions on Resetting the Root Password: Unix and Unix-Like Systems; I made the text file in my home directory, and from there tried to start the MySQL server with the --init-file option. However, I again get a permissions error:

kurt@kurt-ThinkPad:~$ mysqld_safe --init-file=mysql-init &
[1] 18340
kurt@kurt-ThinkPad:~$ /usr/bin/mysqld_safe: 548: /usr/bin/mysqld_safe: cannot create /var/lib/mysql/mysqld_safe.pid: Permission denied
2016-09-20T14:57:04.753720Z mysqld_safe Logging to '/var/log/mysql/error.log'.
cat: /var/run/mysqld/mysqld.pid: Permission denied
rm: cannot remove '/var/run/mysqld/mysqld.pid': Permission denied
2016-09-20T14:57:04.780206Z mysqld_safe Fatal error: Can't remove the pid file:
/var/run/mysqld/mysqld.pid
Please remove it manually and start /usr/bin/mysqld_safe again;
mysqld daemon not started
/usr/bin/mysqld_safe: 135: /usr/bin/mysqld_safe: cannot create /var/log/mysql/error.log: Permission denied
rm: cannot remove '/var/lib/mysql/mysqld_safe.pid': Permission denied

I have read about a similar error in Cant connect to Mysql server ; Can't create/write the pid file, where the proposed solution is to assign the MySQL user to the /var/run/mysqld directory. However, if I try this I get "Operation not permitted":

kurt@kurt-ThinkPad:~$ chown mysql:mysql /var/run/mysqld
chown: changing ownership of '/var/run/mysqld': Operation not permitted

Any ideas on how to get MySQL to work?

like image 991
Kurt Peek Avatar asked Sep 20 '16 15:09

Kurt Peek


2 Answers

I've noticed that one solution is to run mysql with sudo:

kurt@kurt-ThinkPad:~$ sudo mysql
sudo: unable to resolve host kurt-ThinkPad
[sudo] password for kurt: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.15 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> 

(I got this suggestion from https://bbs.archlinux.org/viewtopic.php?id=154010).

like image 57
Kurt Peek Avatar answered Sep 27 '22 00:09

Kurt Peek


I got the same error code after the default installation. I'm running MariaDB 10.4.7 in Ubuntu 18.0.4. Here is how I bypassed it:

sudo mysql -u root
...
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost';
Query OK, 0 rows affected (0.005 sec)
MariaDB [(none)]> set password for 'root'@'localhost'=password('');
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> exit

After this, I can run the mysql command the same old way again:

$ mysql -u root
...
MariaDB [(none)]>

Note this will overwrite the security by default setting of MariaDB 10.4.7. In the test or development environment it's perfectly ok. In the production environment you might want to think twice. I would recommend create different user then set a password for it.

like image 23
sam Avatar answered Sep 24 '22 00:09

sam