MySQL is a popular open-source database management system used by many websites and applications. By default, MySQL listens on port 3306 for incoming connections. However, in some cases, you may want to close this port to prevent unauthorized access to your database.
In this article, we will guide you through the process of closing the port DB HOST localhost in MySQL, ensuring the security of your database.
Step 1: Accessing MySQL Configuration File
The first step is to locate and access the MySQL configuration file. This file contains various settings and options for MySQL, including the port number.
1. Open your preferred text editor.
2. Locate the MySQL configuration file called my.cnf or my.ini. The location of this file may vary depending on your operating system and MySQL installation.
3. Open the configuration file in the text editor.
Step 2: Locating the Port Configuration
Once you have opened the MySQL configuration file, you need to locate the line that specifies the port number.
1. Search for the line that starts with port=. It should look similar to this:
port=3306
2. If you find this line, proceed to Step 3. If you don't find it, you can add it to the configuration file. Simply add the following line:
port=3306
Replace 3306 with the desired port number. Keep in mind that using a non-standard port may require additional configuration in your applications or firewall settings.
Step 3: Commenting Out the Port Configuration
To close the port, you need to comment out the port configuration line in the MySQL configuration file.
1. Add a # symbol at the beginning of the line that specifies the port number. It should look like this:
# port=3306
2. Save the changes to the MySQL configuration file.
Step 4: Restarting MySQL Service
After making the necessary changes to the MySQL configuration file, you need to restart the MySQL service for the changes to take effect.
1. Open your command prompt or terminal.
2. Type the following command to restart the MySQL service:
sudo service mysql restart
3. Press Enter to execute the command.
Once the MySQL service restarts, the port DB HOST localhost will be closed, preventing any incoming connections to the database.
Remember, closing the port may affect the functionality of your applications or services that rely on the MySQL database. Make sure to update the necessary configurations in your applications to use the new port or adjust firewall settings accordingly.
References
| Number | Source |
|---|---|
| 1 | MySQL Documentation - Server Command Options |
| 2 | DigitalOcean - How To Secure MySQL and MariaDB Databases in a Linux VPS |
| 3 | Linuxize - How to Restart MySQL Service on Linux |