How to Change Port in MySQL: Simple Steps
To change the port MySQL listens on, edit the
my.cnf or my.ini configuration file and set the port option under the [mysqld] section. Then restart the MySQL server to apply the new port.Syntax
In the MySQL configuration file (my.cnf on Linux/macOS or my.ini on Windows), you specify the port number under the [mysqld] section using the port option.
[mysqld]: This section configures the MySQL server settings.port=3307: Sets the port number MySQL listens on (default is 3306).
ini
[mysqld]
port=3307Example
This example shows how to change the MySQL port to 3307 by editing the configuration file and restarting the server.
bash
# Step 1: Open the MySQL config file (example for Linux) sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf # Step 2: Add or modify the port under [mysqld] [mysqld] port=3307 # Step 3: Save and exit the editor # Step 4: Restart MySQL server sudo systemctl restart mysql # Step 5: Connect using the new port mysql -u root -p --port=3307
Output
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.x MySQL Community Server - GPL
mysql>
Common Pitfalls
Common mistakes when changing MySQL port include:
- Not editing the correct configuration file or section.
- Forgetting to restart the MySQL server after changes.
- Not updating client connection commands to use the new port.
- Firewall blocking the new port.
Always verify the port change by connecting with the --port option.
ini
[client] # Wrong: port set under [client] instead of [mysqld] port=3307 # Correct: [mysqld] port=3307
Quick Reference
| Step | Action |
|---|---|
| 1 | Edit my.cnf or my.ini file |
| 2 | Set port under [mysqld] section |
| 3 | Save and close the file |
| 4 | Restart MySQL server |
| 5 | Connect using new port with --port option |
Key Takeaways
Change the port by setting the port option under [mysqld] in the MySQL config file.
Always restart the MySQL server after changing the port.
Update client connection commands to specify the new port.
Check firewall settings to allow traffic on the new port.
Verify the change by connecting with the new port number.