<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:51:50 UTC 2024

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary append 'field=key&field=summary' to the URL of your request.
-->
<rss version="0.92" >
<channel>
    <title>MongoDB Jira</title>
    <link>https://jira.mongodb.org</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>9.7.1</version>
        <build-number>970001</build-number>
        <build-date>13-04-2023</build-date>
    </build-info>


<item>
            <title>[JAVA-249] Java Driver cannot connect to &apos;localhost&apos; when mongod is listening only on 127.0.0.1 (or &quot;localhost&quot;)</title>
                <link>https://jira.mongodb.org/browse/JAVA-249</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;p&gt;Issue reported by user but reproducible by me.&lt;/p&gt;

&lt;p&gt;When working with the 2.4 Java driver on Mac OS X (10.6.x) , where mongod listens on bind_ip 127.0.0.1 (also tried with &apos;localhost&apos;), an empty Mongo constructor &lt;span class=&quot;error&quot;&gt;&amp;#91;Mongo()&amp;#93;&lt;/span&gt; connects.  However, connecting to a hostname of &quot;localhost&quot; fails with:&lt;/p&gt;

&lt;p&gt;com.mongodb.MongoInternalException: DBPort.findOne failed&lt;br/&gt;
	at com.mongodb.DBPort.findOne(DBPort.java:153)&lt;br/&gt;
	at com.mongodb.DBPort.runCommand(DBPort.java:159)&lt;br/&gt;
	at com.mongodb.DBTCPConnector.testMaster(DBTCPConnector.java:371)&lt;br/&gt;
	at com.mongodb.Mongo.&amp;lt;init&amp;gt;(Mongo.java:167)&lt;br/&gt;
	at com.mongodb.Mongo.&amp;lt;init&amp;gt;(Mongo.java:151)&lt;br/&gt;
	at com.mongodb.Mongo.&amp;lt;init&amp;gt;(Mongo.java:119)&lt;/p&gt;

&lt;p&gt;Adding an explicit port number makes no difference.&apos;&lt;/p&gt;

&lt;p&gt;User reports they see same behavior if they specify a hostname of &quot;127.0.0.1&quot; although on my machine with a homebrew sourced 1.6.3 and 1.6.5 it functions with &quot;127.0.0.1&quot;.  If I remove the bind_ip declaration it works fine.&lt;/p&gt;

