can not login to mysql Ubuntu 18.04

Viewed 2905

when I installed mysql server into ubuntu machine using this command

sudo apt-get install mysql-client mysql-server 

the command is executed successfully but during the installation mysql not asked me about password , so I can not login to mysql except as root user , in other words if I writing into command line this command

sudo mysql -uroot -p

I can login to mysql even if I changed password every time , server does not matter about password if I used sudo . so how can I solve this problem (set new password) when I logged to mysql I tried to change password from mysql.user table using this query

mysql> UPDATE mysql.user SET Password= PASSWORD('rootroot') WHERE User = 'root';

but there is no Password column into mysql.user table

3 Answers

The reason is that recent Ubuntu installation (maybe others also), mysql is using by default the UNIX auth_socket plugin.

Basically means that: db_users using it, will be "auth" by the system user credentias. You can see if your root user is set up like this by doing the following:

$ sudo mysql -u root # I had to use "sudo" since is new installation

  mysql> USE mysql;
  mysql> SELECT User, Host, plugin FROM mysql.user;

   +------------------+-----------------------+
   | User             | plugin                |
   +------------------+-----------------------+
   | root             | auth_socket           |
   | mysql.sys        | mysql_native_password |
   | debian-sys-maint | mysql_native_password |
   +------------------+-----------------------+

As you can see in the query, the root user is using the auth_socket plugin

There are 2 ways to solve this:

You can set the root user to use the mysql_native_password plugin You can create a new db_user with you system_user (recommended) Option 1:

$ sudo mysql -u root # I had to use "sudo" since is new installation

mysql> USE mysql;
 mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;

 $ service mysql restart

MySQL by default create a password for root@hostname

I tried finding the password from directories of MySQL, but was not successful.

So, I tried resetting password. I followed the following steps.

# Stop MySQL
sudo service mysql stop

# Make MySQL service directory.
sudo mkdir /var/run/mysqld

# Give MySQL user permission to write to the service directory.
sudo chown mysql: /var/run/mysqld

# Start MySQL manually, without permission checks or networking.
sudo mysqld_safe --skip-grant-tables --skip-networking &

# Log in without a password.
mysql -uroot mysql

**Update password for root**

UPDATE mysql.user SET authentication_string=PASSWORD('YOURNEWPASSWORD'), plugin='mysql_native_password' WHERE User='root' AND Host='%';

EXIT;

# Turn off MySQL.
sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown

# Start the MySQL service normally.
sudo service mysql start

Otherwise, please refer this link

I think you should use authentication_string instead of Password.

Related