mongo couldn't connect to [localhost/127.0.0.1:27017] - java

I'm getting the following error:
נוב 08, 2013 12:05:46 PM com.mongodb.DBTCPConnector initDirectConnection
WARNING: Exception executing isMaster command on localhost/127.0.0.1:27017
java.io.IOException: couldn't connect to [localhost/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect
at com.mongodb.DBPort._open(DBPort.java:214)
at com.mongodb.DBPort.go(DBPort.java:107)
at com.mongodb.DBPort.go(DBPort.java:88)
at com.mongodb.DBPort.findOne(DBPort.java:143)
at com.mongodb.DBPort.runCommand(DBPort.java:148)
at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:548)
at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:527)
at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:277)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:257)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:310)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:295)
at com.mongodb.DB.getCollectionNames(DB.java:412)
at Main.Main.main(Main.java:26)
my code is simple (my first time with mongo):
MongoClient Client = new MongoClient( "localhost" , 27017 );
DB db = Client.getDB("qw");
DBCollection coll[] = new DBCollection[4];
Set<String> colls = db.getCollectionNames();
for(String s: colls)
System.out.println(s);
what is the problem?

Possibly you haven't started the Mongo server.
Open a shell and type
mongod
On the file system, you can start it from $MONGO_INSTALL_PATH/bin/mongod.
Don't close the shell and then try to run your code again.
More info:
Manage the mongod process

Try restarting your mongo. I had the same problem, and restarting solved it for me.

You haven't started your MongoDB Server.
First start your mongodb server and then run your code.
Or
You can also create MongoDB service which will always be running in the background, so from next time onwards you don't have to start the MongoDB server.
Here are the steps to create MongoDB service:
Create a folder named ‘log’ parallel to ‘data’ folder, inside mongodb folder.
Copy the ‘mongo.config’ file parallel to log folder inside ‘mongodb’ folder.
Here is the content for your mongo.config file:
stores data here
dbpath = your_drive\mongodb-win32-x86_64-2.2.3\data\db
all output goes here
logpath = your_drive\mongodb-win32-x86_64-2.2.3\log\mongo.log
log read and write operations
diaglog = 3
Create a file MongoServer.bat,
Here is the content for MongoServer.bat
your_drive\mongodb-win32-x86_64-2.2.3\bin\mongod.exe --config "your_drive\mongodb-win32-x86_64-2.2.3\mongo.config"
On your command prompt, go to your mongodb\bin directory and then write this command,
mongod --config your_Drive\mongodb-win32-x86_64-2.2.3\mongo.config –install
This will create the service named as ‘Mongo DB’.
To start the service, type on command prompt
net start MongoDB.
Hence, your service is created. Now you can perform your task.
For further information, you can visit website.

Probably not your problem, but I've seen this Exception raised when mongodb is refusing the connection because it hit a limit on the number of open connections. Check your mongodb log file for a statement like:
"connection refused because too many open connections".
This can mean you're leaving connections open, or you need to raise the limit on number of open files in the db server.

Seems to have the same problem when using the below code to the connection
MongoClient mongo = new MongoClient("localhost", 27017);
But worked out when changed to
MongoClient mongo = new MongoClient("127.0.0.1", 27017);

Related

SQL Developer How Fix issue ? "Status : Failure -Test failed: IO Error: The Network Adapter could not establish the connection"

I have installed sql developer oracle on windows 10, but when I try to create new databse and connect to it I have problem "Status : Failure -Test failed: IO Error: The Network Adapter could not establish the connection" , Something is missed in my installation list setups.
but when I try to create new databse and connect to it I have problem
...Something is missed in my installation list setups
So, keywords, 'create new database' -
That screen does not CREATE a database. It only allows you to create a connection to an existing database.
So you need to get a database first.
You have some free and easy options, which I talk about here.
Also, can you share your installation setup list, so we can see what you've done? It's possible you DO have a database.
But, seeing you're trying to connect as 'root' - I doubt it. That is an OS user, not an Oracle DB user - which on a new system would be something like 'SYS' or 'SYSTEM.'

Java with MongoDb IP connecting issue

I write a Java program to connect MongoDb and write some data into it. For example I set up the MongoDb on 192.168.1.95 and run my program on that server. When I use the following code:
MongoClient mongo = new MongoClient("localhost", 27017);
It is working and no any problem. However, if I use
MongoClient mongo = new MongoClient("192.168.1.95", 27017);
It always throw a exception for me:
Exception in thread "main" com.mongodb.MongoException: unauthorized
at com.mongodb.CommandResult.getException(CommandResult.java:100)
at com.mongodb.CommandResult.throwOnError(CommandResult.java:134)
at com.mongodb.DBTCPConnector._checkWriteError(DBTCPConnector.java:142)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:183)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:155)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:270)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:226)
at com.mongodb.DBCollection.insert(DBCollection.java:75)
at com.mongodb.DBCollection.insert(DBCollection.java:59)
at com.mongodb.DBCollection.insert(DBCollection.java:104)
at com.starscriber.mongoCluster.Main.main(Main.java:29)
Why cannot I use the exactly Ip instead of "localhost"?? And I am pretty sure that I don't need any username and password to log into the MongoDb
Sounds like auth=true in your MongoDB server configuration. If so, you need to log in to your server by connecting over the localhost interface for the first time to create user credentials. For more info see section "Security Considerations" in the Run-time Database Configuration page of the MongoDB manual

Cannot connect to embedded Cassandra

I'm trying to start Cassandra using code, and I can't connect to it. When I telnet to port 7000, it does connect, but when I try to connect to 9042 (the "native transport" port) I get a "connection refused". So, somehow, the native transport isn't happening.
My startup code:
File file = new File(home, "etc/cassandra.yaml");
System.setProperty("cassandra.config", "file:" + file.getPath());
CassandraDaemon cassandra = new CassandraDaemon();
cassandra.init(null);
My cassandra.yaml contains:
start_native_transport: true
native_transport_port: 9042
The logs indicate that Cassandra is starting. I see no reference in the logs to any native transport, even when the log level is set to DEBUG. No references to port 9042.
I'm on Windows. I don't think it's a firewall issue because I'm trying to connect from localhost.
Any ideas?
Have you tried calling the .start method?
I've implemented an Embedded Cassandra Server in Achilles, example of working code here: https://github.com/doanduyhai/Achilles/blob/master/achilles-core/src/main/java/info/archinnov/achilles/embedded/CassandraEmbedded.java
CassandraDaemon cassandraDaemon = new CassandraDaemon();
cassandraDaemon.activate();

H2 database error: Database may be already in use: "Locked by another process"

