简体   繁体   中英

Linux MySQL - Can't login to root

So I wrote this installer script for all my apps/settings on Ubuntu, which I used recently after upgrading to 16.04. One of the things it does is install mysql. I did it in a special way to kill those user prompts, because I don't want my script to stop for user input. Here is the code below:

sudo DEBIAN_FRONTEND=noninteractive apt-get install -qq -y mysql-server

Now, I try to sign into mysql from the terminal, but I can't. It's installed and running, but I keep getting access denied for the root user.

mysqladmin -u root -p password
Enter password: 
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost''

I'm not entering any password, because I have not at any point in time setup a password.

Anyone have any ideas how to fix this? Thanks.

If you want to set a password for root:

For newer versions of MySQL (for MariaDB < 10.2 use the query at the bottom as it doesn't support alter user but still uses auth_socket)

If you install 5.7 and don't provide a password to the root user, it will use the auth_socket plugin. That plugin doesn't care and doesn't need a password. It just checks if the user is connecting using a UNIX socket and then compares the username.

Taken from Change User Password in MySQL 5.7 With "plugin: auth_socket" and https://askubuntu.com/a/801950/395418

So in order to to change the plugin back to mysql_native_password :

  1. Login with sudo:

     sudo mysql -u root 
  2. Change the plugin and set a password with a single command:

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

Of course you can also use the command above to set an empty password.

Just for the record, there is also another way to only change the plugin without providing a password (leaving it empty):

    update mysql.user set plugin = 'mysql_native_password' where User='root';
    FLUSH PRIVILEGES;

Yep I feel silly now. The issue was actually Linux permissions, not mysql. I installed it using sudo, I needed to login using sudo. No password needed, just sudo mysql -u root -p -h local host. Just press enter at the password prompt and viola, I'm in.

Thanks for the help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM