/usr/local/Cellar/mysql/5.1.52/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'
I’ve also tried connecting with mysqladmin -u root -proot and various other combinations, but nothing works. Even running the secure installation script:
You’re having trouble connecting to a fresh MySQL installation on macOS 10.6 using Homebrew. After running brew install mysql 5.1.52 and mysql_install_db, you cannot set the root password because you receive an “Access denied for user ‘root’@‘localhost’ (using password: NO)” error when using mysqladmin or mysql_secure_installation.
Understanding the “Why” (The Root Cause):
The error “Access denied for user ‘root’@‘localhost’ (using password: NO)” usually means MySQL’s data directory is corrupted or the MySQL server isn’t running properly, even if the installation seemed successful. Homebrew installations sometimes have issues with the initial setup of the root user. A fresh installation should allow root access initially without a password, but if the data directory is damaged, MySQL can’t find its configuration or user accounts.
Step-by-Step Guide:
Completely Reinitialize the MySQL Data Directory: This step is crucial and will resolve the issue. We’ll completely remove the existing (potentially corrupted) data directory and force MySQL to create a fresh one.
Stop the MySQL Server: First, stop the MySQL server to prevent errors:
brew services stop mysql
Delete the Data Directory: Next, completely remove the existing data directory. Caution: This will delete all your MySQL data, so only proceed if this is a fresh, test installation.
sudo rm -rf /usr/local/var/mysql
Reinitialize the MySQL Database: Now, reinitialize MySQL using mysql_install_db. This command will recreate the necessary files and directories in the data directory. The following command uses the standard location, but adjust the paths if your Homebrew installation is different.
Start the MySQL Server: Finally, restart the MySQL server:
brew services start mysql
You should now be able to connect as root without a password. If not, proceed to step 2.
Manual Root Password Setup (if Step 1 fails): If the above steps still don’t solve the problem, manually create a root user with the password.
Start MySQL in Safe Mode: Start MySQL in safe mode to bypass authentication checks:
sudo mysqld_safe --skip-grant-tables &
Connect to MySQL: Connect to the MySQL server as the root user without a password:
mysql -u root
Create or Reset Root Password: Once connected, execute the following commands to set a new password. Replace 'your_new_password' with your desired password.
FLUSH PRIVILEGES;
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('your_new_password');
Quit and Restart: Quit MySQL (exit;), stop the safe mode server (sudo pkill mysqld), and then start the MySQL server normally (brew services start mysql).
Common Pitfalls & What to Check Next:
Homebrew Conflicts: Check for conflicts with other installed versions of MySQL. Use ps aux | grep mysql to see all running MySQL processes. If other processes are running, they might be interfering.
Permissions: Make sure the mysql user has the correct permissions on /usr/local/var/mysql. This is unlikely to be the root cause, but worth investigating if the above steps fail.
Incorrect Paths: Double-check that the paths used in the commands (e.g., /usr/local/Cellar/mysql/5.1.52) match your Homebrew installation.
Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!
check if there’s another mysql instance running from an old install. run ps aux | grep mysql to see what’s active. old mysql processes often mess with the homebrew version and create permission conflicts.
yup, looks like mysql’s not up. run sudo /usr/local/Cellar/mysql/5.1.52/bin/mysqld_safe & to start it. homebrew usually doesn’t start it by default, that’s likely the issue.
This issue commonly arises with Homebrew MySQL installations. It’s likely that although MySQL appears to be running, the root user settings may have been disrupted during setup. A solution that worked for me involves first terminating MySQL: sudo pkill mysqld. Then, start it in safe mode using sudo mysqld_safe --skip-grant-tables &. This allows you to connect without a password through mysql -u root. Once connected, execute FLUSH PRIVILEGES; followed by SET PASSWORD FOR 'root'@'localhost' = PASSWORD('yournewpassword');. After exiting MySQL and stopping the safe mode process, restart MySQL normally. This approach resolved the same issue for me.
Been there with MySQL headaches. Manual setup gets messy fast.
I stopped dealing with local MySQL installations after hitting similar walls. Now I automate the entire database workflow with Latenode.
I set up automated flows that connect directly to cloud MySQL instances or provision them on demand. No localhost connection issues, no root password drama, no safe mode recoveries.
Last month I built a flow that spins up a fresh MySQL instance, runs schema migrations, and sets up user permissions automatically. Takes 2 minutes versus hours troubleshooting local installs.
You can trigger database operations from any app without worrying about your local environment being broken. Plus you get proper backups and scaling built in.