MySQL root password change
Asked Answered
O

27

193

I have been trying to reset my MySQL root password. I have run mysqld_safe --skip-grant-tables, updated the root password, and checked the user table to make sure it is there.

Once restarting the MySQL daemon I tried logging in with the new root password that I just set and still get Access denied for user 'root' errors. I have also tried completely removing and reinstalling MySQL (including removing the my.cnf file) and still no luck. What can I do next?

Ochlophobia answered 23/9, 2011 at 19:47 Comment(6)
please post your actual errorHives
If your not locked out SET PASSWORD FOR 'root'@'localhost' = PASSWORD('somepassword'); is safest.Keelson
I followed the steps in the video to change password in one of my servers youtu.be/gFo5DV_pSg8Bryanbryana
Possible duplicate of ERROR 1698 (28000): Access denied for user 'root'@'localhost'Treece
Server version: 8.0.20-0ubuntu0.20.04.1 (Ubuntu) ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'PASSWORD("1")' at line 1 SET PASSWORD FOR 'root'@'localhost' = PASSWORD("somepassword");Bidle
Ubuntu 20.04 has the root user's auth plugin as: auth_socket. That plugin does not support a password. There is an answer below that talks about it. Solution is to change the plugin and password in one statement : ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Password'; The "WITH mysql_native_password" part changes the plugin.Forenoon
O
23

I found it! I forgot to hash the password when I changed it. I used this query to solve my problem:

update user set password=PASSWORD('NEW PASSWORD') where user='root';

I forgot the PASSWORD('NEW PASSWORD') and just put in the new password in plain text.

Ochlophobia answered 25/9, 2011 at 2:14 Comment(6)
make sure you come back and accept your answer to allow others to see the solutionBobstay
dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html shows how to do this with --init-file= if your looked out.Keelson
Any idea why when I use the same command, the following error is returned? Error 1054 (42S22): unknown column 'Password' in 'field list'Electrodynamometer
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('NEW PASSWORD') where user='root'' at line 1Bidle
I too get this error. syntax error. Please add the solution.Throughcomposed
where to run this query i cannot get anything even for mysql -vMarucci
E
246
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('mypass');
FLUSH PRIVILEGES;
Excavator answered 16/2, 2014 at 7:7 Comment(12)
This is working for me in 5.7 while the accepted answer is not.Motorman
This is the correct solution if your MySQL root password is set to blank.Snipes
> UPDATE mysql.user SET authentication_string=PASSWORD('MyNewPass') WHERE user='root'; >FLUSH PRIVILEGES; In MySQL version 5.7.x there is no more password field in the mysql table. It was replaced with authentication_string.Imelda
@Snipes Nope, I had MySQL 5.7 root with password, once I login in cli this code change the password to the new one.Helico
@Snipes Nope, I had MySQL 5.7 root with password, once I login in cli this code change the password to the new one.Helico
where am I supposed to enter this?Owe
This gives a OK to me, but don't actually do anythingSavarin
It needs an extra command FLUSH PRIVILEGES to make effective the change without restarting the database server.Digital
I did this to the root account (which already had a password) and now neither password works and I'm locked out. Could be because the flush privileges command was missing... not sure right now.Blouse
@Blouse - I don't think it's because of the above command. It can be because of text encode issues. Anyway from memory mysql server daemon has the option to run sql to reset password during startup. It gives you an option to reset master password.Excavator
Note this command is specifically for @'localhost'. I went on a wild goose chase trying to figure out why a different machine could not connect with the new password. See SELECT User,Host FROM mysql.user; to see various hosts to set the password for. (in my case @'%' for everywhere else)Fluviomarine
i just remove PASSWORD function.Pintle
A
85

Have a look at this from the MySQL reference manual:

First log in to MySQL:

mysql -u root -p

Then at the mysql prompt, run:

UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';

Then

FLUSH PRIVILEGES;

Look at this page for more information: Resetting the Root Password: Unix Systems

UPDATE:

For some versions of mysql, the password column is no longer available and you'll get this error:

ERROR 1054 (42S22): Unknown column 'Password' in 'field list'

In this case, use ALTER USER as shown in the answer below.

Adrianadriana answered 7/8, 2013 at 9:51 Comment(3)
I got an error "You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column..." when updating the user's password. See #11448568 for how to work around this error.Orozco
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('MyNewPass') WHERE User='root'' at line 1Bidle
PASSWORD function seems to have been removed. See this thread for alternatives #52321076Pauperize
S
40