&lt;p&gt;User reports behavior did not occur with the 2.1 Java driver &amp;#8212; I&apos;ve confirmed this.  Uncertain if it happens on other platforms yet.&lt;/p&gt;</description>
                <environment>Reproducible on Mac</environment>
        <key id="14161">JAVA-249</key>
            <summary>Java Driver cannot connect to &apos;localhost&apos; when mongod is listening only on 127.0.0.1 (or &quot;localhost&quot;)</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="3" iconUrl="https://jira.mongodb.org/images/icons/priorities/major.svg">Major - P3</priority>
                        <status id="6" iconUrl="https://jira.mongodb.org/images/icons/statuses/closed.png" description="The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.">Closed</status>
                    <statusCategory id="3" key="done" colorName="success"/>
                                    <resolution id="9">Done</resolution>
                                        <assignee username="jeff.yemin@mongodb.com">Jeffrey Yemin</assignee>
                                    <reporter username="brendan">Brendan W. McAdams</reporter>
                        <labels>
                    </labels>
                <created>Wed, 5 Jan 2011 16:57:43 +0000</created>
                <updated>Mon, 18 Jun 2012 19:36:09 +0000</updated>
                            <resolved>Mon, 14 May 2012 19:36:59 +0000</resolved>
                                    <version>2.3</version>
                    <version>2.4</version>
                                    <fixVersion>2.8.0</fixVersion>
                                                        <votes>2</votes>
                                    <watches>2</watches>
                                                                                                                <comments>
                            <comment id="134044" author="jeff.yemin" created="Mon, 18 Jun 2012 19:36:09 +0000"  >&lt;p&gt;Closing for 2.8.0 release.&lt;/p&gt;</comment>
                            <comment id="119295" author="auto" created="Mon, 14 May 2012 19:36:18 +0000"  >&lt;p&gt;Author:&lt;/p&gt;
{u&apos;login&apos;: u&apos;jyemin&apos;, u&apos;name&apos;: u&apos;Jeff Yemin&apos;, u&apos;email&apos;: u&apos;jeff.yemin@10gen.com&apos;}
&lt;p&gt;Message: &lt;a href=&quot;https://jira.mongodb.org/browse/JAVA-249&quot; title=&quot;Java Driver cannot connect to &amp;#39;localhost&amp;#39; when mongod is listening only on 127.0.0.1 (or &amp;quot;localhost&amp;quot;)&quot; class=&quot;issue-link&quot; data-issue-key=&quot;JAVA-249&quot;&gt;&lt;del&gt;JAVA-249&lt;/del&gt;&lt;/a&gt;: Removed calls to InetAddress.getLocalHost and InetAddress.getAllByName, using InetAddress.getByName instead&lt;br/&gt;
Simplified code.  Fixed Javadoc.  Removed unused methods.&lt;br/&gt;
Made it thread-safe by making _address volatile&lt;br/&gt;
Branch: master&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/mongo-java-driver/commit/3b12c2ae2c208c9e78f75128154e7ead52f00aa1&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-java-driver/commit/3b12c2ae2c208c9e78f75128154e7ead52f00aa1&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="118701" author="jeff.yemin" created="Fri, 11 May 2012 19:14:02 +0000"  >&lt;p&gt;I&apos;m going to fix this by always using the getByName method in InetAddress.&lt;/p&gt;</comment>
                            <comment id="98120" author="raman@vivosys.com" created="Tue, 13 Mar 2012 01:22:04 +0000"  >&lt;p&gt;Yes, InetAddress.getLocalHost() does not mean &quot;ip address associated with localhost&quot;. Rather, it means the &quot;ip address associated with the local i.e. current host&apos;s name&quot;. Typically, this means that the IP address will be resolved to the external interface and not 127.0.0.1, which is not what a user would expect.&lt;/p&gt;

&lt;p&gt;As noted by Jeff in the previous comment, there should not be an override for &quot;localhost&quot; to InetAddress.getLocalHost(). Rather InetAddress.getAllByName( host ); where host = &quot;localhost&quot; leaves the resolution of the name &quot;localhost&quot; to the OS, as the user would expect.&lt;/p&gt;

&lt;p&gt;I just ran into this issue on a new CentOS EC2 instance, so this is not confined to Mac&apos;s, and funnily enough just submitted a pull request to GitHub with the same exact change Jeff proposed a couple of days ago: &lt;a href=&quot;https://github.com/mongodb/mongo-java-driver/pull/65&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-java-driver/pull/65&lt;/a&gt;.&lt;/p&gt;</comment>
                            <comment id="97549" author="jeff.yemin" created="Sat, 10 Mar 2012 20:48:49 +0000"  >&lt;p&gt;Using localhost works fine (on a Mac at least) if I comment out the first 3 lines of ServerAddress._getAddress&lt;/p&gt;

&lt;p/&gt;
&lt;div id=&quot;syntaxplugin&quot; class=&quot;syntaxplugin&quot; style=&quot;border: 1px dashed #bbb; border-radius: 5px !important; overflow: auto; max-height: 30em;&quot;&gt;
&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot; width=&quot;100%&quot; style=&quot;font-size: 1em; line-height: 1.4em !important; font-weight: normal; font-style: normal; color: black;&quot;&gt;
		&lt;tbody &gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;  margin-top: 10px;   width: auto; padding: 0;&quot;&gt;&amp;nbsp;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: #006699; font-weight: bold; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt; ( host.toLowerCase().equals(&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;&quot;localhost&quot;&lt;/span&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;) ){&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;            &lt;/span&gt;&lt;span style=&quot;color: #006699; font-weight: bold; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #006699; font-weight: bold; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt; InetAddress[] { InetAddress.getLocalHost()};&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;        }&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;        &lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   margin-bottom: 10px;  width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: #006699; font-weight: bold; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt; InetAddress.getAllByName( host );&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
			&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p/&gt;

