88

Cannot login to MySQL database after fresh install with root ID and empty/no password like other older MySQL versions do

Ryan
  • 3,085
  • 5
  • 27
  • 31
  • 6
    root no longer has one because it does not use one. By default the auth method is auth socket. use sudo to access with root access, change the auth method to password and set a password if you need a root lmysql oogin without root system access. – G.Martin Nov 13 '16 at 03:50
  • 3
    @G.Martin would you mind expanding this comment into an answer with more detailed steps? I think this is the solution I want to use but don't know how to change the auth method. (`sudo mysql -u root` does work for me - I want to change it so I can just do `mysql -u root` with no password) – Max Williams Dec 07 '16 at 13:08
  • Sorry You probably already figured this out but I found out my post only applies to debian distros. If its still an issue (doubt it) I can provide more detail. – G.Martin Jul 09 '17 at 21:37

14 Answers14

175

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!
Stewart
  • 17,616
  • 8
  • 52
  • 80
  • 5
    I've followed these steps and it doesn't work for me. Everything looks good, but as soon as I restart MySQL, the plugin value gets reset to `auth_socket`. Any idea what's missing? – Matt Raible May 30 '18 at 16:25
  • @MattRaible I don't know to be honest. Perhaps check the answer I credited / linked above? – Stewart Jun 01 '18 at 04:37
  • 4
    Didn't work for me initially. Then I tried issuing a COMMIT before EXIT and it worked! – LeandroG Jul 10 '18 at 16:43
  • @LeandroG Good to know. The above method relies on auto-commit being configured by default. – Stewart Jul 10 '18 at 16:44
  • 3
    Worked for me but instead of using: mysql -u debian-sys-maint -p , I had to use: sudo mysql -u root . When I used the first one the changes were not being saved even with COMMIT – Peter Jul 27 '18 at 07:59
  • 2
    Need to do a `commit;` before `FLUSH PRIVILEGES;`. – Eric Aug 17 '18 at 08:33
  • 1
    After adding a `COMMIT;` after `FLUSH PRIVILEGES;`, this did the trick. Thanks! – Jeff Moorhead Sep 13 '18 at 00:52
  • Some have suggested adding `COMMIT` *before* `FLUSH PRIVILEGES`; some have suggested adding it *after*. I had `auto-commit` for my CLI, so I'm not sure where it should go, or if it matters. I've added it *after* in an edit. – Stewart Sep 13 '18 at 06:03
  • 3
    Tested this. Mysql **5.7+** which is installed by default with **18.04 LTS** changes password with `UPDATE mysql.user SET authentication_string=PASSWORD('new_password') where user='root';` so, you need to run that instead of `ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';` – message Oct 02 '18 at 14:49
  • @message The code as posted (except for the comments) is **exactly** the code as used in real life. Please leave it alone! – Stewart Oct 02 '18 at 17:07
  • 1
    @Stewart you can just reject my edit changes without that comment. Your solution didn't work for me completely. – message Oct 03 '18 at 15:09
  • 1
    @message, your workaround worked like a charm for me! Thanks – Raghu Oct 11 '18 at 09:19
  • 1
    Thank you very much, after about an hour of trying this worked for me! – handkock Nov 02 '18 at 10:41
  • Not work for me, magic thing happens when I try `ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your new password';` from https://www.percona.com/blog/2016/03/16/change-user-password-in-mysql-5-7-with-plugin-auth_socket/ – Bing Zhao Jan 28 '19 at 09:58
  • 1
    I can confirm that on **5.7** this command `ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';` doesn't work. You can see from the output as it says that 0 rows affected. The one mentioned for **5.7+** does work as long as you don't forget to `Commit;` and `FLUSH PRIVILEGES` – Rui Taborda Oct 22 '19 at 14:40
  • 1
    MAN THANK YOU. I have been scratching my head to figure out whats wrong, following so many other steps, but this one is perfect – Hassan Jalil Mar 18 '20 at 18:01
  • Everything was fine until I restarted and tried to login in the last step. I had to use the command like: sudo mysql -u root -p Without sudo it was not working. – 3AK Nov 23 '21 at 14:55
  • 1
    I cant believe connecting to a brand new mysql installation is so hard and not documented at all – user1532587 Jan 01 '23 at 18:57
121

