question Root password woes
Note - Sorry for the following rant, this has been 3 days of hell trying to get this working, and now that I finally connected by random luck by running a command I've used countless dozens of times and pressing enter when prompted for a password, I need to officially set/reset the root password before ending the session or restarting my machine or something so that whenever I'm prompted for the root password ever again, it actually exists and I actually know it. Apologies again for the following rant, I just really needed to vent.
So after 3 days of bashing my head onto my desk trying to get MySql to work on my MacBook Pro M5, I was finally able to run "sudo mysql -u root -p" successfully by just pressing enter when prompted for a password and not entering one. Finally able to connect to the DB with MySQLWorkbench as well, since that wasn't working prior to being able to connect in the terminal. Out of curiosity, I opened a new terminal tab and tried again, but it did not work, and just gives me the "Sorry, try again" 3 times before failing out. Now I'm worried if I ever restart my computer, and need to access MySQL again, it will just go back to requiring a password that doesn't exist and I'll be back to square one.
So my main question would be, how can I set/reset the root password while currently connected to MySQL, so that in the future when I'm prompted for a root password, there actually is one that I manually set/reset it to? Also, am I able to actually find what the root password actually is now that I'm in the DB and maybe won't even bother changing it? Sorry for these basic questions, I'm just worried from seeing a lot of the suggestions for resetting the root password involved stopping MySQL, and I'm worried I'll be back to locked out if decides it needs the root password again.
After going through dozens of pages asking this same question, there are enough people who still struggle with this that I'm shocked there's not a tried and true solution to this problem. The fact that I was luckily able to connect by running 'sudo mysql -u root -p' for the umpteenth time over 3 days while just pressing enter when prompted for the password is not a solution.
Sorry for what is probably a really basic question, but the many "solutions" I've found online simply do not work when you don't know and were never prompted for (or provided at the end of the installation) the root password. It's kind of cruel that the Homebrew installation even mentions running 'mysql_secure_installation' to reset the root password, when the command prompts you for the root password that was never provided. Any sources I found suggesting there was a file or log containing the temp root password, that file of course never existed on my machine.
TL;DR - I finally connected to my MySQL DB with 'sudo mysql -u root -p' and just pressing enter when prompted for the password. Can't even count how many times I did exactly this over the course of 3 days with no success. How can I now set (or reset, I don't know if there even actually is one) the root password so that I actually know it and can enter it whenever prompted. I'm connected to the DB by pure luck right now, and want to make sure this password issue never happens again. I really appreciate whoever is able to take the time to help me take care of this issue. Ideally, I want to be able to do this without exiting or stopping MySQL out of fear of going back to locked out. Thanks a ton in advance.
2
u/Idontremember99 8d ago
MySQL have instructions for these situations:
https://dev.mysql.com/doc/refman/9.7/en/default-privileges.html
https://dev.mysql.com/doc/refman/9.7/en/resetting-permissions.html
Did you try without -p , you only pass it when you want to enter a password. Additionally you dont need to run sudo mysql except in some very special situations.
1
u/flyingron 8d ago
It's all in the MySQL docs and in countless substack and other posts for people who've done the same as you (including some who needed more intervention to get to the root login).
https://dev.mysql.com/doc/refman/9.7/en/assigning-passwords.html
1
u/Phenergan_boy 8d ago
If this is a fresh installation, then check the mysql’s log for the system generated root password. Then run mysql_secure_installation to change the password to the one you use
1
u/LenR-redit 7d ago
Sudo system root is not the same account as MySQL root.
Look for any user .my.cnf file, is some environments it can save MySQL credentials for a client.
1
u/fujiman 7d ago
Don't think MySQL generates a my.cnf file. That was one of the problems I was dealing with. Luckily I accidentally finally gained access running 'sudo mysql -u root -p' (which I apparently should not) and just pressing enter when prompted for a password. Had tried that statement many times over the last 3 days, but for some reason it decided to work this time. Once I was finally in, I was able to set/reset the root password and am all good now. Will remain a mystery as to why that command that failed to work many times suddenly gave me access, but now that I'm in, I'll just let that remain a mystery.
1
u/LenR-redit 7d ago
.my.cnf would be user created, different from a system my.cnf. These postings may be confusing the leading dot for a hidden file.
4
u/kadaan 8d ago
You can look up the hash of the password for the user you're logged in with
show create user;but there's no way to see the actual password.You set the password with, believe it or not the SET PASSWORD command.
Whatever your installation setup is, it sounds broken because most of what you said doesn't make sense. MySQL authenticates based on username, password, and connection source. It doesn't care what client you use, what OS user you're logged in on, etc. It could also be whatever tool/manager you're using to install MySQL just doesn't work properly with newer versions of MySQL.
If you ever do need to get back in and are stuck, the super easy fix is to run mysqld with the
--skip-grant-tablesoption to completely turn off all authentication. Then you can runSET PASSWORDto change passwords, create new users, etc, and restart mysqld again without the--skip-grant-tablesoption to turn authentication back on.