Please follow the below steps.

  1. sudo service mysql stop
  2. sudo mysqld_safe --skip-grant-tables
  3. sudo service mysql start
  4. sudo mysql -u root
  5. use mysql;
  6. show tables;
  7. describe user;
  8. update user set authentication_string=password('1111') where user='root';
  9. FLUSH PRIVILEGES;

Log in with password "1111".

Subdominant answered 27/2, 2017 at 11:16 Comment(6)
flush privileges is important as a last step.Palatal
It's really done with the ass .I did all the tutorials to reset the password. Everything works except when I log in with the new password, it doesn't work.Bigelow
if mysqld is not starting and message like this mysqld daemon with databases from /var/lib/mysql mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended do this: mkdir /var/run/mysqld chomod 777 /var/run/mysqld and start all from the beggingMisanthropy
Step 2 - I advice to use: sudo mysqld_safe --skip-grant-tables --skip-networking & It is important to run the command ending with & so that it runs in the background. Passing –skip-networking option to skip networking that prevents other clients from connecting to the MySQL server. SourceSocket
chmod 777 = "sets permissions so that, (U)ser / owner can read, can write and can execute. (G)roup can read, can write and can execute. (O)thers can read, can write and can execute.". How secure is that?Devotee
Brevity is good, but can you elaborate in your answer? E.g., why are the first three steps required? What will happen if they are not followed? Aren't steps 6 and 7 for informational purposes only (which ones?) and not really required? Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).Devotee
I
33

This is the updated answer for WAMP v3.0.6 and up.

In the MySQL command-line client, phpMyAdmin or any MySQL GUI:

UPDATE mysql.user
SET authentication_string=PASSWORD('MyNewPass')
WHERE user='root';

FLUSH PRIVILEGES;

In MySQL version 5.7.x there is no more password field in the MySQL table. It was replaced with authentication_string. (This is for the terminal/CLI.)

In the MySQL command-line client, phpMyAdmin or any MySQL GUI:

UPDATE mysql.user SET authentication_string=PASSWORD('MyNewPass') WHERE user='root';

FLUSH PRIVILEGES;
Imelda answered 20/4, 2017 at 4:33 Comment(8)
This answer is one of the few who says that mysql.user table should be updated. Notice that if You are using any other database (set by the use command), You will get an error that the table user does not exist. Thank You, sir!Enthusiastic
works for me . I am using 5.7.x , there's no mysql.user table. other answers are out-datedMonticule
Happy to help!!Imelda
I roll it back because it is my intention to use > because of the semicolon per line which is intended for the terminalImelda
0 rows affected, rows matched: 3 changed: 0 ... ?Waiver
looks like that is a newer version. What version of MYSQL are you working on with?Imelda
In MySQL 8.0+ the function PASSWORD can NOT EXISTS. See #52321076 . So, instead PASSWORD('mypass') you can use CONCAT('*', UPPER(SHA1(UNHEX(SHA1('mypass'))))) that works as the old PASSWORD function worked.Perforation
@Perforation This saves the day. I think you should write an answer.Crosscountry
T
31

I searched around as well and probably some answers do fit some situations,

my situation is Mysql 5.7 on a Ubuntu 18.04.2 LTS system:

(get root privileges)

$ sudo bash

(set up password for root db user + implement security in steps)

# mysql_secure_installation

(give access to the root user via password instead of socket)

(+ edit: apparently, you need to set the password again?)

