I was testing a connection issue and tested port 3307 as specified in my JDBC URL. It worked, so I assumed it was on that port. Then I saw the default port was 3306 and tried random ports, and it still worked. I expected it to fail. Why is it ignoring the port on the command line?
$ mysql -u root --port 999 -h localhost gb
MariaDB [gb]> SHOW VARIABLES WHERE Variable_name = 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
$ mysql --version
mysql Ver 15.1 Distrib 10.1.30-MariaDB, for CYGWIN (i686) using EditLine wrapper
I also tried -P 999
and it worked.
How To Connect to MySQL Server on a Different Port? If your MySQL server is listening on port number different than 3306, you need to specify "--port=portNumber" option to any client program that needs to connect to the server.
Client - Server Connection PortsPort 3306 is the default port for the classic MySQL protocol ( port ), which is used by the mysql client, MySQL Connectors, and utilities such as mysqldump and mysqlpump.
You need to edit your mysql config file, my. cnf in /etc/my. cnf and change the port to 3306. Then restart mysql.
IIRC mysql connects you to a Unix socket if you are connecting to localhost. Since it does not connect you via TCP in this case, there is no port involved and the port number you give does not matter.
Under Linux or Unix, check your IP tables (or similar) configuration to ensure that the port has not been blocked. Under Windows, applications such as ZoneAlarm or Windows Firewall may need to be configured not to block the MySQL port. The grant tables must be properly set up so that the server can use them for access control.
Unfortunately, this application uses port 3306, and it has no method to specify a different port. MySQL default port is 3306. And it has good flexibility to change its port. So let us see how. Go to [path-to-xampp-folder]/mysql/bin/ and open the file my.ini. Or from Xampp, click on Config at the MySQL Module.
If the server was started with the bind_address system variable set to 127.0.0.1, it listens for TCP/IP connections only locally on the loopback interface and does not accept remote connections. Check to make sure that there is no firewall blocking access to MySQL.
It's ignoring the port because it's using a local socket connection, not a TCP/IP connection. That's because the host is specified as localhost
.
localhost
has special meaning in MySQL. It does not resolve to IP address 127.0.0.1 (like we might expect it to, based on our familiarity with that pattern.)
Behavior is documented in MySQL Reference Manual ...
excerpt from https://dev.mysql.com/doc/refman/5.7/en/connecting.html
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option. For example:
It is like spencer wrote, it's using a socket connection by default.
You can force TCP like that:
mysql -u root -p --port=3306 --protocol=TCP
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With