&lt;p&gt;InetAddress.getAllByName(&quot;localhost&quot;) will resolve to the loopback address, whereas InetAddress.getLocalHost() calls java.net.InetAddressImpl#getLocalHostName which does not always return &quot;localhost&quot;, in which case it will try to look up the local host name in DNS, which will typically fail on a dev machine.  For instance, on my dev machine it returns &quot;jmpb.home&quot;&lt;/p&gt;

&lt;p&gt;Another thing we may want to do is to try all the InetAddress instances returned by InetAddress.getAllByName, instead of just the first one, until we find one that works.&lt;/p&gt;</comment>
                            <comment id="22365" author="brendan" created="Fri, 14 Jan 2011 22:28:52 +0000"  >&lt;p&gt;Unfortunately, this is classified as either a Java-level or OS-level bug, and is not occurring within the Mongo driver.&lt;/p&gt;

&lt;p&gt;The resolution error occurs in the layer between Java and the Operating System as it resolves the hostname and can&apos;t be addressed in Mongo&apos;s Java code.&lt;/p&gt;</comment>
                            <comment id="22315" author="martin.grotzke" created="Thu, 13 Jan 2011 09:13:46 +0000"  >&lt;p&gt;We also encounter this on a gentoo, with java driver 2.4.&lt;/p&gt;

&lt;p&gt;Changing localhost to 127.0.0.1 works, however, it should be possible to be able to connect with &quot;localhost&quot; to 127.0.0.1.&lt;/p&gt;</comment>
                            <comment id="22104" author="brendan" created="Wed, 5 Jan 2011 17:58:42 +0000"  >&lt;p&gt;It turns out this is a quirk in the resolver on Mac and some Debians (per EH); localhost != 127.0.0.1 in these cases.&lt;/p&gt;

&lt;p&gt;We should report a better error message to clarify that we couldn&apos;t resolve the host.&lt;/p&gt;</comment>
                            <comment id="22102" author="brendan" created="Wed, 5 Jan 2011 17:29:04 +0000"  >&lt;p&gt;Confirmed as a Mac only bug, and it occurs in the java.net.InetSocketAddress constructor.&lt;/p&gt;

&lt;p&gt; Looking to see if there are any crucial differences in the Mac JDK impl of ISA versus stock Sun.&lt;/p&gt;</comment>
                            <comment id="22101" author="brendan" created="Wed, 5 Jan 2011 17:18:28 +0000"  >&lt;p&gt;The breakage is happening in line 43 of DBPort.java, the  getSocketAddress() call on ServerAddress.  I am confirming it doesn&apos;t break on other platforms and if it is mac only it is likely a bug on Mac&apos;s Java implementation.&lt;/p&gt;

&lt;p&gt;The getSocketAddress() call is returning a different address than input &amp;#8212; when it is called against a ServerAddress for &apos;localhost&apos; it instead of returning localhost/127.0.0.1:27017 like it should, returns &amp;lt;system-public-hostname&amp;gt;/&amp;lt;system-public-ip&amp;gt;:&amp;lt;port&amp;gt;&lt;/p&gt;

&lt;p&gt;If the MongoD is not listening on the public ip the connection fails.&lt;/p&gt;


&lt;p&gt;Here is the debug dump from a Scala console:&lt;/p&gt;

&lt;p&gt;scala&amp;gt; MongoConnection(&quot;127.0.0.1&quot;)       &lt;br/&gt;
SLF4J: Failed to load class &quot;org.slf4j.impl.StaticLoggerBinder&quot;.&lt;br/&gt;
SLF4J: Defaulting to no-operation (NOP) logger implementation&lt;br/&gt;
SLF4J: See &lt;a href=&quot;http://www.slf4j.org/codes.html#StaticLoggerBinder&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;http://www.slf4j.org/codes.html#StaticLoggerBinder&lt;/a&gt; for further details.&lt;br/&gt;
Addr: 127.0.0.1:27017 Socket Addr: /127.0.0.1:27017&lt;br/&gt;
res0: com.mongodb.casbah.MongoConnection = com.mongodb.casbah.MongoConnection@47845cb3&lt;/p&gt;

