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!
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..!
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 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'] = '';