After you installed MySQL-community-server 5.7 from fresh on linux, you will need to find the temporary password from /var/log/mysqld.log to login as root.

  1. grep 'temporary password' /var/log/mysqld.log
  2. Run mysql_secure_installation to change new password

ref: http://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html

Ryan
  • 3,085
  • 5
  • 27
  • 31
  • 8
    Thank you! Kind of crazy to find this information here instead of the MySQL 5.7 doc from Oracle. – maddin2code Feb 09 '16 at 18:12
  • 8
    This saves my life... been searching for solution everywhere and this solves all my problem. Even the document from mysql website didn't mention about this. This is crazy man. Thanks! – nodeffect Mar 11 '16 at 06:33
  • 1
    Where would you look on a Windows machine? – Howard Dec 11 '16 at 19:04
  • 1
    @rotaercz try following this guide? http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html – Ryan Dec 17 '16 at 08:11
  • 22
    /var/log/mysqld.log does not exist for me (mariadb on mint) – donquixote Mar 16 '17 at 18:06
  • thank you ! solved my problem here!! never knew about this "temporary password"!! – costamatrix Jul 05 '17 at 20:15
  • unbelievable I know I shouldn't add this kind of "me too!" comments but holy crap the official documentation would not even come up first for this information – Sebas Aug 27 '17 at 10:41
  • It's not there. – Panu Haaramo Oct 05 '17 at 20:54
  • In MySQL 5.6, you may find the password [in a hidden file in root's home directory](https://stackoverflow.com/a/47169395/5419599). – Wildcard Nov 08 '17 at 00:01
  • 10
    For those who can't find the `/var/log/mysqld.log`, just run `sudo mysql_secure_installation`. Remmember to run that command with `sudo` or you will get the "Access denied" error. – wpclevel May 02 '18 at 09:00
  • 2
    `sudo mysql_secure_installation` will do ABSOLUTELY NOTHING if the MySQL root account makes use of the "auth_socket" plugin. I elaborate in [my answer below](https://stackoverflow.com/questions/33991228/what-is-the-default-root-pasword-for-mysql-5-7/57948037#57948037) – Julian - BrainAnnex.org Sep 15 '19 at 20:41
35

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

Sachin Vairagi
  • 4,894
  • 4
  • 35
  • 61
  • 8
    To be honest I discarded this answer as pretty stupid at first and tried tons of other stuff including searching for random generated passwords in files which did not exist on my local Ubuntu Machine. However a simple SUDO and a blank password just did it. Have an upvote! – MrTony Apr 04 '19 at 09:58
15

MySQL server 5.7 was already installed by default on my new Linux Mint 19.

But, what's the MySQL root password? It turns out that:

The default installation uses auth_socket for authentication, in lieu of passwords!

It allows a password-free login, provided that one is logged into the Linux system with the same user name. To login as the MySQL root user, one can use sudo:

sudo mysql --user=root

But how to then change the root password? To illustrate what's going on, I created a new user "me", with full privileges, with:

mysql> CREATE USER 'me'@'localhost' IDENTIFIED BY 'my_new_password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'me'@'localhost' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;

Comparing "me" with "root":

mysql> SELECT user, plugin, HEX(authentication_string)  FROM mysql.user WHERE user = 'me' or user = 'root';
+------+-----------------------+----------------------------------------------------------------------------+
| user | plugin                | HEX(authentication_string)                                                 |
+------+-----------------------+----------------------------------------------------------------------------+
| root | auth_socket           |                                                                            |
| me   | mysql_native_password | 2A393846353030304545453239394634323734333139354241344642413245373537313... |
+------+-----------------------+----------------------------------------------------------------------------+

Because it's using auth_socket, the root password cannot be changed: the SET PASSWORD command fails, and mysql_secure_installation desn't attain anything...

==> To zap this alternate authentication mode and return the MySQL root user to using passwords:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'SOME_NEW_ROOT_PASSWORD';

A good explanation.

More details from the MySQL manual.

8

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';"
hpaknia
  • 2,769
  • 4
  • 34
  • 63
6

It seems things were designed to avoid developers to set the root user, a better solution would be:

sudo mysql -u root

Then create a normal user, set a password, then use that user to work.

create user 'user'@'localhost' identified by 'user1234';
grant all on your_database.* to 'user'@'localhost';
select host, user from mysql.user;

Then try to access:

mysql -u user -p

Boom!

Pablo Pazos
  • 3,080
  • 29
  • 42
  • Nope. It was designed to set a password for root instead of empty password. – Ryan Oct 07 '18 at 03:12
  • 1
    By default it is designed to connect only via local socket with root. Either way is a bad practice to use root to connect to MySQL even for dev, and my answer describes how to use a good practice of creating a non root user that actually works. – Pablo Pazos Oct 07 '18 at 23:46
6

MySQL 5.7 or newer generates a default temporary password after fresh install.

To use MySQL first you would be required to get that password from the log file which is present at the /var/log/mysqld.log. So follow the following process:

  1. grep 'temporary password' /var/log/mysqld.log

  2. mysql_secure_installation

The second command is required to change the password for MySQL and also to make certain other changes like removing temporary databases, allow or disallow remote access to root user, delete anonymous users etc…

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Vipul Sharma
  • 162
  • 3
  • 8
4

None of these answers worked for me on Ubuntu Server 18.04.1 and MySQL 5.7.23. I spent a bunch of time trying and failing at setting the password and auth plugin manually, finding the password in logs (it's not there), etc.

The solution is actually super easy:

sudo mysql_secure_installation

It's really important to do this with sudo. If you try without elevation, you'll be asked for the root password, which you obviously don't have.

bendodge
  • 470
  • 5
  • 12
4

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.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Leonardo
  • 791
  • 1
  • 5
  • 21
1

I just installed Linux Mint 19 (based on Ubuntu 18.04) on my machine. I installed MySQL 5.7 from the repo (sudo apt install mysql-server) and surprisingly during installation, the setup didn't prompt to enter root password. As a result I wasn't able to login into MySQL. I googled here and there and tried various answers I found on the net, including the accepted answer above. I uninstalled (purging all dpkgs with mysql in its name) and reinstalled again from the default Linux Mint repositories. NONE works.

After hours of unproductive works, I decided to reinstall MySQL from the official page. I opened MySQL download page (https://dev.mysql.com/downloads/repo/apt) for apt repo and clicked Download button at the bottom right.

Next, run it with dpkg:

sudo dpkg -i mysql-apt-config_0.8.10-1_all.deb

At the installation setup, choose the MySQL version that you'd like to install. The default option is 8.0 but I changed it to 5.7. Click OK to quit. After this, you have a new MySQL repo in your Software Sources.

Update your repo:

sudo apt update

Finally, install MySQL:

sudo apt install mysql-server

And now I was prompted to provide root password! Hope it helps for others with this same experience.

  • 1
    I spent days solving it while installing `mysql-server` from ubuntu repo but at the end official, the one MySQL provides, works. – Inam Ul Huq Jul 29 '19 at 12:16
1

As of Ubuntu 20.04 with MySql 8.0 : you can set the password that way:

  1. login to mysql with sudo mysql -u root

  2. 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

Ohad Cohen
  • 5,756
  • 3
  • 39
  • 36
1

To do it in non interactive mode (from a script):

systemctl start mysqld
MYSQL_ROOT_TMP_PSW=$(grep 'temporary password' $logpath/mysqld.log |sed "s|.*: ||")

## POPULATE SCHEMAS WITH ROOT USER
/usr/bin/mysql --connect-expired-password -u root -p${MYSQL_ROOT_TMP_PSW} < "$mysql_init_script"

Here's the head of the init script

SET GLOBAL validate_password_policy=LOW;
FLUSH privileges;

SET PASSWORD = PASSWORD('MYSQL_ROOT_PSW');
FLUSH privileges;

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
FLUSH privileges;

...

Then restart the service systemctl restart mysqld

tuxErrante
  • 1,274
  • 12
  • 19
0

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.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Donald L Wilson
  • 1,291
  • 1
  • 8
  • 10
0

In my case the data directory was automatically initialized with the --initialize-insecure option. So /var/log/mysql/error.log does not contain a temporary password but:

[Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

What worked was:

shell> mysql -u root --skip-password
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

Details: MySQL 5.7 Reference Manual > 2.10.4 Securing the Initial MySQL Account

Ra.
  • 2,499
  • 3
  • 28
  • 41