0

I am using "mysql -u root -p" command to start mysql but I am getting error as:

Access denied for user 'root'@localhost''

I always have to use sudo to to launch it. Other applications start normally. How do I get around it? I am doing jdbc connection (java). Mysql doesn't give access to database in java. I think requiring sudo command is the problem.

System: Ubuntu 18.04 LTS dual booted with Windows 10.

Tejas Joshi
  • 90
  • 1
  • 9

1 Answers1

0

I always have to use sudo to to launch it

No. You need to use sudo to get the client to authenticate against the server.

The reason for this is that recent versions of MySQL (and MariaDB, PerconaDB) use SO_PEERCRED to very that the username asserted in the connection string (root) is the same user as started the client (this makes use of a password somewhat redundant).

Since SO_PERRCRED only works on filesystem sockets (AF_UNIX) you may be able to bypass the constraint by connecting via a network socket, e.g.

 mysql -h 127.0.0.1 -u root -p

But do be aware that MySQL typically has separate user records for connections via network (host != 'localhost') and filesystem (host='localhost') sockets.

But as per the question @Ciarambola flagged, 'root' is a special case and should not be used for routine access - you should create a new user.

Mysql doesn't give access to database in java

You should never use an admin account as the embedded credentials in an application. If you make that account with the same name as your user you won't need to use sudo when you connect to 'localhost'.

symcbean
  • 47,736
  • 6
  • 59
  • 94