(don't set it to 'mySecretPassword'!!!)

# mysql -u root

mysql> USE mysql;
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> set password for 'root'@'localhost' = PASSWORD('mySecretPassword'); 
mysql> FLUSH PRIVILEGES;
mysql> exit;

# service mysql restart

Many thanks to zetacu (and erich) for this excellent answer (after searching a couple of hours...)

Enjoy :-D

S.

Edit (2020):

This method doesn't work anymore, see this question for future reference...

Treece answered 10/5, 2019 at 13:33 Comment(3)
This is the best answer so far, at least for mysql server 5.7.Fissure
The BEST answer. Thank you.Plainsman
mysql_secure_installation worked for me after a fresh installation.Spandex
S
28

On MySQL 8 you need to specify the password hashing method:

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'new-password';
Sturgis answered 31/12, 2020 at 13:37 Comment(2)
The only worked for me.Cambridgeshire
Don't forget the FLUSH PRIVILEGES too. This works if MySQL uses the new password hashing (I think it was introduced in MySQL 8.0, and was not mandatory at installation time, just recommended).Aponte
O
23

I found it! I forgot to hash the password when I changed it. I used this query to solve my problem:

update user set password=PASSWORD('NEW PASSWORD') where user='root';

I forgot the PASSWORD('NEW PASSWORD') and just put in the new password in plain text.

Ochlophobia answered 25/9, 2011 at 2:14 Comment(6)
make sure you come back and accept your answer to allow others to see the solutionBobstay
dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html shows how to do this with --init-file= if your looked out.Keelson
Any idea why when I use the same command, the following error is returned? Error 1054 (42S22): unknown column 'Password' in 'field list'Electrodynamometer
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('NEW PASSWORD') where user='root'' at line 1Bidle
I too get this error. syntax error. Please add the solution.Throughcomposed
where to run this query i cannot get anything even for mysql -vMarucci
V
21

On MySQL 8.0.4+

To update the current root user:

select current_user();
set password = 'new_password';

To update another user:

set password for 'otherUser'@'localhost' = 'new_password';

To set the password policy before updating the password:

set global validate_password.policy = 0;
set password = 'new_password';
set password for 'otherUser'@'localhost' = 'new_password';

Another / better way to update the root password:

mysql_secure_installation

Do you want to stick with 5.x authentication, so you can still use legacy applications?

In my.cnf file

default_authentication_plugin = mysql_native_password

To update root:

set global validate_password.policy = 0;
alter user 'root'@'localhost' identified with mysql_native_password by 'new_password';
Vacillate answered 7/7, 2019 at 6:14 Comment(0)
S
13

For MySQL 5.7.6 and later:

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

For MySQL 5.7.5 and earlier:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
Staghound answered 4/11, 2018 at 14:20 Comment(0)
A
13

For me, only these steps could help me setting the root password on version 8.0.19:

mysql
SELECT user,authentication_string FROM mysql.user;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_pass_here';
FLUSH PRIVILEGES;
SELECT user,authentication_string FROM mysql.user;

If you can see changes for the root user, then it works. Source: Can't set root password MySQL Server

Alper answered 4/2, 2021 at 11:52 Comment(3)
I don't know why setting the password for the MySQL root user has to be so obscure, but this worked on Ubuntu 21, MySQL 8. Thanks Sergey.Demarcate
this is the ONLY answer that works along many answers on many questions for mysql 8Rudder
This is the only answer that worked for me on 5.7.40Kellene
J
6

You have to reset the password! Steps for Mac OS X (tested and working) and Ubuntu:

Stop MySQL

sudo /usr/local/mysql/support-files/mysql.server stop

Start it in safe mode:

sudo mysqld_safe --skip-grant-tables

(The above line is the whole command.)

This will be an ongoing command until the process is finished, so open another shell/terminal window and log in without a password:

mysql -u root

mysql> UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root';

Start MySQL

sudo /usr/local/mysql/support-files/mysql.server start

Your new password is 'password'.

Jovita answered 19/2, 2015 at 7:51 Comment(2)
I've got a message : no database selectedTarweed
@Tarweed try this : #21945436Jovita
V
6

Using the mysqladmin command-line utility to alter the MySQL password:

mysqladmin --user=root --password=oldpassword password "newpassword"

Source

Vertebral answered 1/12, 2018 at 6:0 Comment(1)
This works, and works better as mysqladmin -p password prompting for old password once, and the new password twice.Repression
E
6

For the current latest MySQL version (8.0.16), none of these answers worked for me.

After looking at several different answers and combining them together, this is what I ended up using that worked:

update user set authentication_string='test' where user='root';
Edging answered 29/5, 2019 at 22:48 Comment(1)
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) and then [Warning] [MY-010319] [Server] Found invalid password for user: 'root@localhost'; Ignoring userYogurt
V
3

I tried the answer from kta, but it didn't work for me.

I am using MySQL 8.0.

This worked for me in the MySQL command-line client (executable mysql):

SET PASSWORD FOR 'root'@'localhost' = 'yourpassword'

Vaticide answered 11/3, 2019 at 11:35 Comment(3)
Please add some explanation to your code - how exactly does this solve a seven years old question?Micrometeorite
It looks like the syntax has been changed over the seven years. It's not wrapping password with PASSWORD() dev.mysql.com/doc/refman/8.0/en/set-password.htmlBozo
Password hash should be a 41-digit hexadecimal numberTarweed
G
3

This is for Mac users.


On 8.0.15 (maybe already before that version) the PASSWORD() function does not work. You have to do:

Make sure you have Stopped MySQL first (above). Run the server in safe mode with privilege bypass:

sudo mysqld_safe --skip-grant-tables

Replace this mysqld_safe with your MySQL path like in my case it was

sudo /usr/local/mysql/bin/mysqld_safe –skip-grant-tables

then you have to perform the following steps.

mysql -u root

UPDATE mysql.user SET authentication_string=null WHERE User='root';
FLUSH PRIVILEGES;
exit;

Then

mysql -u root

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
Groff answered 29/12, 2020 at 19:48 Comment(1)
This worked for me on Ubuntu 21.04, MySQL 8Paroxysm
I
3

Now just use:

SET PASSWORD FOR <user> = '<plaintext_password>'

Because 'SET PASSWORD FOR <user> = PASSWORD('<plaintext_password>')' is deprecated and will be removed in a future release.(Warning in 04/12 2021) Please use SET PASSWORD FOR <user> = '<plaintext_password>' instead.

Update 04/12 2021 AM 2:22:07 UTC/GMT -5 hours.

Use the following statement to modify directly in the mysql command line:

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

or 1.The terminal enters the bin directory of MySQL

cd /usr/local/mysql/bin

2.Open MySQL

mysql -u root -p

3.At this time you can use your default password

4.Perform operations in MySQL at this time

show databases;

5.You will be prompted to reset the root user password.

So how to reset the root password? I checked a lot of information but it didn’t take effect.

Including entering to modify the database in safe mode, using the mysqladmin command: "Mysqladmin -u root password"your-new-password"" etc., Will not work.

The correct steps are as follows:

1.It is still in the cd /usr/local/mysql/bin/ directory

2.sudo su

After entering, you will be asked to enter your computer password.

When you enter it, nothing is displayed. After you enter it, press Enter

Then press enter

3.Cross the authorization verification

sh-3.2# ./mysqld_safe --skip-grant-tables &

If the execution of the command is stopped, and the execution has been completed at this time,

press Enter directly, and then exit to exit:

sh-3.2# exit

4.Re-enter MySQL at this time, no -p parameter, no password

./mysql -u root

5.Select the database MySQL (here MySQL refers to a database in MySQL,

there are other databases in MySQL, you can view it through show databases;)

use mysql;

6.Update the password of the root user in the database table:

update user set authentication_string=‘123456’ where User='root';

Note: The password field here is authentication_string,

not the password circulated on the Internet.

It is estimated that MySQL was updated later.

Re-enter MySQL and use the password you just set, is it all right?

Because you have just set to bypass the authorization authentication,

you can log in to MySQL directly without a password.

My stupid way is to restart the computer and log in to MySQL with the password again to see if the modification is effective;

Interlay answered 4/12, 2021 at 6:54 Comment(0)
T
2

In MySQL 5.7, the password is replaced with 'authentication_string'. Use

update user set authentication_string=password('myfavpassword') where user='root';
Tiffin answered 14/5, 2018 at 11:34 Comment(0)
B
2

So many comments, but I was helped by this method:

sudo mysqladmin -u root password 'my password'

In my case after installation I had got the MySQL service without a password for the root user, and I needed to set the password for my security.

Blackpool answered 11/6, 2019 at 7:57 Comment(1)
It could be used to reset mysql root user password without knowing the old one. But make sure no one has access to your syslog, as this password is logged: Aug 07 16:21:55 ubuntupc sudo[93250]: user : TTY=pts/0 ; PWD=/home/user ; USER=root ; COMMAND=/usr/bin/mysqladmin -u root password 'my password'Linked
B
1

A common error I run into from time to time, is that I forget the -p option, so be sure to use:

mysql -u root -p
Bartko answered 23/9, 2011 at 19:53 Comment(0)
C
1

For example, you can change the password of the user root to apple with ALTER USER or SET PASSWORD as shown below. *You probably need to log in with the user root:

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

Or:

SET PASSWORD FOR 'root'@'localhost' = 'apple';

*Don't use the command below to change the password of the user root to apple because the password does not work to log in to MySQL with the user root. Finally, you have to reset the password following the doc:

UPDATE mysql.user SET authentication_string='apple' WHERE User='root';
FLUSH PRIVILEGES;

And, you can disable the password of the user root with '' as shown below:

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

Or:

SET PASSWORD FOR 'root'@'localhost' = '';

