Can't change MySQL password [duplicate] - mysql

This question already has answers here:
How to change MySQL root password to default?
(2 answers)
Closed 7 years ago.
I installed MySQL, and the installation requires a password. I didn't want to enter one, but I had to to click "Next" and finish the installation.
So, I tried to change it.
In the cmd prompt:
C:\>mysql -u root -p
Enter password: **
mysql> use mysql;
Database changed
mysql> select `password` from `user` where `user` = 'root';
That returns an empty password field. But when I do mysql -u root I get the access denied error, and then I do mysql -u root -p and it asks for a password, I press enter, and I get denied access. So I repeat what's in the code block above, and the same thing happens.

According to MySQL's documentation on How to Reset the Root Password:
UPDATE mysql.user SET password=PASSWORD("my-new-password") WHERE User='root';
FLUSH PRIVILEGES;

Try this
In your command prompt,
Type the following :
mysqladmin -u root (assuming that ur username is "root")
Hit enter button, then it will show you commands to execute some stuffs.
for password change
Type the following ;
mysqladmin -u root password
It will then ask for new password
Enter ur new password , that is, the password you wish to use. Hit enter, it will request it again. Enter it and hit enter.
That is it, your password is changed
Now you can check the status
Type:
mysqladmin -u root -p status
It says, enter password ,
Use your new password
I hope it helps

Related

Change MySQL password without displaying it

There are many solutions and documentation on the web that have an answer on how to change a MySQL user password. All of them though, display the password to be changed. Is there a way that the new password be hidden or replaced by asterisks for increased security?
You can use the mysqladmin command:
mysqladmin -u user -p -h localhost password
where:
user: is the username.
localhost: you can change this to your host, or leave it as is for localhost.
password: is the mysqladmin command that instructs the change of password.
By executing the above command, you will be prompted to enter you current password and then type your new password, both without the characters being displayed on the screen.

Unable to access MySql root user, even after setting the password through safemode in linux terminal

1) service stoped
sudo /etc/init.d/mysqld stop
2) started in safe mode
sudo mysqld_safe --skip-grant-tables &
3) user created
mysql -u root
4) setting passwd
UPDATE user SET Password=PASSWORD('my_password') where USER='root';
exit from the mysql console,
and restart the service and trying to access mysql console with
mysql -u root -p my_password
[vikram#VoltyLinux ~]$ mysql -u root -p
Enter password: *******
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using
password: YES)
Go in as before in Step 2, that is, restart mysql daemon in safe-mode.
Issue the command
select user,host,password from mysql.user where user='root';
cut and paste output into a text editor for comparison (of the hashed password column values) before and after the following.
For each row notice the host column. In the following example I am assuming I have two rows. One row has % as host, the next has localhost
For each row that had a host variation, you will issue one command. So in my example above, I would be issuing 2 commands in total, such as:
SET PASSWORD FOR 'root'#'%' = PASSWORD('MyNewPassword');
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('MyNewPassword');
run
select user,host,password from mysql.user where user='root';
cut and paste that into text editor. Notice the change to the password hashes.
Shutdown the daemon from safe mode, and restart normally.
Try to login as root with MyNewPassword
Here are a few links. The one for SET PASSWORD Syntax, and an often next step for GRANT Syntax.
For the narrow scope of this question, that is to login, the grants would not be necessary. But without proper grants to databases, one would be sandboxed, not able to do much, other than simple commands like select now();
I am not suggesting that your root user does not have privileges once the password is changed. The Grants are necessary for normal users to be sure.

SQL Password not setting * not change password but not setting password

Having trouble setting the password on the command line for MYSQL. My original installation from homebrew was successful and I was able to access the database by entering mysql. However when I am setting up the new password it will not recognize it. I know that I have been typing my new password and confirming my new password correctly so I don't think it an error like that.
Code:
Fareedahs-MacBook-Pro:~ fareedahjames$ mysqladmin password
New password:
Confirm new password:
mysqladmin: unable to change password; error: 'Can't find any matching row in the user table'
I even tried entering the password incorrectly just to test the error message so when the password i have typed match it is not setting the password
code:
Fareedahs-MacBook-Pro:~ fareedahjames$ mysqladmin password
New password:
Confirm new password:
mysqladmin: Passwords don't match
I read on another post about using
SET PASSWORD FOR 'root'#'127.0.0.1' = password('cangetin');
but it did not work
I tried running that in mysql and got
ERROR 1044 (42000): Access denied for user ''#'localhost' to database 'mysql'
I am at a loss
It seems you didn't have the grant permission..
check the entries in your mysql.user table and paste here if possible.
Thanks
Changing MySQL root user password using mysql command
This is an another method. MySQL stores username and passwords in user table inside MySQL database. You can directly update or change the password using the following method for user called root:
Login to mysql server, type the following command at shell prompt:
$ mysql -u root -p
Use mysql database (type command at mysql> prompt):
mysql> use mysql;
Change password for user root, enter:
mysql> update user set password=PASSWORD("NEWPASSWORD") where User='root';
Finally, reload the privileges:
mysql> flush privileges;
mysql> quit

'Access denied for user 'root'#'localhost' (using password: NO)'

