Cannot login to MySQL database after fresh install with root ID and empty/no password like other older MySQL versions do
There's so many answers out there saying to reinstall mysql or use some combo of
mysqld_safe --skip-grant-tables
and / or
UPDATE mysql.user SET Password=PASSWORD('password')
and / or something else ...
... None of it was working for me
Here's what worked for me, on Ubuntu 18.04, from the top
With special credit to this answer for digging me out of the frustration on this ...
$ sudo apt install mysql-server
$ sudo cat /etc/mysql/debian.cnf
Note the lines which read:
user = debian-sys-maint
password = blahblahblah
Then:
$ mysql -u debian-sys-maint -p
Enter password: // type 'blahblahblah', ie. password from debian.cnf
mysql> USE mysql
mysql> SELECT User, Host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| User | Host | plugin |
+------------------+-----------+-----------------------+
| root | localhost | auth_socket |
| mysql.session | localhost | mysql_native_password |
| mysql.sys | localhost | mysql_native_password |
| debian-sys-maint | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
4 rows in set (0.00 sec)
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> COMMIT; // When you don't have auto-commit switched on
Either:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
Or:
// For MySQL 5.7+
UPDATE mysql.user SET authentication_string=PASSWORD('new_password') where user='root';
Then:
mysql> FLUSH PRIVILEGES;
mysql> COMMIT; // When you don't have auto-commit switched on
mysql> EXIT
$ sudo service mysql restart
$ mysql -u root -p
Enter password: // Yay! 'new_password' now works!
MySQL 5.7 changed the secure model: now MySQL root login requires a sudo
The simplest (and safest) solution will be create a new user and grant required privileges.
1. Connect to mysql
sudo mysql --user=root mysql
2. Create a user for phpMyAdmin
CREATE USER 'phpmyadmin'@'localhost' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'phpmyadmin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Reference - https://askubuntu.com/questions/763336/cannot-enter-phpmyadmin-as-root-mysql-5-7
In case you want to install mysql or percona unattended (like in my case ansible), you can use following script:
# first part opens mysql log
# second part greps lines with temporary password
# third part picks last line (most recent one)
# last part removes all the line except the password
# the result goes into password variable
password=$(cat /var/log/mysqld.log | grep "A temporary password is generated for" | tail -1 | sed -n 's/.*root@localhost: //p')
# setting new password, you can use $1 and run this script as a file and pass the argument through the script
newPassword="wh@teverYouLikE"
# resetting temporary password
mysql -uroot -p$password -Bse "ALTER USER 'root'@'localhost' IDENTIFIED BY '$newPassword';"
I to was experiencing the same problem and the only thing I was able to do to make it work was to go this route:
drop user admin@localhost;
flush privileges;
create user admin@localhost identified by 'admins_password'
This allowed me to recreate my username and enter a password for the user name.
After a lot of try, I could reset the default password with the following commands (Ubuntu and derivatives):
sudo -i
mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld
/etc/init.d/mysql stop
mysqld_safe --skip-grant-tables &
mysql -uroot
use mysql;
update user set authentication_string=password('YOURPASSWORD') where user='root';
update user set plugin="mysql_native_password" where User='root';
flush privileges;
quit;
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start
Sometimes, even after typed in the terminal
mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld
/etc/init.d/mysql stop
mysqld_safe --skip-grant-tables &
I got the error that the mysqld don't exists. So, quit, and type the same commands again.
And the final command
sudo /etc/init.d/mysql start
Sometimes doesn't work. Only after restart the computer.
As of Ubuntu 20.04 with MySql 8.0 : you can set the password that way:
login to mysql with sudo mysql -u root
change the password:
USE mysql; UPDATE user set authentication_string=NULL where User='root'; FLUSH privileges; ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'My-N7w_And.5ecure-P@s5w0rd'; FLUSH privileges; QUIT
now you should be able to login with mysql -u root -p
(or to phpMyAdmin with username root) and your chosen password.
P,S:
You can also login with user debian-sys-maint
, the password is written in the file /etc/mysql/debian.cnf