I am trying to use the H2 database from a Java application.
I created the database and its tables through the H2 Console and then I try to connect from Java using
Connection con = DriverManager.getConnection("jdbc:h2:~/dbname", "username", "password");
However I receive the following error:
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Database may be already in use: "Locked by another process". Possible solutions: close all other connection(s); use the server mode [90020-161]
I tried to delete the dbname.lock.db file but it is automatically re-created.
How can I unlock the database to use it from my Java program?
H2 is still running (I can guarantee it). You need to use a TCP connection for multiple users such as ->
<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/C:\Database\Data\production;"/>
OR
DriverManager.getConnection("jdbc:h2:tcp://localhost/server~/dbname","username","password");
It also means you need to start the server in TCP mode. Honesetly, it is pretty straight forward in the documentation.
Force kill the process (javaw.exe for Windows), and make sure that any application that might have started it is shut down. You have an active lock.
I had the same problem.
in Intellj, when i want to use h2 database when my program was running i got the same error.
For solve this problem i changed the connection url from
spring.datasource.url=jdbc:h2:file:~/ipinbarbot
to:
spring.datasource.url=jdbc:h2:~/ipinbarbot;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE
And then my problem gone away. now i can connect to "ipinbarbot" database when my program is.
If you use Hibernate, also don't forget to have:
spring.jpa.hibernate.ddl-auto = update
goodluck
I'm using h2db with a test T24 tafj application, I had the same problem but I managed to resolve it by identifying the application that is running h2 (launched when I attempted to setup a database connection).
ps aux|grep java
will give output as:
sysadmin 22755 3.2 0.1 5189724 64008 pts/3 Sl 08:28 0:00 /usr/java/default/bin/java -server -Xmx2048M -XX:MaxPermSize=256M -cp h2-1.3.175.jar:/r14tafj/TAFJ/dbscripts/h2/TAFJFunctions.jar org.h2.tools.Server -tcp -tcpAllowOthers -baseDir /r14tafj/t24/data
now kill this with its process id:
kill -9 22755
and at last remove the lock file:
rm -f dbname.lock.db
I got clue from Saman Salehi above.
My usecase:
Preparing REST application for client-side load balancing(running two JVM instances of REST). Here my MVC application will call this REST application that has ActiveMQ backend for DATA.
I had the problem when I ran two instances of REST application in eclipse and trying to run both instances at the same time with the following configuration
spring.datasource.url=jdbc:h2:file:./Database;
spring.jpa.properties.hibernate.hbm2ddl.auto=update
After adding DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE
spring.datasource.url=jdbc:h2:file:./Database;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE
Both instances are running and showing in Eureka dasboard.
Don't close the database when the VM exits : jdbc:h2:;DB_CLOSE_ON_EXIT=FALSE
Multiple processes can access the same database without having to start the server manually ;AUTO_SERVER=TRUE
Further reading:
http://www.h2database.com/html/features.html
You can also visit the "Preferences" tab from the H2 Console and shutdown all active sessions by pressing the shutdown button.
Simple step: Go to the task manager and kill the java process
then start your apllication
You can also delete file of the h2 file database and problem will disappear.
jdbc:h2:~/dbname means that file h2 database with name db name will be created in the user home directory(~/ means user home directory, I hope you work on Linux).
In my local machine its present in: /home/jack/dbname.mv.db I don't know why file has a name dbname.mv.db instead a dbname.
May be its a h2 default settings.
I remove this file:
rm ~/dbname.mv.db
OR:
cd ~/
rm dbname.mv.db
Database dbname will be removed with all data. After new data base init all will be ok.
If you are running same app into multiple ports where app uses single database (h2), then add AUTO_SERVER=TRUE in the url as follows:
jdbc:h2:file:C:/simple-commerce/price;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE;AUTO_SERVER=TRUE
I ran into similar problems running with ORMLite from a web application. I initially got stuck on the syntax to use server mode in the url. The answers above helped with that. Then I had the similar user/password error which was easier to figure out. I did not have to shut anything down or erase any files. The following code worked:
protected ConnectionSource getConnectionSource() throws SQLException {
String databaseUrl = "jdbc:h2:tcp://localhost/~/test";
return new JdbcConnectionSource(databaseUrl,"sa","sa");
}
To use H2 in server mode on wildfly, I Modifed connection-url in standalone.xml
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool- name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:tcp://localhost/~/test</connection-url>
…
</datasource>
Identify the H2 process id and kill it. For mac
ps -ef|grep h2
Then get the process id and kill it.
kill -9 PID
According H2 Database Tutorial you can run the H2 Database in three different modes:
Server mode:
jdbc:h2:tcp://localhost/~/test
When using H2 db in server mode (also known as client/server mode) all data is transferred over TCP/IP. Before application can use H2 Database in server mode, you need to start the H2 DB within the same or another machine.
Embedded mode:
jdbc:h2:~/test
H2 db in embedded mode will be faster but the downside of it is that no other process can access the Database. In the above connection string, the Data will be saved into the ‘test’ folder, under the user’s home directory.
Mixed mode:
The mixed mode combines some features of the embedded and the server mode. The first application connecting to the H2 db does that in embedded mode, but at the same time it starts a server so that other applications can concurrently access the same data, even from different processes.
jdbc:h2:/data/test;AUTO_SERVER=TRUE
When using automatic mixed mode, you can share the JDBC URL for all applications using the DB. By default the server uses any free TCP port. The port can be set manually using AUTO_SERVER_PORT=9090.
Ran into a similar issue the solution for me was to run fuser -k 'filename.db' on the file that had a lock associated with it.
Hope this helps!
I was facing this issue in eclipse . What I did was, killed the running java process from the task manager.
It worked for me.
In your application.properties file > edit the datasource into:
spring.datasource.url=jdbc:h2:file:C:/temp/test;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE
Happy coding!
answer for this question => Exception in thread "main" org.h2.jdbc.JdbcSQLException: Database may be already in use: "Locked by another process". Possible solutions: close all other connection(s); use the server mode [90020-161]
close all tab from your browser where open h2 database also Exit h2 engine from your pc
For InteliJ: right lick on your database in the database view and choose "Disconnect".
I tried to delete the dbname.lock.db file but it is automatically re-created.
How can I unlock the database to use it from my Java program?
Just add FILE_LOCK=NO;. FILE_LOCK=NO doesn't make dbname.lock.db.
spring.datasource.url=jdbc:h2:file:./testdb/h2;DB_CLOSE_ON_EXIT=false;FILE_LOCK=NO;
The detail for FILE_LOCK reference this.
Using the method NO forces the database to not create a lock file at all

