Bug #75286 server accepts garbage port
Submitted: 22 Dec 2014 12:23 Modified: 22 Dec 2014 14:56
Reporter: Peter Laursen (Basic Quality Contributor) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Options Severity:S3 (Non-critical)
Version:5.7.5, 5.6.22 OS:Any
Assigned to: CPU Architecture:Any

[22 Dec 2014 12:23] Peter Laursen
Description:
http://dev.mysql.com/doc/refman/5.7/en/server-options.html#option_mysqld_port and http://dev.mysql.com/doc/refman/5.1/en/server-options.html#option_mysqld_port are clear that the max. port no. is 65535

But you can specify a larger number and the server starts. I am not able to connect to the server though. But we have a report from a customer who is able to connect to two servers (using Workbench) started with ports 562201 and 562202 specified. His exact server version is not clear and I am also not sure of the OS, he is using (if it matters). 

We have in our client programs implemented a client-side validation that port no. is specified in in the documented range. And user is not willing to accept that he can connect with Workbench an not with our programs.

How to repeat:
my.ini:
# The TCP/IP Port the MySQL Server will listen on
# port=3311
port=562201

C:\Program Files\MySQL\MySQL Server 5.7\bin>net start mysql57
Tjenesten MySQL57 starter.
Tjenesten MySQL57 er startet.

C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uroot -p --port=562201
Enter password: ********
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)

I don't claim that the server is actually listening on port 562201 (if such exists at all in the socket layer).  It may "overrun" a buffer several times (so that the real port used could be (562201 MOD 65535) for instance.

I have only checked with server 5.7.5 and don't know if older versions have  same issue.

Suggested fix:
Server should not start with an invalid port specified?
[22 Dec 2014 14:02] MySQL Verification Team
Hello Peter Laursen,

Thank you for the report.
Observed this behavior with 5.6.22 release build.

Thanks,
Umesh
[22 Dec 2014 14:04] MySQL Verification Team
// port conf

[root@cluster-repo mysql-advanced-5.6.22]# cat /etc/my.cnf|grep port
port=562201

// Restart mysqld

[root@cluster-repo mysql-advanced-5.6.22]# /etc/init.d/mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]

// Extract from error log

2014-12-24 20:02:19 28714 [Note] IPv6 is available.
2014-12-24 20:02:19 28714 [Note]   - '::' resolves to '::';
2014-12-24 20:02:19 28714 [Note] Server socket created on IP: '::'.
2014-12-24 20:02:19 28714 [Note] Event Scheduler: Loaded 0 events
2014-12-24 20:02:19 28714 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.6.22-log'  socket: '/var/lib/mysql/mysql.sock'  port: 562201  MySQL Community Server (GPL)

// Check the port on which it is listening
// So, may be that huge value must have wrapped around 64k a few times?

[root@cluster-repo mysql-advanced-5.6.22]# netstat -apn | grep mysqld
tcp        0      0 :::37913                    :::*                        LISTEN      28714/mysqld
unix  2      [ ACC ]     STREAM     LISTENING     10579056 28714/mysqld        /var/lib/mysql/mysql.sock

// connecting with port 37913/562201
[root@cluster-repo mysql-advanced-5.6.22]# mysql -u root -p --protocol=tcp --port=37913
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.22-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

[(none)]> show variables like 'port';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| port          | 562201 |
+---------------+--------+
1 row in set (0.00 sec)

[(none)]> \q
Bye
[root@cluster-repo mysql-advanced-5.6.22]# mysql -u root -p --protocol=tcp --port=562201
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.22-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

[(none)]> show variables like 'port';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| port          | 562201 |
+---------------+--------+
1 row in set (0.00 sec)

#### from remotely

[yyyyyyy@xxxxx]/export/umesh/mysql-5.6.22: bin/mysql -hx.x.x.x -u ushastry -p --port=562201
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.22-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like 'port';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| port          | 562201 |
+---------------+--------+
1 row in set (0.28 sec)

mysql> \q
Bye
[yyyyyyy@xxxxx]/export/umesh/mysql-5.6.22: bin/mysql -hx.x.x.x -u ushastry -p --port=37913
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.22-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like 'port';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| port          | 562201 |
+---------------+--------+
1 row in set (0.28 sec)

mysql>
[22 Dec 2014 14:22] Peter Laursen
The question is, if it is a recent issue/phenomenon?
[22 Dec 2014 14:39] MySQL Verification Team
// Similar behavior with 5.1.73

[root@cluster-repo mysql-5.1.73]# netstat -apn | grep mysqld
tcp        0      0 0.0.0.0:37913               0.0.0.0:*                   LISTEN      29768/mysqld
unix  2      [ ACC ]     STREAM     LISTENING     10580923 29768/mysqld        /tmp/mysql.sock

// Localhost

[root@cluster-repo mysql-5.1.73]# bin/mysql -u root -p --protocol=tcp --port=37913
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.73 MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

[(none)]> show variables like 'port';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| port          | 562201 |
+---------------+--------+
1 row in set (0.00 sec)

[(none)]> \q
Bye
[root@cluster-repo mysql-5.1.73]# bin/mysql -u root -p --protocol=tcp --port=37913
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.73 MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

[(none)]> GRANT ALL ON *.* TO 'ushastry'@'%';
Query OK, 0 rows affected (0.00 sec)

[(none)]>

// remotely

bin/mysql -hx.x.x.x -u ushastry -p --port=37913
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.1.73 MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like 'port';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| port          | 562201 |
+---------------+--------+
1 row in set (0.28 sec)

mysql>

bin/mysql -hx.x.x.x -u ushastry -p --port=562201
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.73 MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like 'port';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| port          | 562201 |
+---------------+--------+
1 row in set (0.28 sec)

mysql>
[22 Dec 2014 14:56] Peter Laursen
Ok .. so this is not new.  

The problem is that if a port > 64K is specified, it (as I understand it) wraps around 64K as many times as possible and uses the modulus (remains from last wrap possible). This may very well cause conflicts with another program or the system itself.

Also note that I cannot connect to the server on Windows on the specified  port, like you can on Lnux.