Skip to content

Instantly share code, notes, and snippets.

@scodx
Last active September 2, 2019 17:15
Show Gist options
  • Save scodx/68116f13ee4798112a5b87e47cefd2c3 to your computer and use it in GitHub Desktop.
Save scodx/68116f13ee4798112a5b87e47cefd2c3 to your computer and use it in GitHub Desktop.
MySQL root password not working

MySQL or Mariadb Root password not working

After installing mysql/mariadb the root password is not working, even after executing the secure_installation script. I followed these steps on a Ubuntu server 18.08 and seems to work there.

sudo su
/etc/init.d/mysql stop
mysqld_safe --skip-grant-tables &
mysql -uroot

This will completely stop mysql, bypass user authentication (no password needed) and connect to mysql with user "root".

Now, in mysql console, go using mysql administrative db:

use mysql;
update user set password=PASSWORD("mynewpassword") where User='root';
update user set plugin="mysql_native_password";
quit;

Depending on the mysql installation the passwordcolumn might not be available, you can change the password with:

use mysql;
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
update user set plugin="mysql_native_password";
quit;

This one will overwrite authentication method, remove the unix_socket request (and everything else), restoring a normal and working password method

Stop and start everything related to mysql:

/etc/init.d/mysql stop
kill -9 $(pgrep mysql)
/etc/init.d/mysql start
exit;

You should be able to login normally by:

mysql -u root -p

EOF

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment