How to Use mysql_secure_installation
to Secure MySQL
After installing MySQL, run the following command to secure it:
You'll be prompted with a series of questions. Here's a breakdown of each one and what it does:
Step-by-Step Prompts and Explanations
-
Enter current password for root (enter for none):
-
If you’re using MySQL 8.0+, a temporary password was generated during installation. Paste it here.
-
If you're reinstalling or upgrading and no password was set, just press Enter.
-
Set root password? [Y/n]:
-
Choose Y to set a new, strong root password.
-
This is essential to prevent unauthorized access.
-
Remove anonymous users? [Y/n]:
-
Choose Y.
-
This removes users who can log in without a username, reducing security risks.
-
Disallow root login remotely? [Y/n]:
-
Choose Y for best security (recommended for most setups).
-
This prevents the root user from logging in remotely over the network.
-
Remove the test database and access to it? [Y/n]:
-
Choose Y.
-
The test database is only for development and should not exist on a production system.
-
Reload privilege tables now? [Y/n]:
-
Choose Y to apply all changes made.
-
Example Output
Conclusion
Using mysql_secure_installation
is a best practice for anyone deploying MySQL. It hardens your database against common threats by removing insecure default settings.