83

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.

Ankur
  • 2,369
  • 7
  • 22
  • 24

10 Answers10

104

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.

Andy
  • 5,190
  • 23
  • 34
Ankur
  • 2,369
  • 7
  • 22
  • 24
43

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.

sybreon
  • 7,357
  • 1
  • 19
  • 19
  • 2
    the -p flag only works on linux, afaik. (definitely not Mac OS X, at least) – jdizzle Mar 08 '10 at 04:48
  • 2
    guess that you're stuck with `lsof -i TCP` then. – sybreon Mar 09 '10 at 05:18
  • The Linux command and windows command vary slightly. For Windows, you must be elevated, and replace the p with a b – IceMage Feb 19 '19 at 18:14
  • `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*? – iCantC Feb 03 '20 at 09:58
  • i would also like to know the answer to the above question commentor's question @iCantC – frei Feb 25 '20 at 07:35
7

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.

eem
  • 103
  • 4
IgnacioAinol
  • 71
  • 1
  • 1
5

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.

Alex
  • 1,103
  • 6
  • 12
4

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.

Dave Cheney
  • 18,307
  • 7
  • 48
  • 56
3

An alternative method to the ones already listed (and not as good but hey, it works).

prompt>lsof -n | grep 'mysql.*TCP'
mysqld     1564     mysql   10u     IPv4            3246456       0t0        TCP *:mysql (LISTEN)
prompt>cat /proc/1564/net/tcp
  sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
   0: 11AC11AC:0035 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 14299 1 ffff88012a429a00 299 0 0 2 -1
   1: 017AA8C0:0035 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 13871 1 ffff880129962080 299 0 0 2 -1
   2: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 12596 1 ffff880129960000 299 0 0 2 -1
   3: 0100007F:0277 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 11459 1 ffff88012a429380 299 0 0 2 -1
   4: 00000000:8D58 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 11315 1 ffff88012a428d00 299 0 0 2 -1
   5: 00000000:0019 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 12940 1 ffff880129960680 299 0 0 2 -1
   6: 0100007F:177A 00000000:0000 0A 00000000:00000000 00:00000000 00000000   500        0 3600557 1 ffff8800672dee80 299 0 0 2 -1
   7: 0100007F:177B 00000000:0000 0A 00000000:00000000 00:00000000 00000000   500        0 3603871 1 ffff88012a42ee80 299 0 0 2 -1
   8: 0100007F:177C 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 3608169 1 ffff88012a42f500 299 0 0 2 -1
   9: 0100007F:177D 00000000:0000 0A 00000000:00000000 00:00000000 00000000   500        0 3615687 1 ffff88012a42e180 299 0 0 2 -1
  10: 0100007F:00C7 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 3608454 1 ffff8800672db400 299 0 0 2 -1
  11: 00000000:0CEA 00000000:0000 0A 00000000:00000000 00:00000000 00000000    27        0 3246456 1 ffff8800672dba80 299 0 0 2 -1
  12: 00000000:024B 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 12944 1 ffff880129961380 299 0 0 2 -1
  13: 00000000:006F 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 10657 1 ffff88012a428680 299 0 0 2 -1
  14: 00000000:01D1 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 12942 1 ffff880129960d00 299 0 0 2 -1
  15: 0100007F:1DD2 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 3631325 1 ffff8800672da700 299 0 0 2 -1
  16: 0100007F:990B 0100007F:177B 01 00000000:00000000 02:0002F5D8 00000000   500        0 3610110 2 ffff8800672df500 20 3 26 4 19
  17: 0100007F:177B 0100007F:990C 01 00000000:00000000 00:00000000 00000000   500        0 3610124 1 ffff88012a42d480 20 3 0 5 -1
  18: 0100007F:990D 0100007F:177B 01 00000000:00000000 02:00031144 00000000   500        0 3610142 2 ffff8800672d9380 20 3 0 5 -1
  19: 0100007F:177B 0100007F:990B 01 00000000:00000000 00:00000000 00000000   500        0 3610111 1 ffff8800672dc100 21 3 29 4 -1
  20: 0100007F:177B 0100007F:9949 01 00000000:00000000 00:00000000 00000000   500        0 3611026 1 ffff8800672dad80 20 3 0 5 -1
  21: 0100007F:9912 0100007F:177B 01 00000000:00000000 02:0005D3D3 00000000   500        0 3610249 2 ffff8800672de180 20 3 0 3 -1
  22: 0100007F:177B 0100007F:990D 01 00000000:00000000 00:00000000 00000000   500        0 3610143 1 ffff8800672de800 20 3 1 5 -1
  23: 11AC11AC:0016 480111AC:9074 01 00000000:00000000 02:0002BCFF 00000000     0        0 3608096 2 ffff88012a428000 20 3 1 5 16
  24: 11AC11AC:0016 480111AC:8485 01 00000000:00000000 02:0005819F 00000000     0        0 3615610 2 ffff88012a42c100 20 3 1 5 -1
  25: 0100007F:177B 0100007F:9923 01 00000000:00000000 00:00000000 00000000   500        0 3610494 1 ffff88012a42ce00 20 3 0 5 -1
  26: 0100007F:990F 0100007F:177B 01 00000000:00000000 02:0003117F 00000000   500        0 3610209 2 ffff8800672dd480 20 3 0 5 -1
  27: 0100007F:9949 0100007F:177B 01 00000000:00000000 02:0005D3D3 00000000   500        0 3611025 2 ffff8800672ddb00 20 3 0 3 -1
  28: 0100007F:90CF 0100007F:177B 01 00000000:00000000 02:00004637 00000000   500        0 4049147 2 ffff8800672dc780 20 3 1 5 -1
  29: 11AC11AC:0016 480111AC:C273 01 00000000:00000000 02:00021D06 00000000     0        0 3600488 4 ffff88012a42a700 20 6 31 4 34
  30: 0100007F:177B 0100007F:90CF 01 00000000:00000000 00:00000000 00000000   500        0 4049148 1 ffff8800672d8680 20 3 0 5 -1
  31: 11AC11AC:0016 480111AC:C7C3 01 00000000:00000000 02:00093A92 00000000     0        0 3603799 2 ffff88012a42ba80 20 3 26 5 31
  32: 0100007F:177B 0100007F:9912 01 00000000:00000000 00:00000000 00000000   500        0 3610250 1 ffff8800672da080 20 3 0 5 -1
  33: 0100007F:177B 0100007F:990F 01 00000000:00000000 00:00000000 00000000   500        0 3610210 1 ffff8800672d9a00 20 3 1 5 -1
  34: 0100007F:990C 0100007F:177B 01 00000000:00000000 02:00031147 00000000   500        0 3610123 2 ffff88012a42ad80 20 3 1 5 -1
  35: 0100007F:9923 0100007F:177B 01 00000000:00000000 02:0005D3D3 00000000   500        0 3610493 2 ffff88012a42e800 20 3 16 3 -1
prompt>grep '^mysql:' /etc/passwd
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
prompt>

The uid 27 line is the mysqld process' listening socket, and 0CEA is hex for 3306.

hlovdal
  • 1,075
  • 11
  • 18
1

Ok, this works on my linux box, but I'm not sure that Unix will store the cnf file in the same place.

cat /etc/mysql/my.cnf | grep 'port'

jonfhancock
  • 236
  • 2
  • 6
0

If you try this: $ grep port /etc/mysql/mysql.conf.d/mysqld.cnf

you will see which port you are looking for.

0

you may try tailing the mysql log....try running

shell$> tail -f /var/log/mysql/mysqld.log

while restarting mysql by

service mysqld stop/start

on another ssh terminal/tab. The port on which its listening will be displayed in the log as follows:

Version: '5.7.21-log'  socket: '/tmp/mysql.sock'  port: 3307  MySQL Community Server (GPL)
RalfFriedl
  • 3,008
  • 4
  • 12
  • 17
-2

above commands did not help me in my mac.

I used lsof -i TCP:3306 to find out the mysqld process. which was actually listenting into *.mysql