I used apt install mysql-server to install MySQL on Ubuntu 16.04 but during the installation, it did not ask for root password.

After installation I got ERROR 1045 when I tried to login as root and mysql_secure_installation threw the same error. I purged and autoremoved then reinstalled but it does not work.

How could i fix this? Can I set the root password if I didn't set it during installation?

This is my installation log:

The following additional packages will be installed:
  libaio1 mysql-client-5.7 mysql-client-core-5.7 mysql-server-5.7
Suggested packages:
  mailx tinyca
The following NEW packages will be installed:
  libaio1 mysql-client-5.7 mysql-client-core-5.7 mysql-server mysql-server-5.7
0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/17,9 MB of archives.
After this operation, 160 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Preconfiguring packages ...
Selecting previously unselected package libaio1:amd64.
(Reading database ... 227144 files and directories currently installed.)
Preparing to unpack .../libaio1_0.3.110-2_amd64.deb ...
Unpacking libaio1:amd64 (0.3.110-2) ...
Selecting previously unselected package mysql-client-core-5.7.
Preparing to unpack .../mysql-client-core-5.7_5.7.12-0ubuntu1_amd64.deb ...
Unpacking mysql-client-core-5.7 (5.7.12-0ubuntu1) ...
Selecting previously unselected package mysql-client-5.7.
Preparing to unpack .../mysql-client-5.7_5.7.12-0ubuntu1_amd64.deb ...
Unpacking mysql-client-5.7 (5.7.12-0ubuntu1) ...
Selecting previously unselected package mysql-server-core-5.7.
Preparing to unpack .../mysql-server-core-5.7_5.7.12-0ubuntu1_amd64.deb ...
Unpacking mysql-server-core-5.7 (5.7.12-0ubuntu1) ...
Selecting previously unselected package mysql-server-5.7.
Preparing to unpack .../mysql-server-5.7_5.7.12-0ubuntu1_amd64.deb ...
Unpacking mysql-server-5.7 (5.7.12-0ubuntu1) ...
Selecting previously unselected package mysql-server.
Preparing to unpack .../mysql-server_5.7.12-0ubuntu1_all.deb ...
Unpacking mysql-server (5.7.12-0ubuntu1) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for systemd (229-4ubuntu4) ...
Processing triggers for ureadahead (0.100.0-19) ...
Setting up libaio1:amd64 (0.3.110-2) ...
Setting up mysql-client-core-5.7 (5.7.12-0ubuntu1) ...
Setting up mysql-client-5.7 (5.7.12-0ubuntu1) ...
Setting up mysql-server-core-5.7 (5.7.12-0ubuntu1) ...
Setting up mysql-server-5.7 (5.7.12-0ubuntu1) ...
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Checking if update is needed.
This installation of MySQL is already upgraded to 5.7.12, use --force if you still need to run mysql_upgrade
Setting up mysql-server (5.7.12-0ubuntu1) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...

7 Answers 7


You can recover or set root password without knowing the current one by starting mysql without loading the grant-tables.

Please note the $ in the commands. This is specifying the terminal prompt you see when typing in the command. It's showing it's a line of text, but and actual typed terminal command. The "mysql>" prefix is also a prompt. That is the prompt you get when running mysql interactivately.

This is the cli (command line) to do this:
(Be sure to stop the current server before performing the steps. Only one server can run at a time.)

$ sudo mkdir /var/run/mysqld; sudo chown mysql /var/run/mysqld
$ sudo mysqld_safe --skip-grant-tables&

Now you can log in as root without a password and perform all commands, as in this case, set the root password as root.

$ sudo mysql --user=root mysql

This is the set root password that you will perform inside mysql if you have MySQL 5.6 or below:

mysql> update user set Password=PASSWORD('new-password') where user='root';
flush privileges;

In MySQL 5.7 or above

mysql> update user set authentication_string=PASSWORD('new-password') where user='root';
flush privileges;

From there, quit (kill the running msqld) mysql and start it as normal.

Notes on starting and stopping the mysql service:

Stop mysql:

$ sudo service mysql stop

Start mysql (normal):

$ sudo service mysql start

Kill the temporary mysql safe mode session:

$ sudo mysqladmin shutdown
  • 11
    in mysql 5.7.12 there is no column named password in user table. Hence following command worked for me. update user set authentication_string=PASSWORD('new-password') where user='root';
    – The Code
    Jun 20, 2016 at 19:05
  • 9
    I'm still getting ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
    – llamerr
    Jul 18, 2016 at 16:51
  • @ErelSegal-Halevi There are two things you should do. Run $ mysql_upgrade -u root -p. Also try the command above given by RP Singh . Aug 4, 2016 at 13:16
  • 5
    before executing FLUSH PRIVILEGES;, execute one more query: UPDATE user SET plugin='mysql_native_password'; so that the MySQL server uses its native password plugin to authenticate.
    – Aryo
    Mar 4, 2020 at 0:11
  • 1
    For Mysql 8 -> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
    – makkasi
    Mar 8, 2021 at 17:05

It will not ask for the password while installing mysql in Ubuntu 16.04 but you can set it after successful installation in following way:

After completion of mysql installation, run command:

sudo mysql_secure_installation

It will show:


In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here.

Enter current password for root (enter for none): (here press Enter)

OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation.

Set root password? [Y/n] y   (press 'y' to set new password)
New password: 
Re-enter new password:

Password updated successfully! Reloading privilege tables.. ... Success!

For Ubuntu 18.04 OR mysql-server version 5.7.22, THIS METHOD WILL NOT WORK

To set root password in Ubuntu 18.04, First follow the first three commands or first two steps of L.D. James's answer then run,

mysql> alter user 'root'@'localhost' identified by '<password>';

Password for root user is set!


Follow these steps to set root password in 18.04:

As there is no password set for root user, simply login with blank password

sudo mysql -u root -p
Enter password: (press enter as no password is set)

after then can easily run query

ALTER USER 'root'@'localhost' IDENTIFIED BY '<password>';
  • 4
    The problem in the case of this question is that the user can't get to the prompts you're mentioned because he doesn't know the current password for root. May 4, 2016 at 5:12
  • @L.D.James yes,you're right. I don't remember about ERROR : 1405 when i run mysql_secure_installation but sudo mysql_secure_installation worked for me. mysql installation doesn't ask for password. It happened with me the same. and here press enter means press enter without password.
    – d a i s y
    May 4, 2016 at 5:35
  • 2
    As concers ubuntu 18.04 or debian 9+ it won't connect with blank password and even after resetting pass for root, it still refuses to connect
    – chefarov
    Oct 11, 2018 at 18:04
  • @chefarov Facing the same issue, intensely frustrating Dec 6, 2018 at 1:52
  • Indeed, I still get the same error Jan 2, 2019 at 14:01

After the installation just:

  1. sudo mysql (yes, no user no pass)

  2. You are now in the mysql console. Execute:

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


That's it. You can now enter the console next time as usual:

mysql -u root -pmy_new_pass
  • 5
    I tried many other answers, but this one worked on Amazon EC2 Apr 21, 2019 at 7:39
  • 3
    This was the correct answer for WSL Ubuntu (Linux on Windows). May 14, 2019 at 20:20
  • 3
    This is the only answer which worked for me on ubuntu 18.04 new installation on google cloud.
    – Aakash
    Jun 17, 2019 at 6:19
  • 2
    Worked for me on Ubuntu 19.04. I did run mysql_secure_myinstallation, but it seemed that the root password was not set. Aug 20, 2019 at 11:24
  • 3
    I tried dozens of solutions. This was the easiest and the only one that worked.
    – mreff555
    Oct 10, 2019 at 1:25

Apparently the mysql-server installation on 16.04 (or any 5.7 installation?) allows root access not through password, but through the auth_socket plugin. Running sudo mysql -u root (n.b. w/o a password) will give you mysql console whereas running the command as non-root prompts you for a password.

It would seem that changing the password doesn't make much of a difference since the auth backend doesn't even check for a password. There is a very comprehensive article on how to change the plugin and switch to password authentication.

  • 4
    I was able to confirm with a fresh installation of apt install mysql-server without specifying a password on 18.04. The symptom and solution is exactly as described.
    – EricC
    Jun 18, 2018 at 2:45
  • so how the hell do i connect? Jan 2, 2019 at 14:02
  • @JamieHutber change the authentication plugin to use by executing: UPDATE user SET plugin='mysql_native_password'; just before the FLUSH PRIVILEGES; so that it uses its native password plugin to authenticate.
    – Aryo
    Mar 4, 2020 at 0:14
  • confirmed on Ubuntu 20.04.3 LTS
    – lineage
    Dec 29, 2021 at 11:05
  1. there is a default user account.

    $ vim /etc/mysql/debian.cnf 

    you will get this account from it like this:

    user     = debian-sys-maint
    password = nUZTARYslBsASzpw
  2. you login with this account

    $ mysql -u debian-sys-maint -p
  3. edit you root password

    mysql> show databases;
    mysql> use mysql;
    mysql> update user set authentication_string=PASSWORD("new_password") where user='root';
    mysql> update user set plugin="mysql_native_password";
    mysql> flush privileges;
    mysql> quit;
  4. restart mysql

    $ /etc/init.d/mysql restart;

then you can mysql -u root -p with you previous setting.

  • 1
    I tried every other solution, and this is the only one that worked for me. (Linux Mint 19.2). Oct 26, 2019 at 10:45

Adding to the main answer, If you are stuck at

"mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql"

1 thing to remember that you have to use another terminal window to enter the following command.

$ sudo mysql --user=root mysql

If you try to do it in the same window, it will shut down the mysqld safemode process.


Here's how to setup mysql-server with a terminal password prompt in Ubuntu 18.10.

  1. Download The MySQL APT Repository via https://dev.mysql.com/downloads/repo/apt/
  2. cd into your directory where you downloaded and run dpkg -i mysql-apt-config_0.8.13-1_all.deb (check your version number)
  3. From the prompt menu select the last option "ok" and then "ok" again.

  4. Update and install mysql-server and mysql-client:

sudo apt update
sudo apt install mysql-server mysql-client
  1. Type in your root password in the promt.
  2. Select "Use Legacy Authentication Method" if your running frameworks/applications (such as laravel) which do not support the new "Strong Password Encryption" yet, otherwise if your framework/application supports "Strong Password Encryption" then select it.
  3. Login as root and check if it works.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .