Documentation Home
MySQL 5.7 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 35.6Mb
PDF (A4) - 35.6Mb
PDF (RPM) - 34.7Mb
EPUB - 8.7Mb
HTML Download (TGZ) - 8.5Mb
HTML Download (Zip) - 8.5Mb
HTML Download (RPM) - 7.3Mb
Eclipse Doc Plugin (TGZ) - 9.3Mb
Eclipse Doc Plugin (Zip) - 11.5Mb
Man Pages (TGZ) - 203.5Kb
Man Pages (Zip) - 310.2Kb
Info (Gzip) - 3.3Mb
Info (Zip) - 3.3Mb
Excerpts from this Manual

B.5.3.2 How to Reset the Root Password

If you have never assigned a root password for MySQL, the server does not require a password at all for connecting as root. However, this is insecure. For instructions on assigning a password, see Section 2.10.4, “Securing the Initial MySQL Accounts”.

If you know the root password and want to change it, see Section 14.7.1.1, “ALTER USER Syntax”, and Section 14.7.1.7, “SET PASSWORD Syntax”.

If you assigned a root password previously but have forgotten it, you can assign a new password. The following sections provide instructions for Windows and Unix and Unix-like systems, as well as generic instructions that apply to any system.

B.5.3.2.1 Resetting the Root Password: Windows Systems

On Windows, use the following procedure to reset the password for the MySQL 'root'@'localhost' account. To change the password for a root account with a different host name part, modify the instructions to use that host name.

  1. Log on to your system as Administrator.

  2. Stop the MySQL server if it is running. For a server that is running as a Windows service, go to the Services manager: From the Start menu, select Control Panel, then Administrative Tools, then Services. Find the MySQL service in the list and stop it.

    If your server is not running as a service, you may need to use the Task Manager to force it to stop.

  3. Create a text file containing the password-assignment statement on a single line. Replace the password with the password that you want to use.

    MySQL 5.7.6 and later:

    ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
    

    MySQL 5.7.5 and earlier:

    SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
    
  4. Save the file. This example assumes that you name the file C:\mysql-init.txt.

  5. Open a console window to get to the command prompt: From the Start menu, select Run, then enter cmd as the command to be run.

  6. Start the MySQL server with the special --init-file option (notice that the backslash in the option value is doubled):

    C:\> cd "C:\Program Files\MySQL\MySQL Server 5.7\bin"
    C:\> mysqld --init-file=C:\\mysql-init.txt
    

    If you installed MySQL to a different location, adjust the cd command accordingly.

    The server executes the contents of the file named by the --init-file option at startup, changing the 'root'@'localhost' account password.

    To have server output to appear in the console window rather than in a log file, add the --console option to the mysqld command.

    If you installed MySQL using the MySQL Installation Wizard, you may need to specify a --defaults-file option. For example:

    C:\> mysqld
             --defaults-file="C:\\ProgramData\\MySQL\\MySQL Server 5.7\\my.ini"
             --init-file=C:\\mysql-init.txt
    

    The appropriate --defaults-file setting can be found using the Services Manager: From the Start menu, select Control Panel, then Administrative Tools, then Services. Find the MySQL service in the list, right-click it, and choose the Properties option. The Path to executable field contains the --defaults-file setting.

  7. After the server has started successfully, delete C:\mysql-init.txt.

You should now be able to connect to the MySQL server as root using the new password. Stop the MySQL server and restart it normally. If you run the server as a service, start it from the Windows Services window. If you start the server manually, use whatever command you normally use.

If the ALTER USER statement fails to reset the password, try repeating the procedure using the following statements to modify the user table directly:

UPDATE mysql.user
    SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'
    WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;
B.5.3.2.2 Resetting the Root Password: Unix and Unix-Like Systems

On Unix, use the following procedure to reset the password for the MySQL 'root'@'localhost' account. To change the password for a root account with a different host name part, modify the instructions to use that host name.

