how to login mysql shell when mysql have no password

I got this error:

root@sys3026:/home/sys3026# mysql --user=root --password    
Enter password:     
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Any one please help me. Thanks in advance..


Solution 1:

When a MySQL user is configured to use auth_socket (instead of mysql_native_password), as it is by default in Ubuntu 18.04, you can log-in as root (for example) in the following way:

sudo mysql -u'root'

or just:

sudo mysql

References:

  • Digital Ocean: How To Install MySQL on Ubuntu 18.04 - read Step 3

Solution 2:

If your database has no password, just leave out the --password parameter.

$ mysql --user=root

Reference:

  • https://stackoverflow.com/questions/3843973/mysql-is-prompting-for-password-even-though-my-password-is-empty

Solution 3:

If you have forgotten your password or you can not login you can always run mysql in a "safe_mode". Which allows you to access it without any password restriction - to change the root password or adjust something else if something went wrong.

systemctl stop mysql
sudo mysqld_safe --skip-grant-tables &

Now you can access the mysql server without a password.

mysql -uroot

Add a new password to the root user in the mysql shell

use mysql;
update user set password=PASSWORD("newpassword") where User='root';
flush privileges;

Now restart it in normal mode again and it will work with the new password.

systemctl stop mysql
systemctl start mysql