Is there a possibility to authenticate the root user by unix_socket (by root shell) or by password (when it is connected by localhost:3306)?
The purpose of auth_socket
in MySQL or unix_socket
in MariaDB is to disable password authentication in favor of Unix sockets, which only allow verified local users to connect to the server.
In other words, you can't have multiple authentication plugins for the same user/host. However like others have pointed out, a common workaround is creating additional users (with the same username) that are associated with different host
addresses (passwords can even be the same).
As @ColinM mentioned, localhost
usually defaults to using sockets, whereas 127.0.0.1
is inherently non-socket because it is an IP address, e.g. TCP protocol.
Here's an example of this for MySQL 8.0.x:
mysql -e "CREATE USER IF NOT EXISTS 'root'@'localhost' IDENTIFIED WITH auth_socket;"
mysql -e "CREATE USER 'root'@'127.0.0.1' IDENTIFIED WITH mysql_native_password BY '@DB_PASSWORD_ROOT';"
mysql -e "CREATE USER 'root'@'::1' IDENTIFIED WITH mysql_native_password BY '@DB_PASSWORD_ROOT';"
To ensure auth_socket
is persistent add lines to your my.cnf
:
plugin-load-add = auth_socket.so
auth_socket = FORCE_PLUS_PERMANENT
Keep in mind that it is now recommended to create a separate non-root user to be used with database management tools such as phpMyAdmin or Adminer, e.g. admin@127.0.0.1
and also realize that the new password hash plugin caching_sha2_password
is less compatible with many applications, esp. if they are not authenticating using either TLS/SSL, Unix sockets, or shared memory.