I'm trying to set the password of the user root but I'm gettin the error below, any idea?
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root#localhost |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' IDENTIFIED BY PASSWORD '*436576511F70A4E3B305E1AB8E209851945D8687' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> exit
Bye
root#tirengarfio:/var/www/rs2# mysqladmin -u root password foo,
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'#'localhost' (using password: NO)'
$ mysqladmin -u root -p password
Enter password:
New password:
Confirm new password:
password is to be typed literally. It's a command. You don't have to substitute password with your actual password.
# /etc/init.d/mysqld stop
Stopping MySQL: [ OK ]
# mysqld_safe --skip-grant-tables &
[1] 13694
# Starting mysqld daemon with databases from /var/lib/mysql
# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
Set/Change password:
mysqladmin -u root -p password
Login to MySQL console:
mysql -u root -p
To exit the console:
.\q
when trying to run this command i got the same error
sudo mysqladmin create asteriskcdrdba
i simply add a few lines to the code
-u root -p
and pressed the enter key. i then typed my password and hit enter. Linux liked my command as nothing more was displayed
so maybe try
sudo <your command here> -u <username> -p
after that hit enter and enter your password
Those are all good answers, but don't quite touch the deep cause of the problem one most likely has if faced with the OP question. That is, not knowing the ORIGINAL "starting" password created during the installation time.
And all these command lines with "-u root ..." etc. imply knowing and using THAT password.
Now this part from the original installation message may help anyone facing the problem above:
Initial password for first time use of MySQL is saved in $HOME/.mysql_secret
ie. when you want to use "mysql -u root -p" first you should see password
in /root/.mysql_secret
This is basically a more detailed version of a previous answer.
In your Terminal, go to the location of your utility program, mysqladmin
For example, if you were doing local development and using an application like M/W/XAMP, you might go to the directory:
/Applications/MAMP/Library/bin
This is where mysqladmin resides.
If you're not using an application like MAMP, you may also be able to find your local installation of mysql at: /usr/local/mysql
And then if you go to: /usr/local/mysql/bin/
You are in the directory where mysqladmin resides.
Then, to change the password, you will do the following:
At your Terminal prompt enter the exact command below (aka copy and paste) and press enter. The word "password" is part of the command, so don't be confused and come to the conclusion that you need to replace this word with some password you created previously or want to use in the future. You will have a chance to enter a new password soon enough, but it's not in this first command that you will do that:
./mysqladmin -u root -p password
The Terminal will ask you to enter your original or initial password, not a new one yet. From the above image you provided, it looks like you have one already created, so enter it here:
Enter password: oldpassword
The Terminal will ask you to enter a new password. Type it here and press enter:
New password: newpassword
Then the Terminal will ask you to confirm the new password. Type it here and press enter:
Confirm new password: newpassword
Reset or restart your Terminal.
In some cases, as with M/W/XAMP, you will have to update this new password in various files in order to get your application running properly again.
open my.cnf using following path
C:\xampp\mysql\bin
under # The following options will be passed to all MySQL clients
#password remove # comment sign if it is there password = "newpassword"
save file
close file
re-start mysql
Firstly, go to the folder support-files on terminal, and start the server
by mysql.server start,
Secondly, go to the folder bin on terminal or type
/usr/local/mysql/bin/mysqladmin -u root -p password
It would ask you for the old temporary password which was given to you while installing Mysql, type that and type in your new password and it would work.

MySQL root password change

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?
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('mypass');
FLUSH PRIVILEGES;
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
You can find Resetting the Root Password in the MySQL documentation.
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.
Please follow the below steps.
sudo service mysql stop
sudo mysqld_safe --skip-grant-tables
sudo service mysql start
sudo mysql -u root
use mysql;
show tables;
describe user;
update user set authentication_string=password('1111') where user='root';
FLUSH PRIVILEGES;
Log in with password "1111".
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;
I searched around as well and probably some answers do fit for 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 in stead 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...
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.
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';
On MySQL 8 you need to specify the password hashing method:
ALTER USER 'root'#'localhost' IDENTIFIED WITH caching_sha2_password BY 'new-password';
This worked for me -
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
Chapter 4 Resetting the Root Password: Windows Systems
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');
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
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'.
Using the mysqladmin command-line utility to alter the MySQL password:
mysqladmin --user=root --password=oldpassword password "newpassword"
Source
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';
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'
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';
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;
Update from 2022
I've tried a few of the answer but the one that works for me is the following
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
Courtesy of StrongDM
Note: I'm using the MySql client for Windows 10 and I'm also logging as the root user.
In MySQL 5.7, the password is replaced
with 'authentication_string'. Use
update user set authentication_string=password('myfavpassword') where user='root';
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.
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
Or just use interactive configuration:
sudo mysql_secure_installation
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.
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: wamp → bin → mysql → mysql (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...
Resetting root password.
sudo mysql --defaults-file=/etc/mysql/debian.cnf
alter user 'root'#'localhost' identified with mysql_native_password by 'new_password';
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.
On Mac open system preferences   MySQL.
In the configuration section of MySQL, check for "Initialize Database".
Change the password in the prompt.