mysql says you can't login as root but you have definitely got the right password

If mysql says you can't login as root but you have definitely got the right password and you have just installed it for the first time, it can be a nuisance. Do the below as user root.


My suggestion is:


1. If you have existing databases, and want to keep them, back them up with mysqldump.

    

mysqldump -u user_with_access -pPassword database_to_rescue > database_to_rescue.sql


Assuming that you have at least one user account, e.g. "user_with_access", who can read the database called “database_to_rescue".


2. Erase the mysql directory and start over:

    

service mysql stop
rm -rf /var/lib/mysql/*


3. Re-run mysqld initialisation to set the password to blank:


mkdir -p /var/run/mysqld

chown -R /var/run/mysqld
chmod 777 /var/run/mysqld # you can change this back to 755 later

mysqld --initialize-insecure --user=mysql


If it says it can't find the socket in /var/run/mysqld, repeat the commands mkdir-chown above.


4. It will then set up mysql from scratch with a blank root password. The item "initialise-insecure" above erases everything and starts over. 


5. Run


service mysql restart


If it says it can't find the socket in /var/run/mysqld, repeat the commands mkdir-chown above.


6. Login


mysql -u root -p


it will ask for the password, just press enter as it should be blank


7. Run this sql:


ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'abcd1234*';

FLUSH PRIVILEGES;

EXIT;


8. Now permanently set the password for mysql for yourself so you can just run mysql without logging in:


login as yourself (not root)


user=`whoami` 

sudo su -c "mysql_config_editor set --host=localhost --user=root --password" -s /bin/bash "$user" 


It will first ask for your login password to turn you temporarily into root

When it asks for a second password, give your mysql password, e.g. abcd1234*

It will then ask if you must save and overwrite what is there, say yes.

Comments

Popular posts from this blog

what to do if you crashed your database OR it says there are no tables (but there are!)

completely erase and reinstall mysql script.

Backup a database