MySQL: Change Default Port Under Linux / UNIX

Q.I’m using MySQL 5 under Red Hat Enterprise Linux 5. How do I change default mysql server port (tcp port 3306) under Linux or UNIX operating systems?

ADVERTISEMENTS

A. MySQL server and client uses a file called my.cnf. You need to open /etc/my.cnf (Global mysqld configuration file) to specify new port.

MySQL Change Default Port

Open /etc/my.cnf file:
# vi /etc/my.cnf
Set new port 5123:
port=5123
Here is is my sample /etc/my.cnf file:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
port=5123
old_passwords=1
bind = 10.10.29.66
key_buffer = 500M
table_cache = 4000
sort_buffer_size = 3M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
max_connections = 400
query_cache_type = 1
query_cache_limit = 1M
query_cache_size = 100M

max_allowed_packet = 1M
thread_cache_size = 8

# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 4
local-infile=0

[mysql.server]
user=mysql
basedir=/var/lib

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[mysqldump]
quick
max_allowed_packet = 16M

Save and close the file. Restart mysqld:
# service mysqld restart
Please note that once port changed, you need to update all your php, perl, python scripts including iptables scripts.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
11 comments… add one
  • Anjanesh Aug 29, 2008 @ 16:48

    It would’ve been better if it had been a post on how to setup a different version of MySQL running on a different port.

    For example, I have MySQL 5.0.51b on mu Ubuntu box in the default location by sudo apt-get mysql.

    I now want to install MySQL 6 for testing purposes – yet at the same time for all users of my machine running on say, 3307.

    This way I could give the option to users to use both the stable and a dev version.

  • Jon May 28, 2009 @ 13:43

    Hi,

    I wish to set it to listen on port 80, or 443 but it wont start? Nothing is listening on those ports now. What could be wrong? I have heard it can have to do with services settings? Any ideas?

    Best regards – Jon

  • Jon May 28, 2009 @ 13:53

    Well,

    It will not start with the settings I have now
    port = 443.

    Any advice?

    /jon

    • TryME Feb 21, 2011 @ 13:47

      443 port is also used by HTTPS (secure web server). So try other port.

  • alduxx Sep 4, 2009 @ 11:14

    @Jon

    To use this port you need root privileges.
    try:

    sudo /etc/init.d/mysql restart

  • brrrr Nov 5, 2011 @ 21:39

    I have port = 3306 under [client] and [mysqld]

    what’s the difference? which should I change?

  • xinzhong Jul 27, 2013 @ 17:01

    Hi I have a problem for MySQL.

    I have MySQL (5.6, 64bit) installed in windows 7, also have mysql installed in virtualbox Ubuntu (v12). Without Windows MySQL installed, I can access Ubuntu MySQL locally or remotely by MySQL workbench. After Windows MySQl installed ( not started), I cannot connect to Ubuntu MySQL locally and remotely, even with different port numbers ( 3306 for windows and 5123 for Ubuntu).

    Any helps will be appreciated.

    Thanks!

  • Ian Dec 9, 2013 @ 23:55

    Change [mysqld]

  • himmat Feb 22, 2014 @ 6:11

    i was working on wamp server so the port for mysql 3306 is occupied by Wamp…i want nort to uninstall this…how can i resolve this.

  • Sunil Jul 11, 2016 @ 9:58

    Now I am able to connect via 3306 and new port 1234 both.
    I want to disable port 3306 for mysql and keep running on 1234. How can I do that?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.