Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't MySQL client use the port I specify?

Tags:

mysql

mariadb

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.

like image 581
Chloe Avatar asked May 03 '18 16:05

Chloe


People also ask

How do I connect to a specific port in MySQL?

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.

What port does MySQL client use?

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.

How can I change MySQL port from 0 to 3306?

You need to edit your mysql config file, my. cnf in /etc/my. cnf and change the port to 3306. Then restart mysql.

What port does MySQL use to connect to?

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.

Why can't I Access MySQL on my server?

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.

How to change the default MySQL port 3306?

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.

Why is my MySQL server not listening to TCP/IP connections?

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.


Video Answer


2 Answers

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:

like image 109
spencer7593 Avatar answered Oct 01 '22 19:10

spencer7593


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
like image 20
maio290 Avatar answered Oct 01 '22 20:10

maio290