The instructions assume that you will start the MySQL server from the Unix login account that you normally use for running it. For example, if you run the server using the mysql login account, you should log in as mysql before using the instructions. Alternatively, you can log in as root, but in this case you must start mysqld with the --user=mysql option. If you start the server as root without using --user=mysql, the server may create root-owned files in the data directory, such as log files, and these may cause permission-related problems for future server startups. If that happens, you will need to either change the ownership of the files to mysql or remove them.

  1. Log on to your system as the Unix user that the MySQL server runs as (for example, mysql).

  2. Stop the MySQL server if it is running. Locate the .pid file that contains the server's process ID. The exact location and name of this file depend on your distribution, host name, and configuration. Common locations are /var/lib/mysql/, /var/run/mysqld/, and /usr/local/mysql/data/. Generally, the file name has an extension of .pid and begins with either mysqld or your system's host name.

    Stop the MySQL server by sending a normal kill (not kill -9) to the mysqld process. Use the actual path name of the .pid file in the following command:

    shell> kill `cat /mysql-data-directory/host_name.pid`
    

    Use backticks (not forward quotation marks) with the cat command. These cause the output of cat to be substituted into the kill command.

  3. Create a text file containing the password-assignment statement on a single line. Replace the password with the password that you want to use.

    MySQL 5.7.6 and later:

    ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
    

    MySQL 5.7.5 and earlier:

    SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
    
  4. Save the file. This example assumes that you name the file /home/me/mysql-init. The file contains the password, so do not save it where it can be read by other users. If you are not logged in as mysql (the user the server runs as), make sure that the file has permissions that permit mysql to read it.

  5. Start the MySQL server with the special --init-file option:

    shell> mysqld_safe --init-file=/home/me/mysql-init &
    

    The server executes the contents of the file named by the --init-file option at startup, changing the 'root'@'localhost' account password.

  6. After the server has started successfully, delete /home/me/mysql-init.

You should now be able to connect to the MySQL server as root using the new password. Stop the server and restart it normally.

If the ALTER USER statement fails to reset the password, try repeating the procedure using the following statements to modify the user table directly:

UPDATE mysql.user
    SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'
    WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;
B.5.3.2.3 Resetting the Root Password: Generic Instructions

The preceding sections provide password-resetting instructions specifically for Windows and Unix and Unix-like systems. Alternatively, on any platform, you can reset the password using the mysql client (but this approach is less secure):

  1. Stop the MySQL server if necessary, then restart it with the --skip-grant-tables option. This enables anyone to connect without a password and with all privileges, and disables account-management statements such as ALTER USER and SET PASSWORD. Because this is insecure, you might want to use --skip-grant-tables in conjunction with --skip-networking to prevent remote clients from connecting.

  2. Connect to the MySQL server using the mysql client; no password is necessary because the server was started with --skip-grant-tables:

    shell> mysql
    
  3. In the mysql client, tell the server to reload the grant tables so that account-management statements work:

    mysql> FLUSH PRIVILEGES;
    

    Then change the 'root'@'localhost' account password. Replace the password with the password that you want to use. To change the password for a root account with a different host name part, modify the instructions to use that host name.

    MySQL 5.7.6 and later:

    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
    

    MySQL 5.7.5 and earlier:

    mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
    

You should now be able to connect to the MySQL server as root using the new password. Stop the server and restart it normally (without the --skip-grant-tables and --skip-networking options).

If the ALTER USER statement fails to reset the password, try repeating the procedure using the following statements to modify the user table directly:

UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPass')
WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;

User Comments
  Posted by Schneelocke on February 16, 2005
On Windows installations, it is also possible to disable grant table loading and restart the server using the MySQL System Tray Monitor (the relevant option is under Configure Instance->Startup Variables->Security).

I got all sorts of unhelpful error messages when trying to reset the root password on the console as described, but using the System Tray monitor and the graphical MySQL Administrator worked flawlessly.

  Posted by Chetan Graham on May 12, 2008
Make sure you have your InnoDB Base Directory and Data Directory path's correct or you will continue to get this error. "Error 1045 Access denied for 'root'@'localhost'[using password: Yes] "

If you have InnoDB set up you must have the correct path to these two directories in MySQL.

I've learned the hard way for the last several years on similar errors.

Oh, Also please understand, that [using password: Yes] does not necessarily mean you are using the wrong password.
You might need to allow mysqld-nt.exe from your FireWall or your Virus Software has an inbound FireWall setup or you do not have Administrative rights to do this installation (if you are installing for first time.)

Hope this helps someone.
Blessings,
Chetanji
  Posted by cnemo why on September 23, 2008
I also found a good example on how to reset is here: http://blog.dotkam.com/2007/04/10/mysql-reset-lost-root-password/
  Posted by Cameron James on January 13, 2010
If you're looking to fix root's permissions, not just reset the password, insert the following code on a new line at the start of the mysql-init file.
It will effectively reset root with the permissions to do whatever root pleases.

grant all privileges on *.* to root@localhost;
  Posted by Mofassir Haque on January 15, 2010
You can delete root password by re-installing MySqL but mysql folder under program files and under my documents must be deleted first.
  Posted by jacques montes on February 26, 2010
When resetting the root password, I had to remove the previously directory "c:\Documents and Settings\Administrator\Application Data\MySQL" to be able to reset the password. And in my case the directory was hidden ...
  Posted by Brenda Wallace on October 27, 2010
