Related
How do I change the MySQL root password and username in ubuntu server? Do I need to stop the mysql service before setting any changes?
I have a phpmyadmin setup as well, will phpmyadmin get updated automatically?
Set / change / reset the MySQL root password on Ubuntu Linux. Enter the following lines in your terminal.
Stop the MySQL Server: sudo /etc/init.d/mysql stop
(In some cases, if /var/run/mysqld doesn't exist, you have to create it at first: sudo mkdir -v /var/run/mysqld && sudo chown mysql /var/run/mysqld
Start the mysqld configuration: sudo mysqld --skip-grant-tables &
Login to MySQL as root: mysql -u root mysql
Replace YOURNEWPASSWORD with your new password:
For MySQL < 8.0
UPDATE mysql.user SET Password = PASSWORD('YOURNEWPASSWORD') WHERE User = 'root';
FLUSH PRIVILEGES;
If your MySQL uses new auth plugin, you will need to use: update user set plugin="mysql_native_password" where User='root'; before flushing privileges.
Note: on some versions, if password column doesn't exist, you may want to try:
UPDATE user SET authentication_string=password('YOURNEWPASSWORD') WHERE user='root';
Note: This method is not regarded as the most secure way of resetting the password, however, it works.
For MySQL >= 8.0
FLUSH PRIVILEGES;
ALTER USER 'root'#'localhost' IDENTIFIED BY 'YOURNEWPASSWORD';
FLUSH PRIVILEGES;
Last step:
As noted in comments by #lambart, you might need to kill the temporary password-less mysql process that you started, i.e. sudo killall -9 mysqld and then start normal daemon: sudo service mysql start
References:
Set / Change / Reset the MySQL root password on Ubuntu Linux
How to Reset the Root Password (v5.6)
How to Reset the Root Password (v8.0)
The only method that worked for me is the one described here (I am running ubuntu 14.04). For the sake of clarity, these are the steps I followed:
sudo vim /etc/mysql/my.cnf
Add the following lines at the end:
[mysqld]
skip-grant-tables
sudo service mysql restart
mysql -u root
use mysql
select * from mysql.user where user = 'root'; - Look at the top to determine whether the password column is called
password or authentication_string
UPDATE mysql.user set *password_field from above* = PASSWORD('your_new_password') where user = 'root' and host = 'localhost'; - Use the proper password column from above
FLUSH PRIVILEGES;
exit
sudo vim /etc/mysql/my.cnf
Remove the lines added in step 2 if you want to keep your security standards.
sudo service mysql restart
For reference : https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
The official and easy way to reset the root password on an ubuntu server...
If you are on 16.04, 14.04, 12.04:
sudo dpkg-reconfigure mysql-server-5.5
If you are on 10.04:
sudo dpkg-reconfigure mysql-server-5.1
If you are not sure which mysql-server version is installed you can try:
dpkg --get-selections | grep mysql-server
Updated notes for mysql-server-5.7
Note that if you are using mysql-server-5.7 you can not use the easier dpkg-reconfigure method shown above.
If you know the password, login and run this:
UPDATE mysql.user SET authentication_string=PASSWORD('my-new-password') WHERE USER='root';
FLUSH PRIVILEGES;
Alternatively, you can use the following:
sudo mysql_secure_installation
This will ask you a series of questions about securing your installation (highly recommended), including if you want to provide a new root password.
If you do NOT know the root password, refer to this Ubuntu-centric write up on the process.
See for more info:
https://help.ubuntu.com/16.04/serverguide/mysql.html
https://help.ubuntu.com/14.04/serverguide/mysql.html
What worked for me (Ubuntu 16.04, mysql 5.7):
Stop MySQL
sudo service mysql stop
Make MySQL service directory.
sudo mkdir /var/run/mysqld
Give MySQL user permission to write to the service directory.
sudo chown mysql: /var/run/mysqld
Start MySQL manually, without permission checks or networking.
sudo mysqld_safe --skip-grant-tables --skip-networking &
On another console, log in without a password.
mysql -uroot mysql
Then:
UPDATE mysql.user SET authentication_string=PASSWORD('YOURNEWPASSWORD'), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';
EXIT;
Turn off MySQL.
sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown
Start the MySQL service normally.
sudo service mysql start
I am sharing the step by step final solution to reset a MySQL password in Linux
Ubuntu.
Reference taken from blog (dbrnd.com)
Step 1:
Stop MySQL Service.
sudo stop mysql
Step 2:
Kill all running mysqld.
sudo killall -9 mysqld
Step 3:
Starting mysqld in Safe mode.
sudo mysqld_safe --skip-grant-tables --skip-networking &
Step 4:
Start mysql client
mysql -u root
Step 5:
After successful login, please execute this command to change any password.
FLUSH PRIVILEGES;
Step 6:
You can update mysql root password .
UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';
Note: On MySQL 5.7, column Password is called authentication_string.
Step 7:
Please execute this command.
FLUSH PRIVILEGES;
At first run this command:
sudo mysql
and then you should check which authentication method of your MySQL user accounts use.So run this command
SELECT user,authentication_string,plugin,host FROM mysql.user;
now you can see something like this already :
+------------------+-------------------------------------------+-----------------------+-----------+
| 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 |
+------------------+-------------------------------------------+-----------------------+-----------+
in the table that is in the above , you can see that all of your mysql users accounts status & if you have set a password for root account before you see mysql_native_password in plugin column instead auth_socket.
All in all for change your root password you should run :
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Be sure to change password to a strong password of your choosing.
Then for reload your server to put your new changes into effect run this;
FLUSH PRIVILEGES;
So again check the authentication methods which has employed by your mysql , by this command:
SELECT user,authentication_string,plugin,host FROM mysql.user;
and now the output is :
+------------------+-------------------------------------------+-----------------------+-----------+
| 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 |
+------------------+-------------------------------------------+-----------------------+-----------+
as you can see in the grant table your root account has mysql_native_password
.
now you can exit MYSQL shell
exit;
That's it.just you should restart mysql by sudo service mysql restart.
Now you can login to mysql as a root account with your password easily.
I faced problems with ubuntu 18.04 and mysql 5.7, this is the solution
Try restart mysql-server before execution the comands
sudo service mysql restart
MYSQL-SERVER >= 5.7
sudo mysql -uroot -p
USE mysql;
UPDATE user SET authentication_string=PASSWORD('YOUR_PASSWORD') WHERE User='root';
UPDATE user SET plugin="mysql_native_password";
FLUSH PRIVILEGES;
quit;
MYSQL-SERVER < 5.7
sudo mysql -uroot -p
USE mysql;
UPDATE user SET password=PASSWORD('YOUR_PASSWORD') WHERE User='root';
UPDATE user SET plugin="mysql_native_password";
FLUSH PRIVILEGES;
quit;
Change the MySQL root password.
This method exposes the password to the command-line history, these commands should be run as root.
Login through mysql command line tool:
mysql -uroot -poldpassword
Run this command:
SET PASSWORD FOR root#'localhost' = PASSWORD('newpassword');
or
Run this command, which sets a password for the current user ('root' for this case) :
SET PASSWORD = PASSWORD('newpassword');
Stop MySQL
sudo service mysql stop
Make MySQL service directory.
sudo mkdir /var/run/mysqld
Give MySQL user permission to write to the service directory.
sudo chown mysql: /var/run/mysqld
Start MySQL manually, without permission checks or networking.
sudo mysqld_safe --skip-grant-tables --skip-networking &
5.Log in without a password.
mysql -uroot mysql
6.Update the password for the root user.
UPDATE mysql.user SET authentication_string=PASSWORD('YOURNEWPASSWORD'), plugin='mysql_native_password' WHERE User='root' AND Host='%';
EXIT;
Turn off MySQL.
sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown
Start the MySQL service normally.
sudo service mysql start
If you would like to change the MySQL root password, in a terminal enter:
sudo dpkg-reconfigure mysql-server-5.5
The MySQL daemon will be stopped, and you will be prompted to enter a new password.
This works like charm I did it for Ubuntu 16.04.
Full credit to below link as I got it from there.
[https://coderwall.com/p/j9btlg/reset-the-mysql-5-7-root-password-in-ubuntu-16-04-lts][1]
Stop MySQL
sudo service mysql stop
Make MySQL service directory.
sudo mkdir /var/run/mysqld
Give MySQL user permission to write to the service directory.
sudo chown mysql: /var/run/mysqld
Start MySQL manually, without permission checks or networking.
sudo mysqld_safe --skip-grant-tables --skip-networking &
Log in without a password.
mysql -uroot mysql
Update the password for the root user.
make sure at atleast root account gets updated by the below query.
make some selection and check the existing values if you like
UPDATE mysql.user SET
authentication_string=PASSWORD('YOURNEWPASSWORD'),
plugin='mysql_native_password' WHERE User='root';
EXIT;
Turn off MySQL.
sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown
Start the MySQL service normally.
sudo service mysql start
In my case this option helped : https://stackoverflow.com/a/49610152/13760371
Thank you, Rahul.
except for the following moment, when I try entered command:
UPDATE mysql.user SET authentication_string=PASSWORD('YOURNEWPASSWORD'), plugin='mysql_native_password' WHERE User='root' AND Host='%';
the console issued a warning:
1681 'password' is deprecated and will be removed in a future release
cured with this command:
UPDATE mysql.user SET authentication_string=CONCAT('*', UPPER(SHA1(UNHEX(SHA1('NEWPASSWORD'))))), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';
MySQL version 5.7.X
My variant:
1. > sudo service mysql stop
2. > sudo mkdir /var/run/mysqld
3. > sudo chown mysql: /var/run/mysqld
4. > sudo mysqld_safe --skip-grant-tables --skip-networking &
5. > mysql -uroot mysql
6. > UPDATE mysql.user SET authentication_string=CONCAT('*', UPPER(SHA1(UNHEX(SHA1('NEWPASSWORD'))))), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';
7. > \q;
8. > sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown
9. > sudo service mysql start
This solution belongs to the previous version of MySQL.
By logging in to MySQL using socket authentication, you can do it.
sudo mysql -u root
Then the following command could be run.
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Details are available here .
This is the solution for me. I work at Ubuntu 18.04:
https://stackoverflow.com/a/46076838/2400373
But is important this change in the last step:
UPDATE mysql.user SET authentication_string=PASSWORD('YOURNEWPASSWORD'), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';
For Ubuntu 18.04 and mysql version 14.14 Distrib 5.7.22 follow the below step to reset the mysql password.
Step 1
sudo systemctl stop mysql
Step 2
sudo systemctl edit mysql
This command will open a new file in the nano editor, which you'll use to edit MySQL's service overrides. These change the default service parameters for MySQL. This file will be empty, so add the following content:
[Service]
ExecStart=
ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid --skip-grant-tables --skip-networking
Step 3
sudo systemctl daemon-reload
sudo systemctl start mysql
Step 4
sudo mysql -u root
Step 5
FLUSH PRIVILEGES;
Step 6
UPDATE mysql.user SET authentication_string = PASSWORD('new_password') WHEREuser = 'root';
Step 7
UPDATE mysql.user SET plugin ='mysql_native_password' WHERE user = 'root';
Step 8
sudo systemctl revert mysql
and finally
sudo systemctl restart mysql
Now enjoy
Echoing rogerdpack's comment: if you don't know the MySQL root password and you don't care about MySQL data/settings, you can reinstall it and reset the root's password as follows:
sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo rm -rf /var/lib/mysql
sudo apt-get install -y mysql-server mysql-client
During the installation, you can choose the root's password:
If you know your current password, you don't have to stop mysql server.
Open the ubuntu terminal.
Login to mysql using:
mysql - username -p
Then type your password.
This will take you into the mysql console.
Inside the console, type:
> ALTER USER 'root'#'localhost' IDENTIFIED BY 'new_password';
Then flush privileges using:
> flush privileges;
Then you are all done.
You don't need all this. Simply log in:
mysql -u root -p
Then change the current user's password as the mysql> prompt:
mysql> set password=password('the_new_password');
mysql> flush privileges;
Most of the answers on this topic are outdated; two major changes have occurred in MySQL up until the writing of this answer:
1- the 'Password' field in the user table has been replaced by 'authentication_string' column.
2- the 'Password' encryption function : PASSWORD("of some text") is deprecated.
Please refer to this link for further information:dev.mysql.com/doc/refman/8.0/en/resetting-permissions.html
1.Open nano / vim to create a file with the following content and Save file as ~/mysql-pwd
ALTER USER 'root'#'localhost' IDENTIFIED BY 'NEWPASSWORD';
Stop mysql sudo systemctl stop mysql
Run sudo mysqld -init-file=~/mysql-pwd
Restart mysql sudo systemctl start mysql
Now login mysql -u root -p. password will be your NEWPASSWORD
When you use MySQL's PASSWORD() on the system where you want to change the password, it can cause the password turn up in a MySQL log in cleartext [source]. Keeping them, their backups etc. as secure as the password sounds like nightmare to me, so I rather like to do it as follows:
On your local machine, run this with your password:
mysql -u someuser -p < <(echo "SELECT PASSWORD('mypass');")
Note the space in front to prevent it from turning up in the bash history (for other distros than Ubuntu, this might work differently – source).
On your server machine, execute the following command to change its MySQL root password (replace myhash with your password's hash as printed by the first command):
mysql -u root -p < <(echo "SET PASSWORD FOR root#localhost = 'myhash';")
Optionally, let's be a bit paranoid: On your local machine, clear your terminal screen with clear and purge your virtual terminal scrollback, to hide the cleartext password appearing in the command above.
To update the "root" Mysql user password you must have in mind that you will need of super user permissions for that. If you have super user privilegies, try the following commands:
MySQL 5.7.6 and later
sudo su
service mysql stop
mysql -u root
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
\q;
exit
mysql -u root -p MyNewPass
MySQL 5.7.5 and earlier
sudo su
service mysql stop
mysql -u root
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('MyNewPass');
\q;
exit
mysql -u root -p MyNewPass
As mysql documentation on the password() function says:
This function was removed in MySQL 8.0.11.
This invalidates pretty much all existing answers for mysql v8.0.11 and newer.
Per mysql documentation the new generic way to reset the root password is as follows:
The preceding sections provide password-resetting instructions
specifically for Windows and Unix and Unix-like systems.
Alternatively, on any platform, you can reset the password using the
mysql client (but this approach is less secure):
Stop the MySQL server if necessary, then restart it with the
--skip-grant-tables option. This enables anyone to connect without a password and with all privileges, and disables account-management
statements such as ALTER USER and SET PASSWORD. Because this is
insecure, if the server is started with the --skip-grant-tables
option, it enables --skip-networking automatically to prevent remote
connections.
Connect to the MySQL server using the mysql client; no password is
necessary because the server was started with --skip-grant-tables:
shell> mysql
In the mysql client, tell the server to reload the grant tables so that account-management statements work:
mysql> FLUSH PRIVILEGES;
Then change the 'root'#'localhost' account password. Replace the password with the password that you want to use.
To change the password for a root account with a different host name
part, modify the instructions to use that host name.
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
You should now be able to connect to the MySQL server as root using the
new password. Stop the server and restart it normally (without the
--skip-grant-tables and --skip-networking options).
You can use this command:
UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';
after that please use flush:
FLUSH PRIVILEGES;
If you know the 'root' users password, log in to mysql with that credentials. Then execute the following query to update the password.
ALTER USER 'root'#'localhost' IDENTIFIED BY 'new_passowrd';
The steps below worked for me. I'm using MySQL 8.* on Ubuntu
Stop MySQL service and check status to confirm the service stopped
sudo systemctl stop mysql
sudo systemctl status mysql
Edit the systemd config file so you can access MySQL without permission check
sudo systemctl edit mysql
Copy and paste the following 3 lines
[Service]
ExecStart=
ExecStart=/usr/sbin/mysqld --skip-grant-tables --skip-networking
After pasting the lines CTRL+0 to save and then CTRL+X to exit
Reload mysql service and start it (starts the service with --skip-grant-table)
sudo systemctl daemon-reload
sudo systemctl start mysql
5.Now connect to MySQL server without password
sudo mysql -u root
6.Load the grant tables by running
mysql> FLUSH PRIVILEGES;
Reset the root password
mysql> ALTER USER 'root'#'localhost' IDENTIFIED WITH
mysql_native_password BY 'YourPasswordHere';
Close the mysql connection
mysql> exit
Revert the modification done on the mysql systemd file
sudo systemctl revert mysql
Reload the mysql daemon for changes to take place.
sudo systemctl daemon-reload
Lastly restart the MySQL service
sudo systemctl restart mysql
Now connect to mysql with the new password set in step 7
You can visit this link reset root password for mysql 8 for more details.
Instead of resetting the password there is a work around on the local machine if you have setup phpmyadmin to connect without giving the password or username. Check this out by starting mysql, apache etc. I have xampp installed in my local machine. So starting the xampp will start all the necessary services. Now going to http://localhost/phpmyadmin shows me all the databases. This confirms that you have saved the username and passsword in the config file of phpmyadmin which can be found in the phpmyadmin install location. If you have xampp installed the phpmyadmin folder can be found in the root folder of xampp installation. Search for the word password in the config.inc.php file. There you will find the password and username.
You can easily change the mysql password if deployed on xampp through provided phpadmin gui.
phpMyAdmin -> User Accounts -> Edit Privileges (Select the intended user) -> Change Password (Tab)
for mysql 5.6 this command works and you can set password through the wizard:
sudo dpkg-reconfigure mysql-server-5.6
I had to go this route on Ubuntu 16.04.1 LTS. It is somewhat of a mix of some of the other answers above - but none of them helped. I spent an hour or more trying all other suggestions from MySql website to everything on SO, I finally got it working with:
Note: while it showed Enter password for user root, I didnt have the original password so I just entered the same password to be used as the new password.
Note: there was no /var/log/mysqld.log only /var/log/mysql/error.log
Also note this did not work for me:
sudo dpkg-reconfigure mysql-server-5.7
Nor did:
sudo dpkg-reconfigure --force mysql-server-5.5
Make MySQL service directory.
sudo mkdir /var/run/mysqld
Give MySQL user permission to write to the service directory.
sudo chown mysql: /var/run/mysqld
Then:
kill the current mysqld pid
run mysqld with sudo /usr/sbin/mysqld &
run /usr/bin/mysql_secure_installation
Output from mysql_secure_installation
root#myServer:~# /usr/bin/mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: no
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y
New password:
Re-enter new password:
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
Dropping test database...
Success.
Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
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'] = '';
For some reason MySQL stopped giving access for root. Uninstalled and reinstalled with Homebrew. Fresh install, fresh tables but when I enter
mysql -u root -p
I get this error:
Access denied for user 'root'#'localhost' (using password: NO)
I reinstalled MySQL five times but it is still asking for a password. How do I fix this?
None of these worked for me. I think i already had mysql somewhere on my computer so a password was set there or something. After spending hours trying every solution out there this is what worked for me:
$ 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 -uroot
all credit to #Ghrua
Just run this command (where NEWPASS is your password):
$(brew --prefix mysql)/bin/mysqladmin -u root password NEWPASS
I have had the same error and fixed it this way.
In case you have inadvertently set and forgot the root password, and you don't want to wipe all your databases and start over because you are lazy and forgot to have a back up solution in place, and you are using a fairly recent Homebrew install (Winter 2013), here are steps to reset your password for MySQL.
Stop the currently running MySQL instance
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Now start mysql by hand skipping the grant tables and networking
$(brew --prefix mysql)/bin/mysqld_safe --skip-grant-tables --skip-networking
Note that if when you run echo $(brew --prefix mysql) and it does not respond as "/usr/local/opt/mysql" in bash, you will need to adjust the path accordingly.
Once you have done this, you now should have a running, unprotected MySQL instance up.
Log in and set the password
mysql -u root
At the prompt, enter the following MySQL command to set a new password for the effected user.
mysql> update mysql.user set password=PASSWORD('new_password_here') WHERE user='root';
If all went to plan it should say:
Query OK, 1 row affected (0.02 sec)
Rows matched: 4 Changed: 1 Warnings: 0
Exit out of the MySQL prompt.
mysql> exit
Bye
Stop server:
mysqladmin -u root shutdown
Now, lets put back the launch daemon so we have our MySQL at the ready again:
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Congratulations. You've just reset your mysql root password. Pour yourself a coffee and get a backup solution in place!
I had the same problem a couple days ago. It happens when you install mysql via homebrew and run the initialization script (mysql_install_db) before starting the mysql daemon.
To fix it, you can delete mysql data files, restart the service and then run the initialization script:
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
rm -r /usr/local/var/mysql/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
Got this error after installing mysql via home brew.
So first remove the installation. Then Reinstall via Homebrew
brew update
brew doctor
brew install mysql
Then restart mysql service
mysql.server restart
Then run this command to set your new root password.
mysql_secure_installation
Finally it will ask to reload the privileges. Say yes. Then login to mysql again. And use the new password you have set.
mysql -u root -p
If you run on Mojave, Catalina, Big Sur and now on macOS Monterey:
brew install mariadb
...
brew services start mariadb
==> Successfully started `mariadb` (label: homebrew.mxcl.mariadb)
$(brew --prefix mariadb)/bin/mysqladmin -u root password newpass
/usr/local/opt/mariadb/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'#'localhost''
also login with root account fails:
mariadb -u root
ERROR 1698 (28000): Access denied for user 'root'#'localhost'
then default admin user is created same name as your MacOS account username, e.g. johnsmit.
To login as root and set root password, issue (use your username):
mariadb -u johnsmit
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 10.4.11-MariaDB Homebrew
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> ALTER USER 'root'#'localhost' IDENTIFIED BY 'ROOT-PASSWORD';
Query OK, 0 rows affected (0.006 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> exit
Bye
So you change root password form mysql on localhost.
Bonus: to change current or other user pass you can use mysqladmin command:
$(brew --prefix mariadb)/bin/mysqladmin -u arunas password 'newsecret'
but this does not affect localhost for some reason, but should work for app login.
Or use native MySQL change user password SQL, which explicitly specifies host, in my case 'localhost' account of the user:
mariadb -u arunas
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.5.9-MariaDB Homebrew
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> ALTER USER 'arunas'#'localhost' IDENTIFIED BY 'newsecret';
Query OK, 0 rows affected (0.006 sec)
MariaDB [(none)]> exit
Bye
Now let's try to login without password:
mariadb -u arunas
ERROR 1045 (28000): Access denied for user 'arunas'#'localhost' (using password: NO)
you see login failed, thus now we need specify the need of password:
mariadb -u arunas -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 10.5.9-MariaDB Homebrew
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> exit
Bye
Happy usage!
Since the question was asked/answered long time ago, those top answers do not work for me. Here's my solution, in 2020.
Background: Fresh mysql/mariadb installed by homebrew.
Problem: The password for root is not empty and unknown.
The fix:
mysql -u YOUR-SYSTEM-USERNAME -p
The password is empty (press enter)
ALTER USER 'root'#'localhost' IDENTIFIED BY 'NEW-ROOT-PASSWORD';
FLUSH PRIVILEGES;
The reason:
Homebrew will create a user with root privileges named by the current MacOS username.
it has no password
Since it has all privileges, just reset the root password with that user.
The initial password for root was randomly generated.
This worked for me:
sudo mysql -u root
If you are using macOS and install the MariaDB via Homebrew you can use the OS root password and then reset the password to whatever you want, in this case I removed the root's password:
sudo mysqladmin -u root password ''
or if you want to set a password you can put the password between the single quotations:
sudo mysqladmin -u root password 'NEW-PASSWORD-HERE'
Try with sudo to avoid the "Access denied" error:
sudo $(brew --prefix mariadb)/bin/mysqladmin -u root password NEWPASS
go to apple icon --> system preferences
open Mysql
in instances you will see "initialize Database"
click on that
you will be asked to set password for root --> set a strong password there
use that password to login in mysql from next time
Hope this helps.
I had this problem on a fresh install on Mac. I installed MariaDB with:
brew install mariadb
Then started the service:
brew services start mariadb
I was unable to run 'mysql_secure_installation' as it prompted for the root password. Then I noticed in the install output:
mysql_install_db --verbose --user=jonny --basedir=/usr/local/Cellar/ ....
So I tried logging in as the username specified in the mysql_install_db output and was successful e.g.
mysql -u jonny
Then at the mysql prompt if you want to set a password for the root user:
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('ToPsEcReT');
I've just noticed something common to most of the answers here, and confirmed on my fresh install. It's actually obvious if you look at the recommendations to run mysqladmin -u root without -p.
There is no password.
Brew sets mysql up with just a root user and no password at all. This makes sense, I guess, but the post-install Caveats don't mention it at all.
This worked for me for MAC https://flipdazed.github.io/blog/osx%20maintenance/set-up-mysql-osx
Start mysql by running
brew services start mysql
Run the installation script
mysql_secure_installation
You will be asked to set up a setup VALIDATE PASSWORD plugin. Enter y to do this.
Select the required password validation (Doesn’t really matter if it is just you using the database)
Now select y for all the remaining options: Remove anon. users; disallow remote root logins; remove test database; reload privileges tables.
Now you should receive a message of
All done!
This worked for me. Hopefully this works for you too!!!
Follow them.
brew services stop mysql
pkill mysqld
# NB: the following command will REMOVE all your databases!
# Make sure you have backups or SQL dumps if you have important data in them already.
rm -rf /usr/local/var/mysql/
brew services restart mysql
mysql -uroot
UPDATE mysql.user SET authentication_string=null WHERE User='root';
FLUSH PRIVILEGES;
exit;
mysql -u root
ALTER USER 'root'#'localhost' IDENTIFIED WITH caching_sha2_password
BY'YOUR_PASS_WORD!!!!';
Running these lines in the terminal did the trick for me and several others who had the same problem. These instructions are listed in the terminal after brew installs mysql sucessfully.
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/mysql/5.5.25a/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
/usr/local/Cellar/mysql/5.5.25a/bin/mysqladmin -u root password 'YOURPASSWORD'
where YOURPASSWORD is the password for root.
Check that you don't have a .my.cnf hiding in your homedir. That was my problem.
The default password when you install mysql via brew is root try this, it worked for me
mysql -uroot -proot
So, in case someone has the same situation and configuration as I had and is also about to go mad - this worked for me.
After a long story I had a brew-installed MariaDB which kept automatically restarting when I killed its process (this was brew's doing), which had a root password, which I did not know.
$ brew services list
This shows something like:
mariadb started jdoe /path/to/homebrew.mxcl.mariadb.plist
Stop the MySQL server with:
$ brew services stop mariadb
Then start it again without the root user (and not using brew):
$ mariadbd --skip-grant-tables &
Here, mysql_secure_installation did not work for me because of the --skip-grant-tables, and it would not work without the --skip-grant-tables because it needed the password (which I did not have).
Trying $(brew --prefix mysql)/bin/mysqladmin -u root password hunter2 only returned strange errors and did nothing; $(brew --prefix mariadb)/bin/mysqladmin -u root password hunter2 also didn't work, gave different errors, and suggestions that did not work for me.
But you can log into mysql now without credentials: $ mysql
Here, the old method of updating the user table for root doesn't work because "Column 'Password' is not updatable".
The new method uses alter user BUT only works after you have done flush privileges; so do that first.
Then:
MariaDB [(none)]> alter user 'root'#'localhost' identified by 'hunter2';
(MariaDB [(none)]> is the MySQL prompt here)
Then do flush privileges; again.
Exit the MySQL client.
Now as far as brew is concerned, MariaDB is still not running, and so use $ ps aux | grep -i mariadb to find the pid and $ kill -9 <pid> it.
Then use $ brew services start mariadb to start it again.
I stumbled across this too and the solution was unironically to simply run this:
mysql
Terminal 1:
$ mysql_safe
Terminal 2:
$ mysql -u root
mysql> UPDATE mysql.user SET Password=PASSWORD('new-password') WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> quit
Iam using Catalina and use this mysql_secure_installation command and now works for me:
$ mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.
Enter current password for root (enter for none): << enter root here >>
i enter root as current password
OK, successfully used password, moving on...
Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.
and do the rest
For me, mysql was setup with a root user and no password.
I wanted to be able to login as my current user and not require the -u root bit. I used the following command to setup a super user:
mysql -u root -e "CREATE USER '$USER'#'localhost';"
mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO '$USER'#'localhost';"
mysql -u root -e "flush privileges;"
Any value for $USER will work.
I personally concatenated all the above with a semicolon but reformatted to make it hopefully easier for all to read.
Followed the article from #Roman Escart.
I guess the key is to use '$brew link --force mysql#5.7'
https://medium.com/macoclock/setup-mysql-in-a-specific-version-on-macos-35d8ad89c699
login to DB as root :
sudo mysql -u root
you may run secure db as:
sudo mysql_secure_installation
Use init file to start mysql to change the root password.
brew services stop mysql
pkill mysqld
echo "ALTER USER 'root'#'localhost' IDENTIFIED BY 'newRootPass';" > /tmp/mysql-init
$(brew --prefix mysql)/bin/mysqld --init-file=/tmp/mysql-init
Your root password is now changed. Make sure to shutdown server properly to save password change. In new terminal window execute
mysqladmin -u root -p shutdown
and enter your new pass.
Start your service and remove the init file
brew services start mysql
rm /tmp/mysql-init
Tested on mysql version 8.0.19
What is the easiest way to run Mysql on a Mac ?
My solution was to install MAMP.
https://www.mamp.info/en/mac/
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 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.