I just installed MySQL on Ubuntu and the root user can\'t log in :)
How can I recover or find out my password? Using blank for password does not work.
Under MYSQL 5.7, If you are using mysql for development purpose, just :
1.kill mysql :
$ sudo service mysql stop
2.start mysql under --skip-grant-tables mode:
$ sudo mysqld_safe --skip-grant-tables
and, further, you could try to change the user table under "skip-grant-table" mode, however I failed.
so, this is just a workaround.
sudo mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YOUR_PASSWORD_HERE';
FLUSH PRIVILEGES;
mysql -u root -p # and it works
MySQL 5.5 on Ubuntu 14.04 required slightly different commands as recommended here. In a nutshell:
sudo /etc/init.d/mysql stop
sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
mysql -u root
And then from the MySQL prompt
FLUSH PRIVILEGES;
SET PASSWORD FOR root@'localhost' = PASSWORD('password');
UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';
FLUSH PRIVILEGES;
And the cited source offers an alternate method as well.
I'm going to make a bit of an assumption here because I'm not sure. I don't think my MySQL (running on latest 20.04 upgraded) even has a root. I have tried setting one and I remember having problems. I suspect there is not a root user and it will automatically log you in as the MySQL root user if you're logged in as root.
Why do I think this? Because when I do MySQL -u root -p, it will accept any password and log me in as the MySQL root user when I am logged in as root.
I have confirmed that trying that on a non root user doesn't work.
I like this model.
EDIT 2020.12.19: It is no longer a mystery to me why if you are logged in as the root user you get logged into MySQL as the root user. It has to do with the authentication type. Later versions of MySQL are configured with the MySQL plugin 'auth_socket' (maybe you've noticed the /run/mysqld/mysqld.sock file on your system and wondered about it). The plugin uses the SO_PEERCRED option provided by the library auth_socket.so.
You can revert back to password authentication if desired simply by create/update of the password. Showing both ways and options below to make clear.
CREATE USER 'valerie'@'localhost' IDENTIFIED WITH auth_socket;
CREATE USER 'valerie'@'localhost' IDENTIFIED BY 'password';
Here is the best way to set your root password : Source Link Step 3 is working perfectly for me.
Commands for You
- sudo mysql
- SELECT user,authentication_string,plugin,host FROM mysql.user;
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
- FLUSH PRIVILEGES;
- SELECT user,authentication_string,plugin,host FROM mysql.user;
- exit
Now you can use the Password for the root user is 'password' :
- mysql -u root -p
- CREATE USER 'sammy'@'localhost' IDENTIFIED BY 'password';
- GRANT ALL PRIVILEGES ON . TO 'sammy'@'localhost' WITH GRANT OPTION;
- FLUSH PRIVILEGES;
- exit
Test your MySQL Service and Version:
systemctl status mysql.service
sudo mysqladmin -p -u root version
There is a simple solution.
MySql 5.7 comes with anonymous user so you need to reconfigure MySQL server.
You can do that with this command
try to find temp pass:
grep 'temporary password' /var/log/mysqld.log
then:
sudo mysql_secure_installation
On this link is more info about mysql 5.7
https://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html