{"id":1460,"date":"2023-10-18T08:01:28","date_gmt":"2023-10-18T07:01:28","guid":{"rendered":"https:\/\/editjournal.redakt.eu\/faxmodem\/?p=1460"},"modified":"2023-10-18T08:01:28","modified_gmt":"2023-10-18T07:01:28","slug":"mysql-remote-port-forwarding","status":"publish","type":"post","link":"https:\/\/editjournal.redakt.eu\/faxmodem\/blog\/servers\/mysql-remote-port-forwarding\/","title":{"rendered":"Forwarding MySQL from local port to remote server"},"content":{"rendered":"<p>If you have access to a MySQL server which allows whitelisting only a few IP addresses and you need to allow a whole range, you can use a tiny separate &quot;proxy&quot; VPS that would receive the connections and forward them to your MySQL server. You will then only have to whitelist the &quot;proxy&quot; VPS IP with the MySQL server. And then you can whitelist all you want using firewall on that VPS.<\/p>\n<p>An example would be connecting Google Data Studio (which requires whitelisting a whole \/23 range) to Aruba MySQL DBaaS (which on a cheaper shared plan allows whitelisting only two IPv4 addresses).<\/p>\n<p>What won't work:<\/p>\n<ul>\n<li>SSH tunnels require an underlying SSH connection, which Google won't do here<\/li>\n<li><code>stunnel<\/code> is only for SSL or HTTPs<\/li>\n<\/ul>\n<p>What will work:<\/p>\n<blockquote>\n<p><strong>Use socat as a TCP port forwarder:<\/strong><br \/>\nFor multiple connections, use the fork option as used in the examples below:<\/p>\n<pre><code>socat TCP4-LISTEN:81,fork,reuseaddr TCP4:TCP4:192.168.1.10:80<\/code><\/pre>\n<p>This example listens on port 81, accepts connections, and forwards the connections to port 80 on the remote host.<\/p>\n<\/blockquote>\n<p>Except watch out: the above documentation has a typo where it lists <code>TCP4:TCP4<\/code> twice. That will result in an error:<\/p>\n<blockquote>\n<p>socat[7877] E TCP4: wrong number of parameters (3 instead of 2)<\/p>\n<\/blockquote>\n<p>The correct command is:<\/p>\n<pre><code>socat TCP4-LISTEN:3307,fork,reuseaddr TCP4:1.2.3.4:3306 &<\/code><\/pre>\n<ul>\n<li>3307 is the local port on your &quot;proxy&quot; VPS<\/li>\n<li><code>1.2.3.4:3306<\/code> is the remote MySQL server IP address and port<\/li>\n<\/ul>\n<p>On a Debian VPS, install <code>socat<\/code> using:<\/p>\n<pre><code>apt-get install socat<\/code><\/pre>\n<p>Sources:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.redhat.com\/sysadmin\/getting-started-socat\">https:\/\/www.redhat.com\/sysadmin\/getting-started-socat<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/30897533\/netcat-for-mysql-connection-forwarding\">https:\/\/stackoverflow.com\/questions\/30897533\/netcat-for-mysql-connection-forwarding<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>If you have access to a MySQL server which allows whitelisting only a few IP addresses and you need to allow a whole range, you can use a tiny separate &quot;proxy&quot; VPS that would receive the connections and forward them to your MySQL server. You will then only have to whitelist the &quot;proxy&quot; VPS IP&hellip; <a class=\"more-link\" href=\"https:\/\/editjournal.redakt.eu\/faxmodem\/blog\/servers\/mysql-remote-port-forwarding\/\">Continue reading <span class=\"screen-reader-text\">Forwarding MySQL from local port to remote server<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[956],"tags":[775,720,1138],"class_list":["post-1460","post","type-post","status-publish","format-standard","hentry","category-servers","tag-debian","tag-mysql","tag-socat","entry"],"_links":{"self":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/comments?post=1460"}],"version-history":[{"count":1,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1460\/revisions"}],"predecessor-version":[{"id":1463,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1460\/revisions\/1463"}],"wp:attachment":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/media?parent=1460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/categories?post=1460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/tags?post=1460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}