on debian and ubuntu you can reset the password by doing:

sudo dpkg-reconfigure mysql-server-5.1

  Posted by Sam Sherlock on November 9, 2010
Windows 7 when reseting root password using this method I had to run cmd as administrator before the advice above would take effect.

(and yes I did indeed install mysql and forget what pass I gave root; almost immediately - do-do-donut)
  Posted by Klaus Slott on December 3, 2010
If using the example above on a unix system and MySQL is running on non-root account: Remember to ensure that file permissions for the mysql-init file allows mysqld to read the file.
  Posted by Barry Galbraith on February 4, 2011
When resetting root password in Windows, if you start the server with
>mysqld --defaults-file="c:\mysql\my.ini" --init-file=c:\\init.txt
the command will start the server, but not return to a prompt. It will appear to lock up.

Better to use

>start mysqld --defaults-file="c:\mysql\my.ini" --init-file=c:\\init.txt

It opens a new window, starts mysqld, the new window will close, and return to a prompt.
  Posted by ramasamy g on November 26, 2011
If you have trouble stopping mysqld proccess

In debian/ubuntu

try /etc/init.d/mysql stop
  Posted by Apple Grew on January 17, 2012
If you are using CentOS 6 (maybe others too). To reset the root password use the following steps:-

1) sudo service mysqld stop
2) sudo service mysqld startsos
3) mysql -u root
4) Now you will be at mysql prompt. Here type:-
4.1) UPDATE mysql.user SET Password=PASSWORD('NewPassHere') WHERE User='root';
4.2) FLUSH PRIVILEGES;
4.3) quit;
5) sudo service mysqld restart
  Posted by Oliver Candelario on April 23, 2012
  Posted by Brian Lindval on April 25, 2012
An important note for those using the initial recommended steps with MySQL on Windows.
Make sure to run the command prompt as administrator even if you are logged in as administrator. The initial steps failed until elevating the rights for the command prompt for me.
  Posted by Florian Rustedt on July 24, 2012
Very important:
On actual Ubuntu (LTS12) and perhaps also on other systems, the init-file must have the same owner, as the mysqld!

So working way will be p.e.:
mysqld --user=mysql --init-file=/tmp/reset-mysql-pw.sql

Where /tmp/reset-mysql-pw.sql has the owner "mysql", too
  Posted by David Beecher on November 16, 2013
One thing to note--

If you do the password reset and you still can't get in due to the following error:

[root@dbdev log]# mysql -uroot -p
Enter password:
ERROR 2049 (HY000): Connection using old (pre-4.1.1) authentication protocol refused (client option 'secure_auth' enabled)

Go into the /etc/my.cnf file and make sure that:

old_password=0

THEN reset the password and try again.

Good luck,
David

  Posted by Lance Kuttner on December 2, 2013
  Posted by su ema on November 19, 2015
first i use CentOS7, and install mysql5.7.9 with yum
first you have mysql user
#groupadd mysql
#useradd -r -g mysql -s /bin/false mysql
if you have the mysql new installed, then you can delete /var/lib/mysql and /var/lib/mysql-files
next you initialize your mysql database
#mysqld --defaults-file=/etc/my.cnf --initialize-insecure
--initialize-insecure create root user without password
#tailf /var/log/mysql.log
you can see this sentence:
root@localhost is created with an empty password

#mkdir /var/lib/mysql-files

#chown -R mysql:mysql /var/lib/mysql
#chown -R mysql:mysql /var/lib/mysql-files

#su mysql
$mysqld --skip-grant-tables

then you in another console connect the database
#su mysql
$mysql -u root
mysql> alter user 'root'@'localhost' IDENTIFIED BY '123456';
mysql> quit

#kill `cat /var/run/mysqld/mysqld.pid`
restart mysql normally
#systemctl start mysqld.service
then you can go into this base with password 123456
# mysql -u root -p

PS: #is root prompt and $ is mysql prompt
  Posted by Dario Bertini on April 18, 2016
Be aware that apparmor whitelist the directories accessible by mysqld (see /etc/apparmor.d/usr.sbin.mysqld ) on Ubuntu (you might have similar problems with SElinux)

This means that to use the --init-file option you might only be able to get it to read a file in a few directories

You will get errors like:

2016-04-18T12:14:28.335888Z 0 [ERROR] /usr/sbin/mysqld: File '/tmp/resetmysql' not found (Errcode: 13 - Permission denied)

or

2016-04-18T12:11:08.565341Z 0 [ERROR] /usr/sbin/mysqld: File '/dev/fd/63' not found (Errcode: 2 - No such file or directory)
Sign Up Login You must be logged in to post a comment.