&lt;p&gt;scala&amp;gt; MongoConnection(&quot;localhost&quot;)       &lt;br/&gt;
Addr: localhost:27017 Socket Addr: b-mac.local/10.4.1.111:27017&lt;br/&gt;
com.mongodb.MongoInternalException: DBPort.findOne failed&lt;br/&gt;
	at com.mongodb.DBPort.findOne(DBPort.java:154)&lt;br/&gt;
	at com.mongodb.DBPort.runCommand(DBPort.java:160)&lt;br/&gt;
	at com.mongodb.DBTCPConnector.testMaster(DBTCPConnector.java:371)&lt;br/&gt;
	at com.mongodb.Mongo.&amp;lt;init&amp;gt;(Mongo.java:167)&lt;br/&gt;
	at com.mongodb.Mongo.&amp;lt;init&amp;gt;(Mongo.java:151)&lt;br/&gt;
	at com.mongodb.Mongo.&amp;lt;init&amp;gt;(Mongo.java:119)&lt;br/&gt;
	at com.mongodb.casbah.MongoConnection$.apply(MongoConnection.scala:150)&lt;br/&gt;
	at .&amp;lt;init&amp;gt;(&amp;lt;console&amp;gt;:9)&lt;br/&gt;
	at .&amp;lt;clinit&amp;gt;(&amp;lt;console&amp;gt;)&lt;br/&gt;
	at RequestResult$.&amp;lt;init&amp;gt;(&amp;lt;console&amp;gt;:9)&lt;br/&gt;
	at RequestResult$.&amp;lt;clinit&amp;gt;(&amp;lt;console&amp;gt;)&lt;br/&gt;
	at RequestResult$scala_repl_result(&amp;lt;console&amp;gt;)&lt;br/&gt;
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)&lt;br/&gt;
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)&lt;br/&gt;
	at sun.reflect.DelegatingMethodAccessorImpl...&lt;br/&gt;
scala&amp;gt; MongoConnection()                  &lt;br/&gt;
Addr: 127.0.0.1:27017 Socket Addr: /127.0.0.1:27017&lt;br/&gt;
res2: com.mongodb.casbah.MongoConnection = com.mongodb.casbah.MongoConnection@40199011&lt;/p&gt;

</comment>
                            <comment id="22100" author="brendan" created="Wed, 5 Jan 2011 17:03:17 +0000"  >&lt;p&gt;Reproduced issue on 2.3 java driver as well as 2.4; doesn&apos;t occur with 2.2 or earlier.&lt;/p&gt;</comment>
                            <comment id="22099" author="brendan" created="Wed, 5 Jan 2011 17:02:16 +0000"  >&lt;p&gt;Mongod log shows in the case of the exception thrown connect attempts no connection is made to mongod, so this isn&apos;t the command failing but the actual connection.&lt;/p&gt;</comment>
                    </comments>
                <issuelinks>
                            <issuelinktype id="10010">
                    <name>Duplicate</name>
                                                                <inwardlinks description="is duplicated by">
                                        <issuelink>
            <issuekey id="39936">JAVA-577</issuekey>
        </issuelink>
            <issuelink>
            <issuekey id="21961">JAVA-430</issuekey>
        </issuelink>
                            </inwardlinks>
                                    </issuelinktype>
                            <issuelinktype id="10012">
                    <name>Related</name>
                                            <outwardlinks description="related to">
                                        <issuelink>
            <issuekey id="15320">JAVA-310</issuekey>
        </issuelink>
                            </outwardlinks>
                                                        </issuelinktype>
                    </issuelinks>
                <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                        <customfield id="customfield_10011" key="com.atlassian.jira.plugin.system.customfieldtypes:radiobuttons">
                        <customfieldname>Backwards Compatibility</customfieldname>
                        <customfieldvalues>
                                <customfieldvalue key="10038"><![CDATA[Fully Compatible]]></customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hrhchj:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>14823</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>