Then, you can log in with no password as shown below. *The doc says MYSQL_PWD is deprecated as of MySQL 8.0; expect it to be removed in a future version of MySQL. and my answer explains how to log in to MySQL without a password prompt:

mysql -u root

Or:

mysql -u root --password=''

Or:

mysql -u root --password=''

Or:

MYSQL_PWD='' mysql -u root

And, you can log in with no password for the password prompt as shown below:

mysql -u root -p
Enter password:

Or:

mysql -u root --password
Enter password:

Or:

MYSQL_PWD='' mysql -u root -p
Enter password:

Or:

MYSQL_PWD='' mysql -u root --password
Enter password:

Or on Windows, you can set the user root and no password under [client] in my.ini as shown below. *My answer explains [client] and my answer explains where my.ini is located on Windows and my answer explains how to log in with my.ini:

# "my.ini"

[client]
user="root"
password=""

Or:

# "my.ini"

[client]
user="root"
# password=""

Then, you can log in by setting my.ini's location to --defaults-file= or --defaults-extra-file= as shown below:

mysql --defaults-file='C:\ProgramData\MySQL\MySQL Server 8.0\my.ini'

Or:

mysql --defaults-extra-file='C:\ProgramData\MySQL\MySQL Server 8.0\my.ini'

*Not setting my.ini's location to --defaults-file= or --defaults-extra-file= get error as shown below:

mysql
ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)
Coterie answered 20/10, 2023 at 18:17 Comment(0)
A
0

Exit from WAMP and Stop all WAMP services.

Open Notepad and then type:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('');

Then save it to the C: drive with any name... like this "c:/example.txt"

Now go to your "wamp" folder: wampbinmysqlmysql (your version) → bin

In my case the path is "C:\wamp\bin\mysql\mysql5.6.17\bin".

Now copy your path, run CMD with (Ctrl + R), and then type "cmd" (Enter).

Type cd, right click on CMD, and paste the path (Enter).

Now type (mysqld --init-file=C:\\example.txt) without braces and (Enter).

Then restart the PC or open Task Manager and kill mysqld.exe.

Start WAMP and your password will be removed...

Araiza answered 15/7, 2015 at 8:18 Comment(1)
On Windows, I presume (not stated).Devotee
S
0

For macOS users, if you forget your root password, thusharaK's answer is good, but there are a few more tricks:

If you are using a system preference to start MySQL serverside, simply

sudo mysqld_safe --skip-grant-tables

might not work for you.

You have to make sure the command-line arguments are the same with the system start configuration.

The following command works for me:

/usr/local/mysql/bin/mysqld --user=_mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/mysqld.local.err --pid-file=/usr/local/mysql/data/mysqld.local.pid --keyring-file-data=/usr/local/mysql/keyring/keyring --early-plugin-load=keyring_file=keyring_file.so --skip-grant-tables

You can use

ps aux | grep mysql

to check your own.

Shipshape answered 20/4, 2017 at 5:13 Comment(0)
A
0

Or just use interactive configuration:

sudo mysql_secure_installation
Ambur answered 16/9, 2020 at 12:55 Comment(0)
I
0

Resetting root password.

  1. sudo mysql --defaults-file=/etc/mysql/debian.cnf

  2. alter user 'root'@'localhost' identified with mysql_native_password by 'new_password';

Imperishable answered 17/6, 2022 at 7:1 Comment(1)
THIS FILE IS OBSOLETE. STOP USING IT IF POSSIBLE. This file exists only for backwards compatibility for tools that run '--defaults-file=/etc/mysql/debian.cnf' and have root level access to the local filesystem. With those permissions one can run 'mariadb' directly anyway thanks to unix socket authentication and hence this file is useless.Maller
P
0

The other answers did not work for me.

That said, here is how I did it for the password change to the root SQL profile if you can log in already.

While logged in:

Tool Bar -> Servers -> Users and Privileges -> select root - > from here you can change the password, or expire it and change it in the login.

Pogy answered 27/3, 2023 at 16:2 Comment(0)
A
-1

On Ubuntu,

sudo dpkg-reconfigure mysql-server-5.5

Replace 5.5 with your current version and you will be asked for the new root password.

Angelita answered 26/9, 2016 at 9:2 Comment(1)
In what OS? This makes no sense.Palatal
G
-2
  1. On Mac open system preferences   MySQL.

  2. In the configuration section of MySQL, check for "Initialize Database".

  3. Change the password in the prompt.

    Initialize database

Gibbon answered 27/8, 2020 at 10:12 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.