How to Change MySQL Port on Windows: Step-by-Step Configuration Guide


16 views


When attempting to change MySQL's default port (3306) on Windows, many developers encounter the challenge of missing configuration files. MySQL searches for configuration in this order:

  • %WINDIR%\my.ini
  • %WINDIR%\my.cnf
  • C:\my.ini
  • C:\my.cnf
  • INSTALLDIR\my.ini
  • INSTALLDIR\my.cnf

When no configuration files exist (common with newer MySQL installations), you'll need to create one. Here's the proper way to create a working configuration:

# Example my.ini configuration
[client]
port=44999

[mysqld]
port=44999
# Important: Add your existing configuration here
basedir="C:/Program Files/MySQL/MySQL Server 5.5/"
datadir="C:/ProgramData/MySQL/MySQL Server 5.5/Data"

After creating/modifying the my.ini file, you must properly restart the MySQL service:

# Command line method (admin privileges required)
net stop MySQL55
net start MySQL55

# Alternative using PowerShell
Restart-Service -Name MySQL55 -Force

To confirm MySQL is listening on the new port:

# Using command line
netstat -ano | findstr 44999

# MySQL client verification
mysql --port=44999 -u root -p

If the service fails to start after port change:

  1. Check Windows Event Viewer for MySQL errors
  2. Verify file permissions on my.ini (Administrators: Full Control)
  3. Ensure no other services are using the new port
  4. Add explicit bind-address if needed:
[mysqld]
port=44999
bind-address=127.0.0.1

For persistent configuration, modify the service definition:

# First find the current service binary path
sc qc MySQL55

# Then update with new configuration file path
sc config MySQL55 binPath= "\"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqld\" --defaults-file=\"C:\Program Files\MySQL\MySQL Server 5.5\my.ini\" MySQL55"


Many Windows users struggle to find where MySQL actually reads its configuration. Unlike Linux systems where files are typically in /etc/my.cnf, Windows installations can be more ambiguous. MySQL searches these locations in order:

1. %WINDIR%\my.ini
2. %WINDIR%\my.cnf 
3. C:\my.ini
4. C:\my.cnf
5. INSTALLDIR\my.ini
6. INSTALLDIR\my.cnf

For MySQL 5.5+ installations, the most reliable location is typically:

C:\ProgramData\MySQL\MySQL Server 5.5\my.ini

The minimal configuration needed to change ports requires these sections:

[client]
port=44999

[mysqld]
port=44999

For a production server, you should also specify:

[mysqld]
port=44999
bind-address=0.0.0.0
max_connections=200

After modifying the configuration:

  1. Stop MySQL service: net stop MySQL55
  2. Verify no mysqld process remains in Task Manager
  3. Start MySQL service: net start MySQL55

Check active ports using PowerShell:

Get-NetTCPConnection -LocalPort 44999

Or test connection from MySQL client:

mysql --port=44999 -u root -p

Create a new firewall rule for the custom port:

New-NetFirewallRule -DisplayName "MySQL Custom Port" -Direction Inbound -LocalPort 44999 -Protocol TCP -Action Allow

If MySQL reverts to 3306:

  • Check for duplicate my.ini files
  • Verify file permissions (Administrators: Full Control)
  • Inspect error logs: C:\ProgramData\MySQL\MySQL Server 5.5\Data\*.err