How to find out the MySQL root password - mysql

I cannot figure out my MySQL root password; how can I find this out? Is there any file where this password is stored?
I am following this link but I do not have directadmin directory in local.

thanks to #thusharaK I could reset the root password without knowing the old password.
On ubuntu I did the following:
sudo service mysql stop
sudo mysqld_safe --skip-grant-tables --skip-syslog --skip-networking
Then run mysql in a new terminal:
mysql -u root
And run the following queries to change the password:
UPDATE mysql.user SET authentication_string=PASSWORD('password') WHERE User='root';
FLUSH PRIVILEGES;
In MySQL 5.7, the password field in mysql.user table field was removed, now the field name is 'authentication_string'.
Quit the mysql safe mode and start mysql service by:
mysqladmin shutdown
sudo service mysql start

You can't view the hashed password; the only thing you can do is reset it!
Stop MySQL:
sudo service mysql stop
or
$ sudo /usr/local/mysql/support-files/mysql.server stop
Start it in safe mode:
$ sudo mysqld_safe --skip-grant-tables
(above line is the whole command)
This will be an ongoing command until the process is finished so open another shell/terminal window, log in without a password:
$ mysql -u root
mysql> UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root';
MySQL 5.7 and over:
mysql> use mysql;
mysql> update user set authentication_string=password('password') where user='root';
Exit the MySQL CLI:
mysql> exit
Restart MySQL in normal mode, first stopping the safe mode instance:
$ mysqladmin -u root -p shutdown # (when prompted, enter the new password just set)
$ sudo service mysql start
or
$ sudo /usr/local/mysql/support-files/mysql.server start
Your new password is 'password'.

MySQL 5.7 and above saves root in MySQL log file.
Please try this:
sudo grep 'temporary password' /var/log/mysqld.log

One thing that tripped me up on a new install of MySQL and wondering why I couldn't get the default password to work and why even the reset methods where not working.
Well turns out that on Ubuntu 18 the most recent version of MySQL server does not use password auth at all for the root user by default. So this means it doesn't matter what you set it to, it won't let you use it. It's expecting you to login from a privileged socket.
mysql -u root -p
This will not work, even if you are using the correct password.
Instead, you need to use:
sudo mysql
that will work with out any password.
then once you in you need type in
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'Password you want to use';
Then log out and now it will accept your password.

Follow these steps to reset password in Windows system
Stop Mysql service from task manager
Create a text file and paste the below statement
MySQL 5.7.5 and earlier:
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('yournewpassword');
MySQL 5.7.6 and later:
ALTER USER 'root'#'localhost' IDENTIFIED BY 'yournewpassword';
Save as mysql-init.txt and place it in 'C' drive.
Open command prompt and paste the following
C:\> mysqld --init-file=C:\\mysql-init.txt

You cannot find it. It is stored in a database, which you need the root password to access, and even if you did get access somehow, it is hashed with a one-way hash. You can reset it: How to Reset the Root Password

This worked for me:
On terminal type the following
$ sudo mysql -u root -p
Enter password://just press enter
mysql>

Unless the package manager requests you to type the root password during installation, the default root password is the empty string. To connect to freshly installed server, type:
shell> mysql -u root --password=
mysql>
To change the password, get back the unix shell and type:
shell> mysqladmin -u root --password= password root
The new password is 'root'. Now connect to the server:
shell> mysql -u root --password=
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
Oops, the password has changed. Use the new one, root:
shell> mysql -u root --password=root
...
blah, blah, blah : mysql welcome banner
...
mysql>
Bingo! New do something interesting
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
Maurycy

