How to Check MySQL Listening Port

Knowing the port on which your MySQL server is listening is essential for database connectivity, security configurations, and troubleshooting. By default, MySQL listens on port 3306, but this can be changed for various reasons, such as security enhancement or running multiple instances. In this comprehensive guide, we’ll walk you through the process of checking the MySQL listening port and share tips on how to do it efficiently.
Table of Contents
- Understanding MySQL Default Port
- Checking MySQL Port Using Command Line
- Finding Port Information in MySQL Configuration Files
- Using SQL Commands to Retrieve Port Number
- Checking Active Ports with Networking Tools
- Tips for Efficient Port Checking
- Conclusion
1. Understanding MySQL Default Port
By default, MySQL listens on port 3306 for TCP/IP connections. However, this port can be modified during installation or afterward for security purposes or to accommodate multiple MySQL instances on the same server.
Why Might the Port Change?
- Security: Changing the default port can help obscure the service from automated attacks targeting standard ports.
- Multiple Instances: Running more than one MySQL server on the same machine requires assigning different ports to each instance.
2. Checking MySQL Port Using Command Line
The command line is one of the quickest ways to check the MySQL listening port.
Step 1: Access the Terminal
Open your terminal or command prompt.
Step 2: Use the netstat Command
For Linux or macOS:
sudo netstat -plnt | grep mysqld
For Windows (Command Prompt):
netstat -ano | findstr "3306"
Explanation:
netstat -plnt: Lists all listening ports with the associated program names.grep mysqld: Filters the results to show only MySQL-related entries.
Sample Output:
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1234/mysqld
The output shows that MySQL is listening on port 3306.
3. Finding Port Information in MySQL Configuration Files
The MySQL configuration file contains settings defining the server’s operation, including the listening port.
Step 1: Locate the Configuration File
- Linux: Typically located at
/etc/my.cnfor/etc/mysql/my.cnf. - Windows: Look for
my.iniin the MySQL installation directory.
Step 2: Open the Configuration File
Use a text editor to open the file. For example:
sudo nano /etc/mysql/my.cnf
Step 3: Search for the Port Setting
Look for the [mysqld] section and find the port variable.
[mysqld]
port=3306
If the port line is commented out or absent, MySQL uses the default port 3306.
4. Using SQL Commands to Retrieve Port Number
You can query the MySQL server to find out which port it is using.
Step 1: Log into MySQL
mysql -u root -p
Step 2: Run the Query
SHOW GLOBAL VARIABLES LIKE 'port';
Sample Output:
+---------------+-------+ | Variable_name | Value | +---------------+-------+ | port | 3306 | +---------------+-------+
Step 3: Exit MySQL
EXIT;
5. Checking Active Ports with Networking Tools
Use networking tools like ss or lsof for more detailed information.
Using ss Command (Linux)
sudo ss -tulpn | grep mysqld
Using lsof Command
sudo lsof -i -P -n | grep LISTEN | grep mysqld
These commands list open network sockets and filter the results to show MySQL-related entries.
6. Tips for Efficient Port Checking
Automate with Scripts
Create a simple script to check the MySQL port quickly:
#!/bin/bash
PORT=$(mysql -u root -pYOURPASSWORD -e "SHOW GLOBAL VARIABLES LIKE 'port';" | grep 'port' | awk '{print $2}')
echo "MySQL is listening on port: $PORT"
Note: Replace YOURPASSWORD with your actual password or better yet, use a .my.cnf file for credentials.
Use Aliases for Frequent Commands
Add aliases to your shell configuration (~/.bashrc or ~/.zshrc):
alias mysqlport="mysql -u root -p -e \"SHOW GLOBAL VARIABLES LIKE 'port';\""
Reload the shell:
source ~/.bashrc
Now, simply type mysqlport to check the port.
Secure Your MySQL Instance
- Firewall Rules: Ensure your firewall allows traffic on the MySQL port if remote connections are necessary.
- Bind Address: Set
bind-addressin the MySQL configuration to control which interfaces MySQL listens on.[mysqld] bind-address=127.0.0.1
Monitor Port Usage
Regularly monitor port usage to detect unauthorized changes:
sudo netstat -tulpn | grep LISTEN
7. Conclusion
Knowing how to check the MySQL listening port is essential for database administration, security, and troubleshooting. Whether you prefer using command-line tools, querying the database directly, or inspecting configuration files, this guide provides multiple methods to find the information you need efficiently.
Remember: Always secure your MySQL server by keeping ports properly configured and monitored.
- #check MySQL listening port
- #check MySQL port
- #Database Administration
- #database configuration
- #efficient MySQL port checking
- #find MySQL port
- #linux
- #mysql
Comments (0)
Sign in to join the discussion
Sign in

