I was setting up a new CentOS server tonight and installed MySQL via yum. If you’re familiar with installing MySQL via apt-get on Debian-based systems, you’ll know that during the install it prompts you to set the MySQL root password. Not so on CentOS.
When I’ve installed MySQL on CentOS in the past, I could have sworn the default root password was blank, and that right after the installation is complete you set it like so:
mysqladmin -u root password NEW_PASSWORD
Either my memory is foggy, or something’s changed with how this is done. No matter what I tried I kept getting “access denied” messages from MySQL and of course since I never set a root password, I have no idea what it wants from me.
Luckily there’s a solution, at least one that worked for me, though I still feel like I may be missing something so although this worked, I’m happy to be told there’s a simpler way.
First, make sure no MySQL processes are running:
killall -9 mysqld
Next, start MySQL in safe mode and have it skip grant tables:
/usr/bin/mysqld_safe --skip-grant-tables &
This will let you get in as root without a password. After the process starts, log in and use the mysql database:
mysql -u root mysql
Next, set the root user password:
update user set password=PASSWORD('new_password') where user='root';
It should say 2 or 3 rows affected. Finally, flush the privileges:
Quit MySQL, and then you’ll want to kill the process you started with mysqld_safe and start the regular MySQL process. You can either bring the process you started earlier by typing ‘fg’, hitting enter, and then hitting ctrl-C to kill it, or you can do a ps -wef | grep mysqld to find the process ID and kill it as per usual.
Finally, restart MySQL and test your new root password:
mysql -u root -p
When it prompts you for the password, enter the password you set earlier and you should be logged in successfully.
Note this process also works if you’ve forgotten the MySQL root password.
Hope that save someone else a bit of time, and as I said earlier, I’ll be very happy if someone has a better explanation of why I wasn’t able to log in without a password on a fresh MySQL install on CentOS.