As addition to the other answers, in a cpanel installation, the mysql root password is stored in a file named /root/.my.cnf. (and the cpanel service resets it back on change, so the other answers here won't help)

you can view mysql root password , well i have tried it on mysql 5.5 so do not know about other new version well work or not
nano ~/.my.cnf

The default password which worked for me after immediate installation of mysql server is : mysql

The procedure changes depending the version of MySql. Follow the procedure exactly as described for your version:
HINTS - Read before the instructions page for your version of MySql*
In step 5: Instead of run CMD, create a shortcut on your desktop calling CDM.exe. Then right-click on the shortcut and select "Execute as Administrator".
In step 6: Skip the first proposed version of the command and execute the second one, the one with the --defaults-file parameter
Once you execute the command, if everything is ok, the CMD window remains open and the command of step 6 continues executing. Simply close the window (click 'x'), and then force close MySQl from the Task Manager.
Delete the file with the SQL commands, and start again MySQL. The password must be changed now.
5.0
http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html
5.1
http://dev.mysql.com/doc/refman/5.1/en/resetting-permissions.html
... just change the version in the link (5.5, 5.6, 5.7)

In your "hostname".err file inside the data folder MySQL works on, try to look for a string that starts with:
"A temporary password is generated for roor#localhost "
you can use
less /mysql/data/dir/hostname.err
then slash command followed by the string you wish to look for
/"A temporary password"
Then press n, to go to the Next result.

I solved this a different way, this may be easier for some.
I did it this way because I tried starting in safe mode but cannot connect with the error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
What I did was to connect normally as root:
$ sudo mysql -u root
Then I created a new super user:
mysql> grant all privileges on *.* to 'myuser'#'%' identified by 'mypassword' with grant option;
mysql> quit
Then log in as myuser
$ mysql -u myuser -p -h localhost
Trying to change the password gave me no errors but did nothing for me so I dropped and re-created the root user
mysql> drop user 'root'#'localhost;
mysql> mysql> grant all privileges on *.* to 'root'#'localhost' identified by 'mypassword' with grant option;
The root user is now working with the new password

Using Debian / Ubuntu mysql packages, you can login with user debian-sys-maint, which has all the expected privileges, the password is stored in the file /etc/mysql/debian.cnf

Answers provided here did not seem to work for me, the trick turned out to be:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'test';
(complete answer here: Change user password in MySQL 5.7 with “plugin: auth_socket”)

System:
CentOS Linux 7
mysql Ver 14.14 Distrib 5.7.25
Procedure:
Open two shell sessions, logging in to one as the Linux root user
and the other as a nonroot user with access to the mysql command.
In your root session, stop the normal mysqld listener and start a
listener which bypasses password authentication (note: this is a
significant security risk as anyone with access to the mysql
command may access your databases without a password. You may want
to close active shell sessions and/or disable shell access before
doing this):
# systemctl stop mysqld
# /usr/sbin/mysqld --skip-grant-tables -u mysql &
In your nonroot session, log in to mysql and set the mysql root password:
$ mysql
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> SET PASSWORD FOR 'root'#'localhost' = PASSWORD('MyNewPass');
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> quit;
In your root session, kill the passwordless instance of mysqld and restore the normal mysqld listener to service:
# kill %1
# systemctl start mysqld
In your nonroot session, test the new root password you configured above:
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
mysql>

I was stuck with this problem for a couple of minutes and the following was the only solution that actually worked:
https://phoenixnap.com/kb/access-denied-for-user-root-localhost
sudo mysql
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'insert_password';
mysql -u root -p

In case you already set a password in the past the mysql -uroot -p solution will not work,
In my case I used some of the answers above to solve this (Ubuntu 16). The result was:
sudo service mysql stop
sudo mysqld_safe --skip-grant-tables &
if you see this text in the screen:
mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
then do:
sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld
sudo mysqld_safe --skip-grant-tables & # Look at the & at the end!
Enter other terminal to set your password like this:
sudo mysql -u root
mysql> use mysql;
mysql> SET PASSWORD FOR 'root'#'localhost'=PASSWORD('__NEW__PASSWORD__');
mysql> flush privileges;
mysql> quit;
then restart the service and login
# end mysqld_safe in the other terminal
sudo service mysql start
sudo mysql -h 127.0.0.1 -uroot -p

For MySQL 5.5 on Windows 10
You can't find the password as it is hashed in the table, so resetting it is the only option.
The solution of importing the new password script by .txt file, as offered by Lokesh kumar Chippada, didn't work for me. I found that the command prompt just froze after initiating the import.
I added skip-grant-tables to the my.ini file as per the top the answer on this SO post by tonycoupland.
I was then able to login to mysql from the command line
$> mysql
and then in mysql
mysql> FLUSH PRIVILEGES;
mysql> SET PASSWORD FOR 'root'#'localhost' = PASSWORD('MyNewPass');
See 'B.3.3.2.3 Resetting the Root Password: Generic Instructions' on mysql dev page. I have now removed skip-grant-tables from the my.ini file, and I can login as a root user using the new password I created.

Go to phpMyAdmin > config.inc.php > $cfg['Servers'][$i]['password'] = '';

Related

MySQL Error: : 'Access denied for user 'root'#'localhost'

Consider:
./mysqladmin -u root -p** '_redacted_'
Output (including typing the password):
Enter password:
mysqladmin: connect to server at 'localhost' failed error:
'Access denied for user 'root'#'localhost' (using password: YES)'
How can I fix this?
All solutions I found were much more complex than necessary and none worked for me. Here is the solution that solved my problem. There isn't any need to restart mysqld or start it with special privileges.
sudo mysql
-- for MySQL
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
-- for MariaDB
ALTER USER 'root'#'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('root');
With a single query we are changing the auth_plugin to mysql_native_password and setting the root password to root (feel free to change it in the query).
Now you should be able to log in with root. More information can be found in MySQL documentation or MariaDB documentation.
(Exit the MySQL console with Ctrl + D or by typing exit.)
Open and edit /etc/my.cnf or /etc/mysql/my.cnf, depending on your distribution.
Add skip-grant-tables under [mysqld]
Restart MySQL
You should be able to log in to MySQL now using the below command mysql -u root -p
Run mysql> flush privileges;
Set new password by ALTER USER 'root'#'localhost' IDENTIFIED BY 'NewPassword';
Go back to /etc/my.cnf and remove/comment skip-grant-tables
Restart MySQL
Now you will be able to login with the new password mysql -u root -p
None of the previous answers helped me with this problem, so here's the solution I found.
The relevant part:
In Ubuntu systems running MySQL 5.7 (and later versions), the root MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password. This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external program (e.g., phpMyAdmin) to access the user.
In order to use a password to connect to MySQL as root, you will need to switch its authentication method from auth_socket to mysql_native_password. To do this, open up the MySQL prompt from your terminal:
sudo mysql
Next, check which authentication method each of your MySQL user accounts use with the following command:
SELECT user,authentication_string,plugin,host FROM mysql.user;
Output
+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| root | | auth_socket | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)
In this example, you can see that the root user does in fact authenticate using the auth_socket plugin. To configure the root account to authenticate with a password, run the following ALTER USER command. Be sure to change password to a strong password of your choosing, and note that this command will change the root password you set in Step 2:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Then, run FLUSH PRIVILEGES which tells the server to reload the grant tables and put your new changes into effect:
FLUSH PRIVILEGES;
Check the authentication methods employed by each of your users again to confirm that root no longer authenticates using the auth_socket plugin:
SELECT user,authentication_string,plugin,host FROM mysql.user;
Output
+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| root | *3636DACC8616D997782ADD0839F92C1571D6D78F | mysql_native_password | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)
You can see in this example output that the root MySQL user now authenticates using a password. Once you confirm this on your own server, you can exit the MySQL shell:
exit
I tried many steps to get this issue corrected. There are so many sources for possible solutions to this issue that is is hard to filter out the sense from the nonsense. I finally found a good solution here:
Step 1: Identify the database version
mysql --version
You'll see some output like this with MySQL:
mysql Ver 14.14 Distrib 5.7.16, for Linux (x86_64) using EditLine wrapper
Or output like this for MariaDB:
mysql Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using readline 5.1
Make note of which database and which version you're running, as you'll use them later. Next, you need to stop the database so you can access it manually.
Step 2: Stopping the database server
To change the root password, you have to shut down the database server beforehand.
You can do that for MySQL with:
sudo systemctl stop mysql
And for MariaDB with:
sudo systemctl stop mariadb
Step 3: Restarting the database server without permission checking
If you run MySQL and MariaDB without loading information about user privileges, it will allow you to access the database command line with root privileges without providing a password. This will allow you to gain access to the database without knowing it.
To do this, you need to stop the database from loading the grant tables, which store user privilege information. Because this is a bit of a security risk, you should also skip networking as well to prevent other clients from connecting.
Start the database without loading the grant tables or enabling networking:
sudo mysqld_safe --skip-grant-tables --skip-networking &
The ampersand at the end of this command will make this process run in the background so you can continue to use your terminal.
Now you can connect to the database as the root user, which should not ask for a password.
mysql -u root
You'll immediately see a database shell prompt instead.
MySQL Prompt
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
MariaDB Prompt
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
Now that you have root access, you can change the root password.
Step 4: Changing the root password
mysql> FLUSH PRIVILEGES;
Now we can actually change the root password.
For MySQL 5.7.6 and newer as well as MariaDB 10.1.20 and newer, use the following command:
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'new_password';
For MySQL 5.7.5 and older as well as MariaDB 10.1.20 and older, use:
mysql> SET PASSWORD FOR 'root'#'localhost' = PASSWORD('new_password');
Make sure to replace new_password with your new password of choice.
Note: If the ALTER USER command doesn't work, it's usually indicative of a bigger problem. However, you can try UPDATE ... SET to reset the root password instead.
[IMPORTANT] This is the specific line that fixed my particular issue:
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('new_password') WHERE User = 'root' AND Host = 'localhost';
Remember to reload the grant tables after this.
In either case, you should see confirmation that the command has been successfully executed.
Query OK, 0 rows affected (0.00 sec)
The password has been changed, so you can now stop the manual instance of the database server and restart it as it was before.
Step 5: Restart the Database Server Normally
The tutorial goes into some further steps to restart the database, but the only piece I used was this:
For MySQL, use:
sudo systemctl start mysql
For MariaDB, use:
sudo systemctl start mariadb
Now you can confirm that the new password has been applied correctly by running:
mysql -u root -p
The command should now prompt for the newly assigned password. Enter it, and you should gain access to the database prompt as expected.
Conclusion
You now have administrative access to the MySQL or MariaDB server restored. Make sure the new root password you choose is strong and secure and keep it in safe place.
After trying all others answers, this it what finally worked for me:
sudo mysql -- It does not ask me for any password
-- Then in MariaDB/MySQL console:
update mysql.user set plugin = 'mysql_native_password' where User='root';
FLUSH PRIVILEGES;
exit;
I found the answer in the blog post Solved: Error “Access denied for user ‘root’#’localhost’” of MySQL — codementor.tech (Medium).
For Ubuntu/Debian users
(It may work on other distributions, especially Debian-based ones.)
Run the following to connect as root (without any password)
sudo /usr/bin/mysql --defaults-file=/etc/mysql/debian.cnf
If you don't want to add --defaults-file each time you want to connect as root, you can copy /etc/mysql/debian.cnf into your home directory:
sudo cp /etc/mysql/debian.cnf ~/.my.cnf
And then:
sudo mysql
In my experience, if you run without sudo it will not work. So make sure your command is;
sudo mysql -uroot -p
For new Linux users this could be a daunting task. Let me update this with MySQL 8 (the latest version available right now is 8.0.12 as on 2018-09-12)
Open "mysqld.cnf" configuration file at "/etc/mysql/mysql.conf.d/".
Add skip-grant-tables to the next line of [mysql] text and save.
Restart the MySQL service as "sudo service mysql restart". Now your MySQL is free of any authentication.
Connect to the MySQL client (also known as mysql-shell) as mysql -u root -p. There is no password to be keyed in as of now.
Run SQL command flush privileges;
Reset the password now as ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPassword';
Now let's get back to the normal state; remove that line "skip-grant-tables" from "mysqld.cnf" and restart the service.
That's it.
In my case under Debian 10, the error
ERROR 1698 (28000): Access denied for user 'root'#'localhost'
was solved by (good way)
sudo mysql -u root -p mysql
Bad way:
mysql -u root -p mysql
I did this to set my root password in the initial set up of MySQL in OS X. Open a terminal.
sudo sh -c 'echo /usr/local/mysql/bin > /etc/paths.d/mysql'
Close the terminal and open a new terminal.
And the following worked in Linux, to set the root password.
sudo /usr/local/mysql/support-files/mysql.server stop
sudo mysqld_safe --skip-grant-tables
(sudo mysqld_safe --skip-grant-tables: This did not work for me the first time. But on the second try, it was a success.)
Then log into MySQL:
mysql -u root
FLUSH PRIVILEGES;
Now change the password:
ALTER USER 'root'#'localhost' IDENTIFIED BY 'newpassword';
Restart MySQL:
sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server start
My Station here:
UBUNTU 21.04
PHP 5.6.40-57
MYSQL 5.7.37
let's config it
nano /etc/mysql/mysql.conf.d/mysqld.cnf
at the bottom, write this
skip-grant-tables
reload it
service mysql restart
In your MySQL Workbench, you can go to the left sidebar, under Management select "Users and Privileges", click root under User Accounts, in the right section click tab "Account Limits" to increase the maximum queries, updates, etc., and then click tab "Administrative Roles" and check the boxes to give the account access.
Ugh - nothing worked for me! I have a CentOS 7.4 machine running MariaDB 5.5.64.
I had to do this, right after installation of MariaDB from YUM;
systemctl restart mariadb
mysql_secure_installation
The mysql_secure_installation will take you through a number of steps, including "Set root password? [Y/n]". Just say "y" and give it a password. Answer the other questions as you wish.
Then you can get in with your password, using
mysql -u root -p
It will survive
systemctl restart mariadb
The Key
Then, I checked the /bin/mysql_secure_installation source code to find out how it was magically able to change the root password and none of the other answers here could. The import bit is:
do_query "UPDATE mysql.user SET Password=PASSWORD('$esc_pass') WHERE User='root';"
...It says SET Password=... and not SET authentication_string = PASSWORD.... So, the proper procedure for this version (5.5.64) is:
Log in using mysql -u root -p, using the password you already set.
Or, stop the database and start it with:
mysql_safe --skip-grant-tables --skip-networking &
From the mysql> prompt:
use mysql;
select host,user,password from user where user = 'root';
(observe your existing passwords for root).
UPDATE mysql.user set Password = PASSWORD('your_new_cleartext_password') where user = 'root' AND host = 'localhost';
select host,user,password from user where user = 'root';
flush privileges;
quit;
Kill the running mysqld_safe. Restart MariaDB. Log in as root: mysql -u -p. Use your new password.
If you want, you can set all the root passwords at once. I think this is wise:
mysql -u root -p
(login)
use mysql;
select host,user,password from user where user = 'root';
UPDATE mysql.user set Password = PASSWORD('your_new_cleartext_password') where user = 'root';
select host,user,password from user where user = 'root';
flush privileges;
quit;
This will perform updates on all the root passwords: i.e., for "localhost", "127.0.0.1", and "::1"
In the future, when I go to RHEL 8 or what have you, I will try to remember to check the /bin/mysql_secure_installation and see how the guys did it, who were the ones that configured MariaDB for this OS.
Use sudo to alter your password:
sudo mysql
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'insert_password';
Source: Phoenixnap - Access denied for user root localhost
Fix for macOS
Install MySQL from https://downloads.mysql.com/archives/community/ (8.x is the latest as on date, but ensure that the version is compatible with the macOS version)
Give password for root (let <root-password> be the password) during installation (don't forget to remember the password!)
Select Use Legacy Password Encryption option (that is what I had used and did not try for Use Strong Password Encryption option)
Search and open MySQL.prefPane (use search tool)
Select Configuration tab
Click Select option of Configuration File
Select /private/etc/my.cnf
From terminal open a new or existing file with name /etc/my.cnf (vi /etc/my.cnf) add the following content:
[mysqld]
skip-grant-tables
Restart mysqld as follows:
ps aux | grep mysql
kill -9 <pid1> <pid2> ... (grab pids of all MySQL related processes)
mysqld gets restarted automatically
Verify that the option is set by running the following from terminal:
ps aux | grep mysql
> mysql/bin/mysqld ... --defaults-file=/private/etc/my.cnf ... (output)
Run the following command to connect (let mysql-<version>-macos<version>-x86_64 be the folder where MySQL is installed. To grab the actual folder, run ls /usr/local/ and copy the folder name):
/usr/local/mysql-<version>-macos<version>-x86_64/bin/mysql -uroot -p<root-password>
If you are like me and all the information in previous answers failed, proceed to uninstall all versions of MySQL on your machine, search for all remaining MySQL files using the command sudo find / -name "mysql" and rm -rf every file or directory with the "mysql" name attached to it (you should skip files related to programming language libraries).
Now install a fresh version of MySQL and enjoy. NB: You will lose all your data so weigh your options first.
Sometimes a default password is set when you install it - as mentioned in the documentation. This can be confirmed by the following command.
sudo grep 'temporary password' /var/log/mysqld.log
It can happen if you don't have enough privileges.
Type su, enter the root password and try again.
After trying a lot with the following answer:
ALTER USER 'root'#'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('root');
And similar answers, my terminal was still throwing me the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near...
So after researching on the web, this line solved my problem and let me change the root user password:
sudo mysqladmin --user=root password "[your password]"
windows :
cd \Ampps\mysql\bin :
mysql.exe -u root -pmysql
after mysql start (you can see shell like this mysql> )
use this query :
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
try again access with root root
If you reached this page via Google like I did and none of the previous solutions worked, what turned out to be the error was 100% foolishness on my end. I didn't connect to the server. Once connected everything was smooth sailing.
In case it helps to know my setup, I'm using Sequel Pro and am trying to connect to it with Node using the NPM package, mysql. I didn't think I needed to actually connect (other than run Sequel Pro), because I was doing that from my application already.
I was getting the same error while setting up the mysql-8 zip version. Finally, switched to installer version which worked seamlessly. During installation, there is a prompt to set up the root password. Once set, it works for sure.
According to MariaDB official documentation, in MariaDB 10.4.3 and later, the unix_socket authentication plugin is installed by default.
In order to disable it, and revert to the previous mysql_native_password authentication method, add line below in [mysqld] section of my.cnf file:
[mysqld]
unix_socket=OFF
And then run:
mysql_install_db --auth-root-authentication-method=normal
And then start mysqld
This command will then work fine:
mysqladmin -u root password CHANGEME
For additional information, see Configuring mysql_install_db to Revert to the Previous Authentication Method.
I was trying to leverage Docker desktop on Mac to get 5.7.35 running and this docker-compose.yml configuration allowed it to work:
In particular it was the addition of the line...
command: --default-authentication-plugin=mysql_native_password
...that did the trick
version: '3.3'
services:
mysql_db:
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: 'your_password'
ports:
- '3306:3306'
expose:
- '3306'
volumes:
- ~/your/volume/path:/var/lib/mysql
One thing to check is the from-host filter. It may be "localhost" by default. Are you trying to connect from a remote client? Change this to "%".
On Arch Linux
Package: mysql 8.0.29-1
What worked for me:
Edit my.cnf file, normally can be found at /etc/mysql/my.cnf and append this skip-grant-tables at the bottom/end of the file.
Restart mysql service by invoking sudo systemctl restart mysqld
Ensuring mysql service has started properly by invoking sudo systemctl status mysqld
Login to mysql using 'root' by invoking mysql -u root -p
Flush privileges by invoking flush privileges;
Create new user by CREATE USER 'root'#'localhost' IDENTIFIED BY 'rootpassword';
(If you plan to use this db with PHP), you should instead use this CREATE USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'rootpassword';
Check whether your changes have reflected in db by invoking the following in sequence:
use mysql;
SELECT User, password_last_changed FROM user;
Exit mysql console and comment/remove skip-grant-tables by editing my.cnf file (Refer to step 1 for the location)
Restart the mysql service (Refer to step 2 and step 3)
And that's all.
The '-p' argument doesn't expect a space between the argument name and value.
Instead of
./mysqladmin -u root -p 'redacted'
Use
./mysqladmin -u root -p'redacted'
Or just
./mysqladmin -u root -p
which will prompt you for a password.
Solution: Give up!
Hear me out. I spent about two whole days trying to make MySQL work to no avail, always stuck with permission errors, none of which were fixed by the answers to this question. It got to the point that I thought if I continued I'd go insane.
Out of patience for making it work, I sent the command to install SQLite, only using 450 KB, and it worked perfectly right from the word go.
If you don't have the patience of a saint, go with SQLite and save yourself a lot of time, effort, pain, and storage space..!

How can I successfully login with root after running mysql_secure_installation?

I am currently unable to login as root on mysql and I am not quite sure about what's going on. Here's what happened:
I ran mysql_secure_installation as recommended, to secure my mysql installation. Afterwards, I typed the default root password that was asked and then I entered:
No to setting a password for root
Yes on removing anonymous users
Yes on disallowing remote root login
Yes on removing the test database and access to it
Yes on reloading privilege tables
After completing this process, I tried accessing mysql with mysql -u root -p (entered the default password) and received this message:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
Do you have any ideas on what might have went wrong?
I also tried resetting the root password by starting mysql with --skip-grant-tables, but I am still not able to login.
I am using Ubuntu 14.04 and mysql 14.14 (LAMP stack).
I know this is an old post but the main answer is outdated and did not solve my issue.
Below my steps for future reference if anybody is having similar problems.
Stop mysql if it's running
$ sudo service mysql stop
Start mysql in safe mode
$ sudo mysqld_safe --skip-grant-tables --skip-syslog --skip-networking
If you get the error
"mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists"
Just create that folder:
$ mkdir -p /var/run/mysqld
$ chown mysql:mysql /var/run/mysqld
Open a new terminal window and log into mysql service + select mysql database:
$ mysql -u root
mysql> use mysql;
Set new password for root user:
mysql> update user set authentication_string=password('new-password') where user='root';
Flush privileges and exit mysql:
mysql> FLUSH PRIVILEGES;
mysql> exit;
Stop the safemode mysql (from the second terminal, you will see it stop in the first terminal)
$ mysqladmin -u root -p shutdown
Restart mysql
$ sudo service mysql start
You should be able to use the root user with password now to login to mysql/phpmyadmin
CentOS/Redhat:
From what I read in docs, when you run mysql_secure_installation, a temporary root password is generated and is stored in some log file.
sudo grep 'temporary password' /var/log/mysqld.log
Debian/Ubuntu:
During the packages installation, you get a prompt asking for the root password. If you don’t set it up, MySQL’s root user is created without a password. We can read the following line in package installation output:
Shell
2016-05-16T07:27:21.532619Z 1 [Warning] root#localhost is created with
an empty password ! Please consider switching off the
--initialize-insecure option.
but it is configured with the auth_socket plugin. You will only be able to connect using the UNIX socket, therefore any attempt to connect using your local IP or the network fails. Later on, you can change the password to allow connections from the network (as explained in this blog post).
Source
All we can do now is to see the root password. Lets change the root password since you cannot understand hashed password even if we can see it:
sudo service mysql stop
sudo mysqld_safe --skip-grant-tables --skip-syslog --skip-networking
then run mysql in a new terminal
mysql -u root
and run the following query, after changing the password
UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root';
FLUSH PRIVILEGES;
quit the mysql safe mode and start mysql service by
mysqladmin shutdown
sudo service mysql start
just run this script by root , you need custormize you password
mysqlpassword=password
/usr/bin/mysqladmin -u root password "$mysqlpassword"
#configure mysql login privileges
echo "grant all privileges on *.* to root#\"localhost\" identified by \"$mysqlpassword\";show databases;" |mysql -u root -p$mysqlpassword

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.

Setting the MySQL root user password on OS X

I just installed MySQL on Mac OS X. The next step was setting the root user password, so I did this next:
Launch the terminal app to access the Unix command line.
Under the Unix prompt I executed these commands:
cd /usr/local/mysql/bin
./mysqladmin -u root password 'password'
But, when I execute the command
./mysql -u root, this is the answer:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 224
Server version: 5.5.13 MySQL Community Server (GPL)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
I can get into the mysql command line without any password!
Why is this?
Try the command FLUSH PRIVILEGES when you log into the MySQL terminal. If that doesn't work, try the following set of commands while in the MySQL terminal
mysql -u root
mysql> USE mysql;
mysql> UPDATE user SET password=PASSWORD("NEWPASSWORD") WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> quit
Change out NEWPASSWORD with whatever password you want. Should be all set!
Update: As of MySQL 5.7, the password field has been renamed authentication_string. When changing the password, use the following query to change the password. All other commands remain the same:
mysql> UPDATE user SET authentication_string=PASSWORD("NEWPASSWORD") WHERE User='root';
for MySQL 8.0+ Don't use
mysql> UPDATE mysql.user SET authentication_string='password' WHERE User='root';
as it overwrites the authentication_string, which is supposed to be a hash and not plain text, instead use:
mysql> `ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';`
If you don't remember the password you set for root and need to reset it, follow these steps:
Stop the mysqld server, this varies per install
Run the server in safe mode with privilege bypass
sudo mysqld_safe --skip-grant-tables;
In a new window connect to the database, set a new password and flush the permissions & quit:
mysql -u root
For MySQL older than MySQL 5.7 use:
UPDATE mysql.user SET Password=PASSWORD('your-password') WHERE User='root';
For MySQL 5.7+ use:
USE mysql;
UPDATE mysql.user SET authentication_string=PASSWORD("your-password") WHERE User='root';
Refresh and quit:
FLUSH PRIVILEGES;
\q
Stop the safe mode server and start your regular server back. The new password should work now. It worked like a charm for me :)
Note
Run
UPDATE mysql.user SET authentication_string=null WHERE User='root';
if you don't want to set a password for root user. Or if PASSWORD() function doesn't work for you.
Once you've installed MySQL, you'll need to establish the "root" password. If you don't establish a root password, then, well, there is no root password, and you don't need a password to log in.
So, that being said, you need to establish a root password.
Using terminal enter the following:
Installation: Set root user password:
/usr/local/mysql/bin/mysqladmin -u root password NEW_PASSWORD_HERE
If you've made a mistake, or need to change the root password use the following:
Change root password:
cd /usr/local/mysql/bin/
./mysql -u root -p
> Enter password: [type old password invisibly]
use mysql;
update user set password=PASSWORD("NEW_PASSWORD_HERE") where User='root';
flush privileges;
quit
The instructions provided in the mysql website is so clear, than the above mentioned
$ sudo /usr/local/mysql/support-files/mysql.server stop
$ sudo /usr/local/mysql/support-files/mysql.server start --skip-grant-tables
/usr/local/mysql/bin/mysql
mysql> FLUSH PRIVILEGES;
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
mysql> exit or Ctrl + z
$ sudo /usr/local/mysql/support-files/mysql.server stop
$ sudo /usr/local/mysql/support-files/mysql.server start
/usr/local/mysql/support-files/mysql -u root -p
Enter the new password i.e MyNewPass
Reference: http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
Stop the mysqld server.
Mac OS X: System Preferences → MySQL → Stop MySQL Server
Linux (From Terminal): sudo systemctl stop mysqld.service
Start the server in safe mode with privilege bypass
From Terminal: sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
In a new terminal window:
sudo /usr/local/mysql/bin/mysql -u root
This will open the MySQL command-line client. From here enter:
UPDATE mysql.user SET authentication_string=PASSWORD('NewPassword') WHERE User='root';
FLUSH PRIVILEGES;
quit
Stop the mysqld server again and restart it in normal mode.
Mac OS X (From Terminal): sudo /usr/local/mysql/support-files/mysql.server restart
Linux Terminal: sudo systemctl restart mysqld
For the new MySQL 5.7, for some reason the binary commands of MySQL aren't attached to the shell, and you have to do:
Restart the Mac after the installation.
Start MySQL:
System Preferences → MySQL → Start button
Go to MySQL install folder in the terminal:
cd /usr/local/mysql/bin/
Access to MySQL:
./mysql -u root -p
And enter the initial password given to the installation.
In the MySQL client, change the password:
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPassword';
In the terminal, write mysql -u root -p and hit Return.
Enter the current MySQL password that you must have noted down.
And set the password:
SET PASSWORD = PASSWORD('new_password');
Please refer to this documentation here for more details.
If you have forgot the MySQL root password, can’t remember or want to break in….. you can reset the MySQL database password from the command line in either Linux or OS X as long as you know the root user password of the box you are on:
(1) Stop MySQL
sudo /usr/local/mysql/support-files/mysql.server stop
(2) Start it in safe mode:
sudo mysqld_safe --skip-grant-tables
(3) This will be an ongoing command until the process is finished so open another shell/terminal window, log in without a password:
mysql -u root
UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root';
In the UPDATE command above just replace the 'password' with your own new password, make sure to keep the quotation marks
(4) Save and quite
FLUSH PRIVILEGES;
\q
(5) Start MySQL
sudo /usr/local/mysql/support-files/mysql.server start
I solved this by:
Shutting down my MySQL server: mysql.server stop
Running MySQL in safe mode: mysqld_safe --skip-grant-tables
In another terminal, login with mysql -u root
In the same terminal, run UPDATE mysql.user SET authentication_string=null WHERE User='root';, then FLUSH PRIVILEGES; and then exit with exit;
Stop the safe mode server with mysql.server stop and then start the normal one; mysql.server start
Now you can set your new password with
ALTER USER 'root'#'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
None of the previous comments solved the issue on my Mac.
I used the commands below and it worked.
brew services stop mysql
pkill mysqld
rm -rf /usr/local/var/mysql/ # NOTE: this will delete your existing database!!!
brew postinstall mysql
brew services restart mysql
mysql -u root
When I installed OS X v10.10 (Yosemite), I got a problem with MySQL. I tried lot of methods, but none worked. I actually found a quite easy way. Try this out.
First log in to a terminal from super user (su) privileges.
sudo su
Stop MySQL
sudo /usr/local/mysql/support-files/mysql.server stop
Start in safe mode:
sudo mysqld_safe --skip-grant-tables
Open another terminal, log in as su privileges, and then, log in to the MySQL client (mysql) without a password
mysql -u root
Change the password
UPDATE mysql.user SET Password=PASSWORD('new_password') WHERE User='root';
Flush privileges
FLUSH PRIVILEGES;
You are done now.
The methods mentioned in existing answers don't work for MySQL 5.7.6 or later. According the MySQL documentation, this is the recommended way.
B.5.3.2.3 Resetting the Root Password: Generic Instructions
MySQL 5.7.6 and later:
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
Reference: https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
If you can't remember your password, #radtek's answer worked for me except in my case I had set up MySQL using brew which meant that steps 1 and 2 of his answer had to be changed to:
/usr/local/bin/mysql.server stop
/usr/local/bin/mysqld_safe --skip-grant-tables
Note: the lack of sudo.
I think this should work:
ALTER USER 'root'#'localhost' IDENTIFIED BY 'YOURNEWPASSWORD'
(Note that you should probably replace root with your username if it isn't root.)
This is what exactly worked for me:
Make sure no other MySQL process is running. To check this do the
following:
From the terminal, run this command:
lsof -i:3306
If any PID is returned, kill it using kill -9 PID
Go to System Preferences → MySQL → check if any MySQL instances are running, stop them.
Start MySQL with the command:
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
The password for every user is stored in the mysql.user table under columns User and authentication_string respectively. We can update the table as:
UPDATE mysql.user SET authentication_string='your_password' where User='root'
Stopping MySQL Server
sudo /usr/local/mysql/support-files/mysql.server stop
Starting MySQL in safe mode
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables &
Changing the root password
/usr/local/mysql/bin/mysql -u root
use mysql;
UPDATE user SET authentication_string=PASSWORD('NEW_PASSWORD') WHERE user='root';
FLUSH PRIVILEGES;
exit
Testing
Run /usr/local/mysql/bin/mysql -u root
Now enter the new password to start using MySQL.
To reference MySQL 8.0.15 + , the password() function is not available. Use the command below.
Kindly use
UPDATE mysql.user SET authentication_string='password' WHERE User='root';
You can manually turn-off MySQL on Mac, by clicking on  Apple menu and open System Preferences. Choose the “MySQL” preference panel, and then click on the “Stop MySQL Server” button to stop MySQL Server on Mac.
After you stop your MySQL, you'll need to follow these steps.
You'll need to start MySQL in skip-grant-tables mode
sudo /usr/local/mysql/support-files/mysql.server start --skip-grant-tables
In your terminal itself, enter this command to flush existing privileges
/usr/local/mysql/bin/mysql mysql> FLUSH PRIVILEGES;
Now you need to alter the user password
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'newpassword';
mysql> exit
Then you can go to  Apple menu and open System Preferences. Choose the “MySQL” preference panel, then click on the “Stop MySQL Server” button to stop MySQL Server on Mac.
Finally you can again go to  Apple menu and open System Preferences. Choose the “MySQL” preference panel, then click on the “Start MySQL Server” button to start MySQL Server on Mac.
This workaround works on my laptop!
Mac with macOS v10.14.5 (Mojave).
MySQL 8.0.17 was installed with Homebrew.
I run the following command to locate the path of MySQL
brew info mysql
Once the path is known, I run this:
/usr/local/Cellar/mysql/8.0.17/bin/mysqld_safe --skip-grant-table
In another terminal I run:
mysql -u root
Inside that terminal, I changed the root password using:
update mysql.user set authentication_string='NewPassword' where user='root';
and to finish I run:
FLUSH PRIVILEGES;
And voilà, the password was reset.
References
Try this in a terminal:
/usr/local/bin/mysql_secure_installation
macOS v10.14 (Mojave) and later with 5.7.26 installed from the Mac OS X DMG installer.
When attempting to use the UPDATE command posted by other users, it results in the following error:
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
Copy the password that was presented to you by the installer, open a terminal, and do the following:
mysql -uroot -p
ALTER USER 'root'#'localhost' IDENTIFIED BY 'YOURPASSWORDHERE';
For MySQL 8
Shutdown MySQL server
Go to System Preferences -> MySQL
Click Stop MySQL Server button
Open two terminal [command-line] windows
In the first terminal window run the following:
mysqld_safe --skip-grant-tables
In the second terminal window do the following:
4.1. Login to MySQL
mysql -u root
4.2. Run the following in the MySQL prompt:
FLUSH PRIVILEGES;
ALTER USER 'root'#'localhost' IDENTIFIED BY 'NEWPASSWORD';
4.3. Exit MySQL
exit;
Go back to the first terminal window and shutdown mysqld_safe
5.1. Press CTRL + Z
5.2. Run the following command
mysqladmin -u root -p shutdown
5.3. Enter the new password you set in 4.2. when prompted.
Start MySQL Server [see 1.]
If you forgot your password or want to change it to your MySQL:
Start your terminal and enter:
sudo su
Enter the password for you system
Stop your MySQL server:
sudo /usr/local/mysql/support-files/mysql.server stop
Leave this window open, run second terminal window and enter here:
mysql -u root
And change your password for MySQL:
UPDATE mysql.user SET authentication_string=PASSWORD('new_password') WHERE User='root';
where "new_password" - your new password. You don't need old password for MySQL.
Flush, quit and check your new password:
FLUSH PRIVILEGES;
Close all windows and check your new password for MySQL.
Much has changed for MySQL 8. I've found the following modification of the MySQL 8.0 "How to Reset the Root Password" documentation works with Mac OS X.
Create a temporary file, $HOME/mysql.root.txt, with the SQL to update the root password:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY '<new-password>';
This uses mysql_native_password to avoid the Authentication plugin 'caching_sha2_password' cannot be loaded error, which I get if I omit the option.
Stop the server, start with an --init-file option to set the root password, and then restart the server:
mysql.server stop
mysql.server start --init-file=$HOME/mysql.root.txt
mysql.server stop
mysql.server start
mysqld_safe --skip-grant-tables
mysql -u root
UPDATE mysql.user SET authentication_string='yourpasswd' WHERE User='root';
FLUSH PRIVILEGES;
ALTER USER 'root'#'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
I somehow need to do this every time my MacBook restarts.
$ export PATH=$PATH:/usr/local/mysql/bin
now,to make this permanent:
$ echo 'export PATH=$PATH:/usr/local/mysql/bin' >> ~/.bash_profile
next, start mysql in safe mode:
$ sudo mysqld_safe --skip-grant-tables;
If this does not work, go to System Preferences and stop MySQL server.
next, On the **other** terminal, you may use the below:
$ mysql -u root
mysql> USE mysql;
mysql> UPDATE mysql.user SET authentication_string=null WHERE
User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;
$ mysql -u root
mysql> ALTER USER 'root'#'localhost' IDENTIFIED WITH
caching_sha2_password BY 'yourpassword';
$ mysql -u root -p
Enter password:
mysql> SELECT user();
next, start the mysql server in normal mode. and you're done with resetting your root password. this worked for mysql 8.0.17 ver. for me.
thanks to everyone on top, https://stackoverflow.com/questions/36099028/error-1064-42000-you-have-an-error-in-your-sql-syntax-want-to-configure-a-pa,
https://www.houseninetytwo.com/how-to-use-mysql-in-terminal-on-mac-os-high-sierra/#:~:text=You%20may%20have%20gotten%20something,%2Fmysql%2Fbin%2Fmysql.&text=It%20should%20execute%20the%20right,return%20your%20version%20of%20MySQL.
Read more here.
As of Dec 2022, the following works for MySQL 8.0.26 on macOS Big Sur 11.2.3 :
Go to system preferences > mysql > stop server
Open terminal and run: mysqld_safe --skip-grant-tables
Open a new terminal and run: mysql -u root
Run: ALTER USER 'root'#'localhost' IDENTIFIED BY 'ROOT';
ROOT will be your new password.
Run: FLUSH PRIVILEGES;
Run: exit
Go to system preferences > mysql > start server

How to set root password to null

How can I change the password for root user of MySQL to null -- meaning no password or '' -- from the MySQL command line client?
Worked for me and "5.7.11 MySQL Community Server":
use mysql;
update user set authentication_string=password(''), plugin='mysql_native_password' where user='root';
I had to change the 'plugin' field as well because it was set to 'auth_socket'.
After that I could connect as mysql -u root without a password.
If you want an empty password, you should set the password to null and not use the Password hash function, as such:
On the command line:
sudo service mysql stop
sudo mysqld_safe --skip-grant-tables --skip-networking &
mysql -uroot
In MySQL:
use mysql;
update user set password=null where User='root';
flush privileges;
quit;
connect to mysql as user root (use one of the two following methods)
login as root and start mysql using mysql -p, enter current root password
login as self and start mysql using mysql -u root -p, enter current root password
mysql> set password = password('');
Done! No root password.
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('');
This worked for me on Ubuntu 16.04 with v5.7.15 MySQL:
First, make sure you have mysql-client installed (sudo apt-get install mysql-client).
Open terminal and login:
mysql -uroot -p
(then type your password)
After that:
use mysql;
update user set authentication_string=password(''), plugin='mysql_native_password' where user='root';
(tnx #Stanislav Karakhanov)
And the very last important thing is to reset mysql service:
sudo service mysql restart
You should now be able to login (without passsword) also by using MySQL Workbench.
You can recover MySQL database server password with following five easy steps.
Step # 1: Stop the MySQL server process.
Step # 2: Start the MySQL (mysqld) server/daemon process with the --skip-grant-tables option so that it will not prompt for password.
Step # 3: Connect to mysql server as the root user.
Step # 4: Setup new mysql root account password i.e. reset mysql password.
Step # 5: Exit and restart the MySQL server.
Here are commands you need to type for each step (login as the root user):
Step # 1 : Stop mysql service
# /etc/init.d/mysql stop
Output:
Stopping MySQL database server: mysqld.
Step # 2: Start to MySQL server w/o password:
# mysqld_safe --skip-grant-tables &
Output:
[1] 5988
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6025]: started
Step # 3: Connect to mysql server using mysql client:
# mysql -u root
Output:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.15-Debian_1-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
Step # 4: Setup new MySQL root user password
mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit
Step # 5: Stop MySQL Server:
# /etc/init.d/mysql stop
Output:
Stopping MySQL database server: mysqld
STOPPING server from pid file /var/run/mysqld/mysqld.pid
mysqld_safe[6186]: ended
[1]+ Done mysqld_safe --skip-grant-tables
Step # 6: Start MySQL server and test it
# /etc/init.d/mysql start
# mysql
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
# mysql -u root -p
Source: http://www.cyberciti.biz/tips/recover-mysql-root-password.html
For MySQL 8.0 just:
SET PASSWORD FOR 'root'#'localhost' = '';
It's not a good idea to edit mysql database directly.
I prefer the following steps:
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY '';
mysql> flush privileges;
This is from MySQL 8.0.13:
use mysql;
update user set authentication_string=null where user='root';
quit;
I noticed a few of these solutions above are now deprecated.
To set an empty password simply follow these steps:
mysql -u root -p
use mysql
SET PASSWORD FOR 'root'#'localhost' = '';
\q (to quit)
now run: mysql -u root
You should be able to start mysql up without a password now.
It works for me.
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password'
The answer by user64141
use mysql;
update user set password=null where User='root';
flush privileges;
quit;
didn't work for me in MariaDB 10.1.5 (supposed to be a drop in replacement for MySQL). While didn't tested it in MySQL 5.6 to see if is an upstream change, the error I got was:
ERROR 1048 (23000): Column 'Password' cannot be null
But replacing the null with empty single or double quotes worked fine.
update user set password='' where User='root';
or
update user set password="" where User='root';
I am using nodejs and windows 10. A combination of two answers worked for me.
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY '';
mysql> flush privileges;
followed by:
restart;
Hope this helps for others who still have an issue with this.
If you know your Root Password and just wish to reset it then do as below:
Start MySQL Service from control panel > Administrative Tools > Services. (only if it was stopped by you earlier ! Otherwise, just skip this step)
Start MySQL Workbench
Type in this command/SQL line
ALTER USER 'root'#'localhost' PASSWORD EXPIRE;
To reset any other user password... just type other user name instead of root.
For connect to mysql without password:
mysql -p
SET PASSWORD = ""
https://dev.mysql.com/doc/refman/5.7/en/set-password.html
The syntax is slightly different depending on version. From the docs here:
https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
MySQL 5.7.6 and later:
ALTER USER 'root'#'localhost' IDENTIFIED BY '';
MySQL 5.7.5 and earlier:
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('');
My variant for MySQL 5.7:
Stop service mysql:
$ sudo service mysql stop
Running in Safe Mode:
$ sudo mysqld_safe --skip-grant-tables --skip-networking
(above line is the whole command)
Open a new terminal window:
$ mysql -u root
$ mysql use mysql;
$ mysql update user set authentication_string=password('password') where user='root';
$ mysql update user set plugin="mysql_native_password" where User='root';
$ mysql flush privileges;
$ mysql quit;
Run the mysql service:
$ sudo service mysql start
Wanted to put my own 2cents in here bcuz the above answers did not work for me.
On centos 7, mysql community v8, shell is bash.
The correct commands would be as follows:
# start mysql without password checking
systemctl stop mysqld 2>/dev/null
systemctl set-environment MYSQLD_OPTS="--skip-grant-tables" &&
systemctl start mysqld
# set default password to nothing
mysql -u root mysql <<- 'EOF'
FLUSH PRIVILEGES;
UNINSTALL COMPONENT 'file://component_validate_password';
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY '';
FLUSH PRIVILEGES;
INSTALL COMPONENT 'file://component_validate_password';
EOF
# restart mysql normally
systemctl restart mysqld
then you can login without password:
mysql -u root
its all because you installed greater then 5.6 version of the mysql
Solutions
1.you can degrade mysql version solution
2 reconfigure authentication to native type or legacy type authentication using
configure option
On ubuntu 19.10, mysql 8, this is what worked for me:
$ sudo mysqld --skip-grant-tables &
$ mysql
> use mysql
> alter user set authentication_string='', plugin='mysql_native_password' where user = 'root';
> quit
$ sudo mysqladmin shutdown
$ sudo systemctl start mysql
If you get errors trying to run mysqld_safe, in particular: /var/run/mysqld for UNIX socket file don't exists, you can try creating the dir and running mysqld_safe again.
$ sudo mkdir /var/run/mysqld
$ sudo chown mysql /var/run/mysqld
$ sudo chgrp mysql /var/run/mysqld
After searching for hours i found it . just Change the password to something contains Upper case numeric and special characters in it.