How to check what port mysql is running on

509,536

Solution 1

I did

mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';

And that indicated that I was using port 3306 and that my search for the error continues.

Solution 2

The best way to actually know what application is listening to which interface and on what port is to use netstat

You can do this as root:

netstat -tlnp

It will list out all the listening services like this:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      25934/mysqld
tcp6       0      0 :::22                   :::*                    LISTEN      7964/dropbear

The last column shows you that mysqld bound itself to port 3306 listening on all interfaces.

In fact, this works for everything, not just mysql. You can also use it non TCP sockets.

Solution 3

Enter via terminal to mysql:

mysql -u root

and then type the following in the mysql prompt:

mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';

This worked for me.

Solution 4

If you really want to confirm that it is running on the port you can telnet into the port while the process is up like so:

telnet localhost 3306

You'll see it report that you're connected to mySQL.

Alernatively, you can find the process's PID using ps and grep:

ps -ef | grep mysql

and then put that pid into lsof to print out all the open file descriptors. You'll find the port the process is bound to near the top.

Solution 5

MySQL defaults to port 3306 unless you specify another line in the /etc/my.cnf config file.

Unless your /etc/my.cnf contains something like

[mysqld]
port = 3308

Then it is very likely you are using the default port.

Share:
509,536

Related videos on Youtube

Ankur
Author by

Ankur

Junior BA. Sometimes called upon to do programming and deployment.

Updated on September 17, 2022

Comments

  • Ankur
    Ankur over 1 year

    On my windows dev box mysql is running on port 3306

    How can I check what port it is running on the unix server that I have to upload the app to.

    • jonfhancock
      jonfhancock about 14 years
      That depends on how you can access the server. Do you have ssh access, mysql client access, or some web interface like phpMyAdmin?
    • Ankur
      Ankur about 14 years
      ssh access - through putty
    • John Gardeniers
      John Gardeniers about 14 years
      Is this a shared host? If so, there may well be multiple instances of MySQL running on it, each on a different port.
    • Ankur
      Ankur about 14 years
      No it's a VM setup just for me, so there's only one instance.
  • jdizzle
    jdizzle about 14 years
    the -p flag only works on linux, afaik. (definitely not Mac OS X, at least)
  • sybreon
    sybreon about 14 years
    guess that you're stuck with lsof -i TCP then.
  • IceMage
    IceMage about 5 years
    The Linux command and windows command vary slightly. For Windows, you must be elevated, and replace the p with a b
  • Naveed Abbas
    Naveed Abbas about 5 years
    This command can give you PID if the port number is the default 3306. Original Poster (OP) had a non-default number.
  • arunkvelu
    arunkvelu about 5 years
    This command helps to find out all global variables. mysql> SHOW GLOBAL VARIABLES;
  • Bruno Wego
    Bruno Wego about 5 years
    The correct way is using: sudo lsof -i :3306.
  • iCantC
    iCantC about 4 years
    lsof -i TCP works and shows mysqld 777 aajapfmyd 45i IPv6 099899bafe45 0t0 TCP localhost:33060 (LISTEN) on mac but why the port shown is 33060 instead of 3060?