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!
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!
when I installed mysql server into ubuntu machine using this command
sudo apt-get install mysql-client mysql-server
the command is executed successfully but during the installation mysql not asked me about password , so I can not login to mysql except as root user , in other words if I writing into command line this command
sudo mysql -uroot -p
I can login to mysql even if I changed password every time , server does not matter about password if I used sudo . so how can I solve this problem (set new password)
when I logged to mysql I tried to change password from mysql.user table using this query
mysql> UPDATE mysql.user SET Password= PASSWORD('rootroot') WHERE User = 'root';
but there is no Password column into mysql.user table
The reason is that recent Ubuntu installation (maybe others also), mysql is using by default the UNIX auth_socket plugin.
Basically means that: db_users using it, will be "auth" by the system user credentias. You can see if your root user is set up like this by doing the following:
$ sudo mysql -u root # I had to use "sudo" since is new installation
mysql> USE mysql;
mysql> SELECT User, Host, plugin FROM mysql.user;
+------------------+-----------------------+
| User | plugin |
+------------------+-----------------------+
| root | auth_socket |
| mysql.sys | mysql_native_password |
| debian-sys-maint | mysql_native_password |
+------------------+-----------------------+
As you can see in the query, the root user is using the auth_socket plugin
There are 2 ways to solve this:
You can set the root user to use the mysql_native_password plugin
You can create a new db_user with you system_user (recommended)
Option 1:
$ sudo mysql -u root # I had to use "sudo" since is new installation
mysql> USE mysql;
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;
$ service mysql restart
MySQL by default create a password for root#hostname
I tried finding the password from directories of MySQL, but was not successful.
So, I tried resetting password. I followed the following steps.
# 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 password for root**
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
Otherwise, please refer this link
I think you should use authentication_string instead of Password.
I am currently unable to login as root on mysql and I am not quite sure about what's going on. Here's what happened:
I ran mysql_secure_installation as recommended, to secure my mysql installation. Afterwards, I typed the default root password that was asked and then I entered:
No to setting a password for root
Yes on removing anonymous users
Yes on disallowing remote root login
Yes on removing the test database and access to it
Yes on reloading privilege tables
After completing this process, I tried accessing mysql with mysql -u root -p (entered the default password) and received this message:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
Do you have any ideas on what might have went wrong?
I also tried resetting the root password by starting mysql with --skip-grant-tables, but I am still not able to login.
I am using Ubuntu 14.04 and mysql 14.14 (LAMP stack).
I know this is an old post but the main answer is outdated and did not solve my issue.
Below my steps for future reference if anybody is having similar problems.
Stop mysql if it's running
$ sudo service mysql stop
Start mysql in safe mode
$ sudo mysqld_safe --skip-grant-tables --skip-syslog --skip-networking
If you get the error
"mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists"
Just create that folder:
$ mkdir -p /var/run/mysqld
$ chown mysql:mysql /var/run/mysqld
Open a new terminal window and log into mysql service + select mysql database:
$ mysql -u root
mysql> use mysql;
Set new password for root user:
mysql> update user set authentication_string=password('new-password') where user='root';
Flush privileges and exit mysql:
mysql> FLUSH PRIVILEGES;
mysql> exit;
Stop the safemode mysql (from the second terminal, you will see it stop in the first terminal)
$ mysqladmin -u root -p shutdown
Restart mysql
$ sudo service mysql start
You should be able to use the root user with password now to login to mysql/phpmyadmin
CentOS/Redhat:
From what I read in docs, when you run mysql_secure_installation, a temporary root password is generated and is stored in some log file.
sudo grep 'temporary password' /var/log/mysqld.log
Debian/Ubuntu:
During the packages installation, you get a prompt asking for the root password. If you don’t set it up, MySQL’s root user is created without a password. We can read the following line in package installation output:
Shell
2016-05-16T07:27:21.532619Z 1 [Warning] root#localhost is created with
an empty password ! Please consider switching off the
--initialize-insecure option.
but it is configured with the auth_socket plugin. You will only be able to connect using the UNIX socket, therefore any attempt to connect using your local IP or the network fails. Later on, you can change the password to allow connections from the network (as explained in this blog post).
Source
All we can do now is to see the root password. Lets change the root password since you cannot understand hashed password even if we can see it:
sudo service mysql stop
sudo mysqld_safe --skip-grant-tables --skip-syslog --skip-networking
then run mysql in a new terminal
mysql -u root
and run the following query, after changing the password
UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root';
FLUSH PRIVILEGES;
quit the mysql safe mode and start mysql service by
mysqladmin shutdown
sudo service mysql start
just run this script by root , you need custormize you password
mysqlpassword=password
/usr/bin/mysqladmin -u root password "$mysqlpassword"
#configure mysql login privileges
echo "grant all privileges on *.* to root#\"localhost\" identified by \"$mysqlpassword\";show databases;" |mysql -u root -p$mysqlpassword
Cannot login to MySQL database after fresh install with root ID and empty/no password like other older MySQL versions do
There's so many answers out there saying to reinstall mysql or use some combo of
mysqld_safe --skip-grant-tables
and / or
UPDATE mysql.user SET Password=PASSWORD('password')
and / or something else ...
... None of it was working for me
Here's what worked for me, on Ubuntu 18.04, from the top
With special credit to this answer for digging me out of the frustration on this ...
$ sudo apt install mysql-server
$ sudo cat /etc/mysql/debian.cnf
Note the lines which read:
user = debian-sys-maint
password = blahblahblah
Then:
$ mysql -u debian-sys-maint -p
Enter password: // type 'blahblahblah', ie. password from debian.cnf
mysql> USE mysql
mysql> SELECT User, Host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| User | Host | plugin |
+------------------+-----------+-----------------------+
| root | localhost | auth_socket |
| mysql.session | localhost | mysql_native_password |
| mysql.sys | localhost | mysql_native_password |
| debian-sys-maint | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
4 rows in set (0.00 sec)
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> COMMIT; // When you don't have auto-commit switched on
Either:
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'new_password';
Or:
// For MySQL 5.7+
UPDATE mysql.user SET authentication_string=PASSWORD('new_password') where user='root';
Then:
mysql> FLUSH PRIVILEGES;
mysql> COMMIT; // When you don't have auto-commit switched on
mysql> EXIT
$ sudo service mysql restart
$ mysql -u root -p
Enter password: // Yay! 'new_password' now works!
After you installed MySQL-community-server 5.7 from fresh on linux, you will need to find the temporary password from /var/log/mysqld.log to login as root.
grep 'temporary password' /var/log/mysqld.log
Run mysql_secure_installation to change new password
ref: http://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html
MySQL 5.7 changed the secure model: now MySQL root login requires a sudo
The simplest (and safest) solution will be create a new user and grant required privileges.
1. Connect to mysql
sudo mysql --user=root mysql
2. Create a user for phpMyAdmin
CREATE USER 'phpmyadmin'#'localhost' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'phpmyadmin'#'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Reference - https://askubuntu.com/questions/763336/cannot-enter-phpmyadmin-as-root-mysql-5-7
MySQL server 5.7 was already installed by default on my new Linux Mint 19.
But, what's the MySQL root password? It turns out that:
The default installation uses auth_socket for authentication, in lieu of passwords!
It allows a password-free login, provided that one is logged into the Linux system with the same user name. To login as the MySQL root user, one can use sudo:
sudo mysql --user=root
But how to then change the root password? To illustrate what's going on, I created a new user "me", with full privileges, with:
mysql> CREATE USER 'me'#'localhost' IDENTIFIED BY 'my_new_password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'me'#'localhost' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
Comparing "me" with "root":
mysql> SELECT user, plugin, HEX(authentication_string) FROM mysql.user WHERE user = 'me' or user = 'root';
+------+-----------------------+----------------------------------------------------------------------------+
| user | plugin | HEX(authentication_string) |
+------+-----------------------+----------------------------------------------------------------------------+
| root | auth_socket | |
| me | mysql_native_password | 2A393846353030304545453239394634323734333139354241344642413245373537313... |
+------+-----------------------+----------------------------------------------------------------------------+
Because it's using auth_socket, the root password cannot be changed: the SET PASSWORD command fails, and mysql_secure_installation desn't attain anything...
==> To zap this alternate authentication mode and return the MySQL root user to using passwords:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'SOME_NEW_ROOT_PASSWORD';
A good explanation.
More details from the MySQL manual.
In case you want to install mysql or percona unattended (like in my case ansible), you can use following script:
# first part opens mysql log
# second part greps lines with temporary password
# third part picks last line (most recent one)
# last part removes all the line except the password
# the result goes into password variable
password=$(cat /var/log/mysqld.log | grep "A temporary password is generated for" | tail -1 | sed -n 's/.*root#localhost: //p')
# setting new password, you can use $1 and run this script as a file and pass the argument through the script
newPassword="wh#teverYouLikE"
# resetting temporary password
mysql -uroot -p$password -Bse "ALTER USER 'root'#'localhost' IDENTIFIED BY '$newPassword';"
MySQL 5.7 or newer generates a default temporary password after fresh install.
To use MySQL first you would be required to get that password from the log file which is present at the /var/log/mysqld.log. So follow the following process:
grep 'temporary password' /var/log/mysqld.log
mysql_secure_installation
The second command is required to change the password for MySQL and also to make certain other changes like removing temporary databases, allow or disallow remote access to root user, delete anonymous users etc…
It seems things were designed to avoid developers to set the root user, a better solution would be:
sudo mysql -u root
Then create a normal user, set a password, then use that user to work.
create user 'user'#'localhost' identified by 'user1234';
grant all on your_database.* to 'user'#'localhost';
select host, user from mysql.user;
Then try to access:
mysql -u user -p
Boom!
None of these answers worked for me on Ubuntu Server 18.04.1 and MySQL 5.7.23. I spent a bunch of time trying and failing at setting the password and auth plugin manually, finding the password in logs (it's not there), etc.
The solution is actually super easy:
sudo mysql_secure_installation
It's really important to do this with sudo. If you try without elevation, you'll be asked for the root password, which you obviously don't have.
After a lot of try, I could reset the default password with the following commands (Ubuntu and derivatives):
sudo -i
mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld
/etc/init.d/mysql stop
mysqld_safe --skip-grant-tables &
mysql -uroot
use mysql;
update user set authentication_string=password('YOURPASSWORD') where user='root';
update user set plugin="mysql_native_password" where User='root';
flush privileges;
quit;
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start
Sometimes, even after typed in the terminal
mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld
/etc/init.d/mysql stop
mysqld_safe --skip-grant-tables &
I got the error that the mysqld don't exists. So, quit, and type the same commands again.
And the final command
sudo /etc/init.d/mysql start
Sometimes doesn't work. Only after restart the computer.
I just installed Linux Mint 19 (based on Ubuntu 18.04) on my machine. I installed MySQL 5.7 from the repo (sudo apt install mysql-server) and surprisingly during installation, the setup didn't prompt to enter root password. As a result I wasn't able to login into MySQL. I googled here and there and tried various answers I found on the net, including the accepted answer above. I uninstalled (purging all dpkgs with mysql in its name) and reinstalled again from the default Linux Mint repositories. NONE works.
After hours of unproductive works, I decided to reinstall MySQL from the official page. I opened MySQL download page (https://dev.mysql.com/downloads/repo/apt) for apt repo and clicked Download button at the bottom right.
Next, run it with dpkg:
sudo dpkg -i mysql-apt-config_0.8.10-1_all.deb
At the installation setup, choose the MySQL version that you'd like to install. The default option is 8.0 but I changed it to 5.7. Click OK to quit. After this, you have a new MySQL repo in your Software Sources.
Update your repo:
sudo apt update
Finally, install MySQL:
sudo apt install mysql-server
And now I was prompted to provide root password! Hope it helps for others with this same experience.
As of Ubuntu 20.04 with MySql 8.0 : you can set the password that way:
login to mysql with sudo mysql -u root
change the password:
USE mysql;
UPDATE user set authentication_string=NULL where User='root';
FLUSH privileges;
ALTER USER 'root'#'localhost' IDENTIFIED WITH caching_sha2_password BY 'My-N7w_And.5ecure-P#s5w0rd';
FLUSH privileges;
QUIT
now you should be able to login with mysql -u root -p (or to phpMyAdmin with username root) and your chosen password.
P,S:
You can also login with user debian-sys-maint, the password is written in the file /etc/mysql/debian.cnf
To do it in non interactive mode (from a script):
systemctl start mysqld
MYSQL_ROOT_TMP_PSW=$(grep 'temporary password' $logpath/mysqld.log |sed "s|.*: ||")
## POPULATE SCHEMAS WITH ROOT USER
/usr/bin/mysql --connect-expired-password -u root -p${MYSQL_ROOT_TMP_PSW} < "$mysql_init_script"
Here's the head of the init script
SET GLOBAL validate_password_policy=LOW;
FLUSH privileges;
SET PASSWORD = PASSWORD('MYSQL_ROOT_PSW');
FLUSH privileges;
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%';
FLUSH privileges;
...
Then restart the service systemctl restart mysqld
In my case the data directory was automatically initialized with the --initialize-insecure option. So /var/log/mysql/error.log does not contain a temporary password but:
[Warning] root#localhost is created with an empty password ! Please
consider switching off the --initialize-insecure option.
What worked was:
shell> mysql -u root --skip-password
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'new_password';
Details: MySQL 5.7 Reference Manual > 2.10.4 Securing the Initial MySQL Account
I to was experiencing the same problem and the only thing I was able to do to make it work was to go this route:
drop user admin#localhost;
flush privileges;
create user admin#localhost identified by 'admins_password'
This allowed me to recreate my username and enter a password for the user name.
I have been following a manual to install a software suite on Ubuntu. I have no knowledge of MySQL at all. I have done the following installations on my Ubuntu.
sudo apt-get update
sudo apt-get install mysql-server-5.5
sudo apt-get install mysql-client-5.5
sudo apt-get install mysql-common
sudo apt-get install glade
sudo apt-get install ntp
Then I do
cd ~/Desktop/iPDC-v1.3.1/DBServer-1.1
mysql -uroot -proot <"Db.sql"
I ended up with the following error message.
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
How may I fix it and continue?
Note: For MySQL 5.7+, please see the answer from Lahiru to this question. That contains more current information.
For MySQL < 5.7:
The default root password is blank (i.e., an empty string), not root. So you can just log in as:
mysql -u root
You should obviously change your root password after installation:
mysqladmin -u root password [newpassword]
In most cases you should also set up individual user accounts before working extensively with the database as well.
I was recently faced with the same problem, but in my case, I remember my password quite alright, but it kept on giving me the same error. I tried so many solutions, but still none helped. Then I tried this:
mysql -u root -p
After which it asks you for a password like this
Enter password:
And then I typed in the password I used. That's all.
I was able to solve this problem by executing this statement
sudo dpkg-reconfigure mysql-server-5.5
Which will change the root password.
You have to reset the password! Steps for Mac OS X (tested and working) and Ubuntu:
Stop MySQL using
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 --skip-networking
(the 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';
As per #IberoMedia's comment, for newer versions of MySQL, the field is called authentication_string:
mysql> UPDATE mysql.user SET authentication_string =PASSWORD('password') WHERE User='root';
Start MySQL using:
sudo service mysql start
or
sudo /usr/local/mysql/support-files/mysql.server start
Your new password is 'password'.
Note: for version of MySQL > 5.7 try this:
update mysql.user set authentication_string='password' where user='root';
It happens when your password is missing.
Steps to change the password when you have forgotten it:
Stop MySQL Server (on Linux):
sudo systemctl stop mysql
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 and run mysql -u root (as root). It will not ask for a password.
If you get error like as below:
2018-02-12T08:57:39.826071Z mysqld_safe Directory '/var/run/mysqld' for UNIX
socket file don't exists.
mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock' (2)
[1]+ Exit 1
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
Run the same command in step 2 to run MySQL in background.
Run mysql -u root. You will get the MySQL console without entering a password.
Run these commands
FLUSH PRIVILEGES;
For MySQL 5.7.6 and newer
ALTER USER 'root'#'localhost' IDENTIFIED BY 'new_password';
For MySQL 5.7.5 and older
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('new_password');
If the ALTER USER command doesn't work use:
UPDATE mysql.user SET authentication_string = PASSWORD('new_password') WHERE User = 'root' AND Host = 'localhost';
Now exit
To stop the instance started manually:
sudo kill `cat /var/run/mysqld/mysqld.pid`
Restart MySQL
sudo systemctl start mysql
At the initial start up of the server the following happens, given that the data directory of the server is empty:
The server is initialized.
SSL certificate and key files are generated in the data directory.
The validate_password plugin is installed and enabled.
The superuser account 'root'#'localhost' is created. The password for the superuser is set and stored in the error log file.
To reveal it, use the following command:
shell> sudo grep 'temporary password' /var/log/mysqld.log
Change the root password as soon as possible by logging in with the generated temporary password and set a custom password for the superuser account:
shell> mysql -u root -p
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass5!';
If the problem still exists, try to force changing the password:
/etc/init.d/mysql stop
mysqld_safe --skip-grant-tables &
mysql -u root
Set up a new MySQL root user password:
use mysql;
update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
flush privileges;
quit;
Stop the MySQL server:
/etc/init.d/mysql stop
Start the MySQL server and test it:
mysql -u root -p
If none of the other answers work for you, and you received this error:
mysqld_safe Logging to '/var/log/mysql/error.log'.
mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
[1]+ Exit 1 sudo mysqld_safe --skip-grant-tables
Follow the below commands step by step until you reset your password:
# Stop your server first
sudo service mysql stop
# Make the MySQL service directory.
sudo mkdir /var/run/mysqld
# Give MySQL permission to work with the created directory
sudo chown mysql: /var/run/mysqld
# Start MySQL, without permission and network checking
sudo mysqld_safe --skip-grant-tables --skip-networking &
# Log in to your server without any password.
mysql -u root mysql
# Update the password for the root user:
UPDATE mysql.user SET authentication_string=PASSWORD('YourNewPasswordBuddy'), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';
# If you omit (AND Host='localhost') section, it updates
# the root password regardless of its host
FLUSH PRIVILEGES;
EXIT;
# Kill the mysqld_safe process
sudo service mysql restart
# Now you can use your new password to log in to your server
mysql -u root -p
# Take note for remote access. You should create a remote
# user and then grant all privileges to that remote user
I came across this very annoying problem and found many answers that did not work. The best solution I came across was to completely uninstall MySQL and reinstall it. On reinstall you set a root password and this fixed the problem.
sudo apt-get purge mysql-server mysql-client mysql-common mysql-server-core-5.5 mysql-client-core-5.5
sudo rm -rf /etc/mysql /var/lib/mysql
sudo apt-get autoremove
sudo apt-get autoclean
I found this code elsewhere, so I don't take any credit for it. But it works. To install MySQL after uninstalling it, I think DigitalOcean has a good tutorial on it. Checkout my gist for this.
How to install MySQL on Ubuntu (which works)
I am using Ubuntu 16.04 (Xenial Xerus) and installed MySQL 5.7.
I had the same issue
Login denied for root user.
I tried the below steps:
dpkg --get-selections | grep mysql (to get the version of MySQL).
dpkg-reconfigure mysql-server-5.7
mysql -u root -p
Without -p that doesn't prompt you to ask password. Once you are in, you can create a user with a password by following steps:
CREATE USER 'your_new_username'#'your-hostname' IDENTIFIED BY 'your-password';
GRANT ALL PRIVILEGES ON *.* to 'your_new_username'#'your-hostname' WITH GRANT OPTION;
Exit from the root and log in from the <name> you gave above.
mysql -u <your_new_username> -p
For some reason still just typing MySQL does not work. At all. I suggest to make it a habit to use mysql -u <name> -p.
In the terminal, just enter:
mysql -u root -p
Then it will ask the password from you.
I installed MySQL as root user ($SUDO) and got this same issue
Here is how I fixed it:
sudo cat /etc/mysql/debian.cnf
This will show details as:
# Automatically generated for Debian scripts. DO NOT TOUCH! [client] host = localhost user = debian-sys-maint password = GUx0RblkD3sPhHL5 socket = /var/run/mysqld/mysqld.sock [mysql_upgrade] host = localhost user = debian-sys-maint password = GUx0RblkD3sPhHL5 socket = /var/run/mysqld/mysqld.sock
Above we can see the password. But we are just going to use(GUx0RblkD3sPhHL5) that in the prompt.
`mysql -u debian-sys-maint -p
Enter password: `
Now provide the password (GUx0RblkD3sPhHL5).
Now exit from MySQL and log in again as:
`mysql -u root -p
Enter password: `
Now provide the new password. That's all. We have a new password for further uses.
It worked for me.
For those for whom the current answers didn't work can try this (tested on macOS):
mysql -h localhost -u root -p --protocol=TCP
After this, a password will be asked from you and you should use your OS user password. Then when you get into MySQL you can run:
select Host, User from mysql.user;
And you should see:
MySQL [(none)]> select Host, User from mysql.user;
+-----------+------------------+
| Host | User |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
And from here you can change the configurations and edit the password or modify the grants.
Please read the official documentation: MySQL: How to Reset the Root Password
If you have access to a terminal:
MySQL 5.7.6 and later:
mysql
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
MySQL 5.7.5 and earlier:
mysql
mysql> SET PASSWORD FOR 'root'#'localhost' = PASSWORD('MyNewPass');
I am using mysql-5.7.12-osx10.11-x86_64.dmg on Mac OS X.
The installation process automatically sets up a temporary password for the root user. You should save the password. The password can not be recovered.
Follow the instructions:
Go to cd /usr/local/mysql/bin/
Enter the temporary password (which would look something like, "tsO07JF1=>3")
You should get the mysql> prompt.
Run, SET PASSWORD FOR 'root'#'localhost' = PASSWORD('{YOUR_PASSWORD}'); If you wish to set your password: "root" then the command would be, SET PASSWORD FOR 'root'#'localhost' = PASSWORD('root');
Run ALTER USER 'root'#'localhost' PASSWORD EXPIRE NEVER;
Run exit
Run ./mysql -u root -p
Type your password. In my case I would type, "root" (without quote)
That's all.
For convenience, you should add "/usr/local/mysql/bin" to your PATH environment variable.
Now from anywhere you can type ./mysql -u root -p and then type the password and you will get the mysql> prompt.
The answer may sound silly, but after wasting hours of time, this is how I got it to work:
mysql -u root -p
I got the error message
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
Even though I was typing the correct password (the temporary password you get when you first install MySQL).
I got it right when I typed in the password when the password prompt was blinking.
If you have MySQL as part of a Docker image (say on port 6606) and an Ubuntu install (on port 3306) specifying the port is not enough:
mysql -u root -p -P 6606
will throw:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
as it's trying to connect to localhost by default, specifying your local IP address fixes the issue:
mysql -u root -p -P 6606 -h 127.0.0.1
Year 2021.
Answer for Ubuntu 20.04 (Focal Fossa) (maybe other distributions as well).
After days of wandering around... and having none of those answers working for me, I did this and it worked!
Always in a Bash shell:
sudo systemctl disable mysql
In order to stop the daemon from starting on boot.
sudo apt purge mysql-server
and
sudo apt purge mysql-community-server*
There, it warns you you'll erase configuration files... so it's working! Because those are the ones making trouble!
sudo autoremove
To delete all the left behind packages.
Then (maybe it's optional, but I did it) reboot.
Also, I downloaded mysql-server-8.0 from the official MySQL webpage:
sudo apt install mysql-server
A signal that it's working is that when you enter the command above, the system asks you to enter the root password.
Finally:
mysql -u root -p
And the password you entered before.
If the problem still exists, try to force changing the password.
Stop MySQL Server (on Linux):
/etc/init.d/mysql stop
Stop MySQL Server (on Mac OS X):
mysql.server stop
Start the mysqld_safe daemon with --skip-grant-tables:
mysqld_safe --skip-grant-tables &
mysql -u root
Set up a new MySQL root user password:
use mysql;
update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
flush privileges;
quit;
Stop MySQL Server (on Linux):
/etc/init.d/mysql stop
Stop MySQL Server (on Mac OS X):
mysql.server stop
Start the MySQL server service and test to log in by root:
mysql -u root -p
I also came across the same problem. I did:
Open your cmd
Navigate to C:\Program Files\MySQL\MySQL Server 8.0\bin>
(where MySQL Server 8.0 may be different depending on the server you installed)
Then put the following command mysql -u root -p
It will prompt for the password... simply hit Enter, as sometimes the password you entered while installing is changed by to blank.
Now you can simply access the database.
This solution worked for me on the Windows platform.
By default, the password will be null, so you have to change the password by doing the below steps.
Connect to MySQL
root# mysql
Use mysql
mysql> update user set password=PASSWORD('root') where User='root';
Finally, reload the privileges:
mysql> flush privileges;
mysql> quit
Just one line and it solved my issue.
sudo dpkg-reconfigure mysql-server-5.5
In Ubuntu 16.04 (Xenial Xerus) and MySQL version 5.7.13, I was able to resolve the problem with the steps below:
Follow the instructions from section B.5.3.2.2 Resetting the Root Password: Unix and Unix-Like Systems
MySQL 5.7 reference manual
When I tried #sudo mysqld_safe --init-file=/home/me/mysql-init & it failed. The error was in /var/log/mysql/error.log:
2016-08-10T11:41:20.421946Z 0 [Note] Execution of init_file '/home/me/mysql/mysql-init' started.
2016-08-10T11:41:20.422070Z 0 [ERROR] /usr/sbin/mysqld: File '/home/me/mysql/mysql-init' not found (Errcode: 13 - Permission denied)
2016-08-10T11:41:20.422096Z 0 [ERROR] Aborting
The file permission of mysql-init was not the problem. We need to edit AppArmor permissions.
Edit by sudo vi /etc/apparmor.d/usr.sbin.mysqld
....
/var/log/mysql/ r,
/var/log/mysql/** rw,
# Allow user init file
/home/pranab/mysql/* r,
# Site-specific additions and overrides. See local/README for details.
#include <local/usr.sbin.mysqld>
}
Do sudo /etc/init.d/apparmor reload
Start mysqld_safe again. Try step 2 above. Check file /var/log/mysql/error.log. Make sure there is no error and the mysqld is successfully started.
Run mysql -u root -p
Enter password:
Enter the password that you specified in mysql-init. You should be able to log in as root now.
Shutdown mysqld_safe by sudo mysqladmin -u root -p shutdown
Start mysqld the normal way by sudo systemctl start mysql
While the top answer (with mysqladmin) worked on macOS v10.15 (Catalina), it did not work on Ubuntu. Then I tried many of the other options, including a safe start for MySQL, but none worked.
Here is one that does:
At least for the version I got 5.7.28-0ubuntu0.18.04.4 answers were lacking IDENTIFIED WITH mysql_native_password. 5.7.28 is the default on the current LTS and thus should be the default for most new new systems (till Ubuntu 20.04 (Focal Fossa) LTS comes out).
I found Can't set root password MySQL Server and now applied
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'your_pass_here';
which does work.
The error that I faced was:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
It was a problem with the port running on.
By default, MySQL is running on port 3306.
You can check that on by running
in a 32-bit system:
sudo /opt/lampp/manager-linux.run
in a 64-bit system:
sudo /opt/lampp/manager-linux-x64.run
and click on the Configure button.
In my case the port was running on 3307, and I used the command
mysql -u root -p -P 3307 -h 127.0.0.1
Copied from this link, I had the same problem and this solved the problem. After we add a password for the database, we need to add -p (password-based login), and then enter the password. Otherwise, it will return this error:
mysql -u root -p
Because your error message says "PASSWORD: YES" this means you are are using the wrong password. This happened to me also. Luckily I remembered my correct password, and was able to make the DB connection work.
In recent MySQL versions there isn't any password in the mysql.user table.
So you need to execute ALTER USER. Put this one line command into the file.
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
And execute it as an init file (as the root or mysql user):
mysqld_safe --init-file=/home/me/mysql-init &
MySQL server need to be stopped to start mysqld_safe.
Also, there may be a problem with AppArmor permissions to load this init file. Read more in AppArmor and MySQL.
If you haven't set password yet, then run mysql -uroot. It works for me.
On Mac, if you have a problem in logging in with the first password you were given in installation, maybe you can just simply kill the MySQL process and then try.
So:
run the following command to find the PID of MySQL:
ps -aef | grep mysql | grep -v grep
kill the process:
kill -15 [process id]
Then you can log in with the initial password using this command:
mysql -uroot -p
Which asks you to enter your password. Just enter the initial password.