How to run a HSQLDB server in memory-only mode

In the documentation of the HSQLDB is a command line statement to start a HSQLDB server (HSQLDB Doc). But there is this "file:mydb" property, so I assume its not in memory-only mode.
How do I run a memory-only HSQLDB server?
I ran the following but get no clue.
java -cp ../lib/hsqldb.jar org.hsqldb.Server -?
It took around 2 days for me to figure out on how to start a server in-memory and then access from outside. Hope this will save someone's time.
Server server = new Server();
server.setDatabaseName(0, "mainDb");
server.setDatabasePath(0, "mem:mainDb");
server.setDatabaseName(1, "standbyDb");
server.setDatabasePath(1, "mem:standbyDb");
server.setPort(9001); // this is the default port
server.start();
When you have to access the in-memory database for any CRUD, here is what you need to do :-
String url="jdbc:hsqldb:hsql://192.168.5.1:9001/mainDb";
Class.forName("org.hsqldb.jdbc.JDBCDriver");
Connection conn = DriverManager.getConnection(url, "SA", "");
where 192.168.5.1 is the server ip where HSQL is running. To connect to the standbyDb, replace mainDb with standbyDb in the first line. Once you get the connection, you can perform all database related operations.
To connect to the server from remote using DatabaseManagerSwing, here is what you need to do.
Download hsqldb-x.x.x jar and copy it to a folder (x.x.x is the version)
open a terminal or command prompt and cd to the folder and run
java -cp hsqldb-x.x.x.jar org.hsqldb.util.DatabaseManagerSwing
Select "HSQL Database Engine Server" from the Type drop down and give "jdbc:hsqldb:hsql://192.168.5.1:9001/mainDb" as the URL. This will connect you to the remote HSQL in-memory Server instance.
Happy Coding !!
DbManagerSwing UI
use java -cp .\hsqldb-1.8.0.10.jar org.hsqldb.Server -database.0 mem:aname
In memory mode is specified by the connection url - so if you want, you can just have a server.properties file in the same directory, and set the connection url to use the mem protocol - or if you are using hsqldb in another application that allows you to specify the connection url such as jdbc, specify jdbc:hsqldb:mem:aname.
I believe the file is used to load up the db into memory, and then persist when Server stops. I don't think the file is accessed while you're running.
It's been awhile since I've used HSQLDB (or H2), but I'm pretty sure that's how it works.

Categories

Resources