{"id":4519,"date":"2015-04-13T14:32:52","date_gmt":"2015-04-13T06:32:52","guid":{"rendered":"http:\/\/rmohan.com\/?p=4519"},"modified":"2015-04-13T14:32:52","modified_gmt":"2015-04-13T06:32:52","slug":"disable-ipv6-on-windows","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=4519","title":{"rendered":"disable ipv6 on windows"},"content":{"rendered":"<p><strong>MySQL client\u00a0fails to\u00a0connect to\u00a0localhost on Windows, due to IPv6<\/strong><br \/>\nA MySQL client running on Windows 2008 or Windows 7 may fail to\u00a0connect to\u00a0the host name of &#8220;localhost&#8221;. This is due to windows resloving the host name of localhost to the IPv6 loopback address of ::1. Windows will resolve localhost to ::1, even if IPv6 is disabled on all local network adapters.<\/p>\n<p><strong>MySQL IPv6 status<\/strong><br \/>\nMySQL Server version\u00a05.5.3 (March 2010) and later, support IPv6 connections to localhost, using the ::1 IPv6 address. So recent\u00a0MySQL installations can at least listen for IPv6 client connections. But previous versions of MySQL server interpret ::1 as a string or host name, rather than an IP address.<\/p>\n<p>On the client software side, things are not as simple.<br \/>\nWe have the official MySQL Connector client libraries of connector\/j, connector\/odbc, connector\/net, connector\/c, connector\/c++, connector\/mxj, and the MySQL-PHP native driver (mysqlnd).\u00a0On PHP we have ext\/mysql,\u00a0mysqli and pdo_mysql. ColdFusion\u00a0uses its own MySQL client, with a different version of that client in each version of ColdFusion (mx, 7, 8, 9). Then there are\u00a0many third party\u00a0MySQL client libraries for Perl, ASP.Net, etc, etc, etc.<\/p>\n<p>You just can&#8217;t trust the client to support IPv6.\u00a0Only a few MySQL client libraries currently\u00a0support IPv6 connections, and of those that do support IPv6, only the most recent versions may properly support IPv6.<\/p>\n<p><strong>The localhost problem<\/strong><br \/>\nSo here is where we run into a problem&#8230;<\/p>\n<p>By default, Windows 7 and Windows 2008 R2 resolve the localhost host name to the ::1 IPv6 loopback\u00a0address, rather than the 127.0.0.1\u00a0IPv4 loopback address. Windows will resolve &#8220;localhost&#8221; to ::1, even if you have disabled IPv6 on all of the installed network addapters.<\/p>\n<p>This doesn&#8217;t just affect MySQL. This affects any TCP\/IP client\/server program that may use &#8220;localhost&#8221; as a connection parameter.<\/p>\n<p>As you can see in the image below (mouse over images, for larger image) a &#8220;Ping localhost&#8221; command, on a default w2k8 r2 install, returns the IPv6 ::1 loopback address.<br \/>\n<img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/ping-ipv6.png.ashx?width=75\" alt=\"\" \/><\/p>\n<p>The images below show that ping localhost returns the ::1\u00a0IPv6 address, even when IPv6 is disabled on all local network adapters.<br \/>\n<img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/ipv6-eth0.png.ashx?width=75\" alt=\"\" \/><span class=\"Apple-converted-space\">\u00a0<\/span><img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/ipv6-eth1.png.ashx?width=75\" alt=\"\" \/><span class=\"Apple-converted-space\">\u00a0<\/span><img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/ping-ipv6.png.ashx?width=75\" alt=\"\" \/><\/p>\n<p><strong><br \/>\nFixing localhost IPv4 resolution<\/strong><br \/>\nSo we&#8217;ve decideded that we don&#8217;t like this sneaky IPv6 result for localhost resolution. The software that we are running prefers that\u00a0localhost\u00a0resolve to trusty old 127.0.0.1.<br \/>\nWhat to do?<\/p>\n<p>We can\u00a0correct this behavior\u00a0in the Windows Hosts file, or by using the Windows registry to\u00a0modify how IPv6 works or disable IPv6 altogether. My personal preference is to use the Windows hosts file to specifically map localhost to 127.0.0.1<span class=\"Apple-converted-space\">\u00a0<\/span><strong><em>and<\/em><\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>to modify the IPv4\/IPv6 resolution preference.<\/p>\n<p><strong>Via Windows Hosts file<\/strong><br \/>\nFirst, let&#8217;s take a look at the default\u00a0Windows\u00a0hosts file, at C:\\Windows\\System32\\drivers\\etc\\hosts (open with notepad)<br \/>\nIn previous versions of Windows, we find the following active entry for &#8220;localhost&#8221;<br \/>\n127.0.0.1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 localhost<\/p>\n<p>But in Windows 7 and Windows 2008 R2, the hosts file is effectively empty. While there are both IPv4 and IPv6 entries for &#8220;localhost&#8221;, both are disabled by being\u00a0commented out.<br \/>\n#\u00a0127.0.0.1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 localhost<br \/>\n#\u00a0::1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 localhost<\/p>\n<p>The image below shows the default hosts file, on a clean install of w2k8 r2.<br \/>\n<img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/ipv6-hosts-w2k8-default.png.ashx?width=75\" alt=\"\" \/><\/p>\n<p>If both &#8220;localhost&#8221; entries are disabled (commented out), localhost will resolve to the ::1 IPv6 address.<br \/>\nIf both &#8220;localhost&#8221; entries are enabled (active), localhost will<span class=\"Apple-converted-space\">\u00a0<\/span><strong><em>still<\/em><\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>resolve to the ::1 IPv6 address.<br \/>\n<img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/ipv6-hosts-w2k8-default.png.ashx?width=75\" alt=\"\" \/><span class=\"Apple-converted-space\">\u00a0<\/span><img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/hosts-w2k8-both.png.ashx?width=75\" alt=\"\" \/><span class=\"Apple-converted-space\">\u00a0<\/span><img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/ping-ipv6.png.ashx?width=75\" alt=\"\" \/><span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>If we<span class=\"Apple-converted-space\">\u00a0<\/span><strong><em>only enable the 127.0.0.1 entry for localhost<\/em><\/strong>, then we get the proper IPv4 resolution for a ping of localhost.<br \/>\n<img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/hosts-w2k8-ipv4.png.ashx?width=75\" alt=\"\" \/><span class=\"Apple-converted-space\">\u00a0<\/span><img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/ping-ipv4.png.ashx?width=75\" alt=\"\" \/><span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p><strong>Via Windows Registry<\/strong><br \/>\nWe can modify the behavior of Windows IPv6 from the &#8220;HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\TCPIP6\\Parameters\\&#8221; registry key. The entry we are interested in is named &#8220;DisabledComponents&#8221;. But the DisabledComponents entry probably doesn&#8217;t exist on your system. So we need to create a new dword entry named &#8220;DisabledComponents&#8221;.<\/p>\n<p>You can now disable IPv6 support by setting the value of DisabledComponents to a hex value of ffffffff (that&#8217;s\u00a08 &#8220;f&#8217;s&#8221;). After you reboot, a ping of localhost will return 127.0.0.1, even if you don&#8217;t touch the hosts file.<br \/>\n<img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/ipv6-registry-disabled.png.ashx?width=75\" alt=\"\" \/><span class=\"Apple-converted-space\">\u00a0<\/span><img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/ipv6-hosts-w2k8-default.png.ashx?width=75\" alt=\"\" \/><span class=\"Apple-converted-space\">\u00a0<\/span><img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/ping-ipv4.png.ashx?width=75\" alt=\"\" \/><span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>But instead of completely disabling IPv6 support in Windows, you can just tell Windows to prefer IPv4 over IPv6. You can do this by setting the value of DisabledComponents to a hex value of 20. After you reboot, a ping of localhost will return 127.0.0.1, even if you don&#8217;t touch the hosts file.<br \/>\n<img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/ipv6-registry-ordered.png.ashx?width=75\" alt=\"\" \/><span class=\"Apple-converted-space\">\u00a0<\/span><img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/ipv6-hosts-w2k8-default.png.ashx?width=75\" alt=\"\" \/><span class=\"Apple-converted-space\">\u00a0<\/span><img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/ping-ipv4.png.ashx?width=75\" alt=\"\" \/><span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>My personal preference is a belt and suspenders approach to make sure that localhost resolves to 127.0.0.1, while trying not to break other (or future) Windows features by ripping out IPv6. I set a 127.0.0.1 IPv4 address in the hosts file<span class=\"Apple-converted-space\">\u00a0<\/span><strong><em>and<\/em><\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>I set DisabledComponents to a hex value of 20, to adjust the IPv4\/IPv6 preference.<br \/>\n<img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/ipv6-registry-ordered.png.ashx?width=75\" alt=\"\" \/><span class=\"Apple-converted-space\">\u00a0<\/span><img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/hosts-w2k8-ipv4.png.ashx?width=75\" alt=\"\" \/><span class=\"Apple-converted-space\">\u00a0<\/span><img decoding=\"async\" src=\"http:\/\/www.victor-ratajczyk.com\/images\/ipv6-localhost\/ping-ipv4.png.ashx?width=75\" alt=\"\" \/><span class=\"Apple-converted-space\"><br \/>\n<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL client fails to connect to localhost on Windows, due to IPv6 A MySQL client running on Windows 2008 or Windows 7 may fail to connect to the host name of &#8220;localhost&#8221;. This is due to windows resloving the host name of localhost to the IPv6 loopback address of ::1. Windows will resolve localhost to [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/4519"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4519"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/4519\/revisions"}],"predecessor-version":[{"id":4522,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/4519\/revisions\/4522"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}