Tuesday 22 August 2017

How to change MySQL port

Few days back I was working on a request to change the port for MySQL instance. I thought by writing a blog post would help other folks who are new to the MySQL database platform. This is not something new in MySQL world, but for me it was a new learning.
By default MySQL will be listening to port 3306. From security standpoint it’s not a good idea to run your instance with default port. To change the port all you have to do is edit the configuration file.

In Windows -> my.ini
In Linux -> my.cnf

Ok. Next question is, where is my configuration file? How can I locate it?

In windows

  • Open services.msc console. Look for your MySQL service
  • Right click MySQL service and open ‘Properties’.
  • Under ‘Path to execute’ you can find the my.ini location


"C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqld.exe" --defaults-file="C:\ProgramData\MySQL\MySQL Server 5.6\my.ini" MySQL56

























  • Open the configuration file and search for port, by default its 3306












  • I have changed it to 3333













You have to restart MySQL service to make the changes effective.
Let’s check if MySQL instance is listening to 3333 port. To check the port you can use below command from MySQL client.

SHOW VARIABLES WHERE VARIABLE_NAME LIKE 'PORT';

















In Linux, configuration will be located in /etc/mysql/my.cnf. You have to open and follow the same process.

Note: Take backup of configuration file before making any changes

Hope this helps

Cheers,
Naveen