How to get MySQL's port on Mac through the terminal?

I started MySQL with its PrefPane in System Preferences:

enter image description here

When I enter ps aux | grep mysql I get the following output:

ldl              33675   0.0  0.0  2432804    772 s003  S+   11:01上午   0:00.00 grep mysql
_mysql           33474   0.0  0.1  2829668   8108   ??  Ss   10:55上午   0:00.54 /usr/local/mysql/bin/mysqld --user=_mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/mysqld.local.err --pid-file=/usr/local/mysql/data/mysqld.local.pid

But how do I know which port MySQL is using on my Mac? Entering lsof -i tcp:3306 I find nothing.


Solution 1:

There are a few options:

You can list the ports that are being "listened on" with netstat

netstat -ap tcp | grep -i "listen"

tcp4       0      0  localhost.4380         *.*                    LISTEN     
tcp4       0      0  localhost.4370         *.*                    LISTEN     
tcp4       0      0  localhost.5945         *.*                    LISTEN  
tcp4       0      0  localhost.25035        *.*                    LISTEN

(I only run MySQL on VMs so it won't show up on my Mac, but this illustrates the output)

You can also just look and see what port is configured for MySQL to use by examining the /etc/my.cnf file. Under the [mysqld] section.

[mysqld]
# Connection and Thread variables

port                           = 3306

Solution 2:

Assuming that mysql is on your $PATH then in the terminal:

mysql server status

Solution 3:

If you have turned on Apache on your Mac and also enabled PHP, you can run the phpinfo() command which will give you tons of information such as:

phpinfo() info about mysqli

Based on what I've read, 3306 is the default, so if you didn't specifically change it, then it's probably that.

You can also use MySQL Workbench from Oracle. When I launch it, it shows:

MySQL port shown here