Q - Cannot authenticate via Mongo Java Driver. Things work fine on mongo shell.
MongoDB Server Version -
db version v3.2.6
Java Mongodb Driver Maven Dependency
version 2.6.2
This is what I did -
Start MongoDB - sudo mongod --port 27017 --dbpath /data/db
Open shell - mongo --port 27017
use admin
db.createUser({user:"abc",pwd:"abc",roles:[{role:"root",db:"admin"}]})
Restart with auth required - sudo mongod --auth --port 27017
--dbpath /data/db
Java side
MongoOptions moptions = new MongoOptions();
moptions.autoConnectRetry = Boolean.getBoolean(prop.getProperty("MONGO_OPTIONS_AUTOCONNECT_RETRY"));
moptions.connectionsPerHost = Integer.parseInt(prop.getProperty("MONGO_OPTIONS_CONNECTIONS_PER_HOST"));
moptions.threadsAllowedToBlockForConnectionMultiplier = Integer.parseInt(prop.getProperty("MONGO_OPTIONS_THREADS_BLOCK"));
ServerAddress srvAddr = new ServerAddress(prop.getProperty("MONGODB_HOST"), Integer.parseInt(prop.getProperty("MONGODB_PORT")));
client = new Mongo(srvAddr, moptions);
DB adminDB = client.getDB("admin");
Boolean auth = adminDB.authenticate("abc", "abc".toCharArray());
Result
auth always comes out FALSE.
The authentication procedure for the MongoDB Java driver 2.x I know works as follows:
MongoCredential credential = MongoCredential.createCredential(
user, "admin", password.toCharArray());
Mongo mongo = new MongoClient(new ServerAddress(mongoHostname),
Arrays.asList(credential));
Note for accessing a Mongo 3.2 database using a 2.x driver you need at least MongoDB Java driver Version 2.14 (see MongoDB compatibility info). Your version 2.6.2 is way too old. You need to upgrade it.
Related
I'm attempting to run a Spring Boot app that connects a Postgres DB using:
docker-compose.yml (for Postgres) :
version: '3'
services:
postgres-db:
container_name: postgres-db
image: postgres:latest
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: my_user
POSTGRES_PASSWORD: my_password
POSTGRES_DB: shorten-db
To run the Postgres DB:
docker-compose up
.Dockerfile (for the Spring Boot app) :
FROM openjdk:12-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
In order to run the Spring app using Docker I use:
mvn package
docker build -t url-shorten/url-shorten-docker .
docker run -p 8080:8080 url-shorten/url-shorten-docker
But I receive the error when starting when running above docker command:
Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
In Spring application.properties I connect to the DB using:
spring.datasource.url=jdbc:postgresql://localhost:5432/shorten-db
I think this error is due to the Spring Boot app is running in a different container to DB so it cannot find the DB on localhost. Is there an idiomatic way of connecting the Spring Book docker container to the DB container. Or do I have do access the IP address of my machine and use this address to connect to the Postgres DB running on Docker?
Yes, you can't use localhost in this situation
spring.datasource.url=jdbc:postgresql://postgres-db:5432/shorten-db
In Spring application.properties, try to change DB config to:
spring.datasource.url=jdbc:postgresql://postgres-db:5432/shorten-db
In container networks, You need to use the container name as a host.
You can add both DB and app containers to one Docker network and change PostgreSQL host in datasource URL to postgres-db. Then Spring app will work with your DB.
I am trying to connect Azkaban ( A job scheduler for hadoop) with my local mysql. The configiration file of azkaban looks like:
database.type=mysql
mysql.port=3306
mysql.host=localhost
mysql.database=azkaban
#Changed by Prakhar for azkaban , Azkaban
mysql.user=root
mysql.password= [ Password of mysql ]
My MySql has a database named "azkaban" and i am able to login mysql using command:
./mysql -u root -p
Also mysql is working on port 3306, which i have verified.
Still i am unable to connect to mysql. The logs of azkaban looks like this:
2020/04/11 22:38:05.584 +0530 ERROR [MySQLDataSource] [Azkaban] Failed to find write-enabled DB connection. Wait 15 seconds and retry. No.Attempt = 1
java.sql.SQLException: Cannot create PoolableConnectionFactory (Could not create connection to database server.)
at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2294)
at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2039)
at azkaban.db.MySQLDataSource.getConnection(MySQLDataSource.java:76)
at org.apache.commons.dbutils.AbstractQueryRunner.prepareConnection(AbstractQueryRunner.java:175)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:286)
at azkaban.db.DatabaseOperator.query(DatabaseOperator.java:68)
at azkaban.executor.ExecutorDao.fetchActiveExecutors(ExecutorDao.java:53)
at azkaban.executor.JdbcExecutorLoader.fetchActiveExecutors(JdbcExecutorLoader.java:266)
at azkaban.executor.ExecutorManager.setupExecutors(ExecutorManager.java:223)
at azkaban.executor.ExecutorManager.<init>(ExecutorManager.java:131)
at azkaban.executor.ExecutorManager$$FastClassByGuice$$e1c1dfed.newInstance(<generated>)
at com.google.inject.internal.DefaultConstructionProxyFactory$FastClassProxy.newInstance(DefaultConstructionProxyFactory.java:89)
at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:111)
at
Few pointers that might help you to resolve the issue.
Please check the version of MySQL installed vs the MySQL client mentioned in the Gradle file used to build Azkaban
Preferably use MySQL 5.x version
Make sure to install the compatible client version
Also, create the Mysql tables in the database before starting the web executor
So i have production server that i want to connect to with Mysql Workbench.
When i start application all runs fine, so i conenct to it via IP of the server and credentials.
My docker-compose looks like this :
...
workaround-mysql:
container_name: workaround-mysql
image: mysql
environment:
MYSQL_DATABASE: workaround
MYSQL_USER: someuser
MYSQL_PASSWORD: somepass
MYSQL_ROOT_PASSWORD: somepassss
MYSQL_ROOT_HOST: %
ports:
- "3308:3306"
restart: always
....
So on my prod machine i run spring application, mysql, nginx.
When i try to connect via workbench this is the error i get :
Error 10061.
I dont understand what im doing wrong. I had no trouble when i was doing local testing. Workbench worked fine.
Is there some additional settings i have to do ? And can i do these required settings via docker-compose?
On my firewall, i have set accept connection to mysql port:
Oh and the additional note : I get that error right when i press connect on workbench, I dont even have prompt to enter password.
And docker compose up output for mysql part :
Maybe it is just a typo, but you put 3308 as a port in your docker-compose file. So either fix that to 3306 or use 3308 when connecting.
I am facing a problem connecting to mongodb from my machine to public IP server on which mongodb installed as a windows service with --auth
When I removed authentication as below command, I am able to access the database collection
mongod --install --noauth --dbpath "c:\mongodb\data" --logpath
"c:\mongodb\logs\log.txt" --bind_ip "0.0.0.0"
And when I use the --auth in place of --noauth, I am getting the following error:
errmsg : "auth failed" code :18 login failed
And I am giving correct login details to connect to the mongodb.
What is causing this and how can I fix it?
What command are you using to connect to your database?
If you use mongo like mongo -u login -p password -h xxx.yyy.zzz.aaa try to add --authenticationDatabase admin.
Need to validate the user authentication in DB2 from Java before creating ant Database or executing any SQL query.
Is there any DB2 command for this?
Thanks in Advance.
You can use this command to run on db2cmd to verify the user name
db2icrt <db2_instancename> -u <userName> , <passWord>
But how to use this from Java, i have no idea.
You can use DB2Jcc - IBM Data Server Driver for JDBC and SQLJ diagnostic utility to test database connectivity and validate DB2 authentication using Java. A sample command and it's output is shown as below:
$ java com.ibm.db2.jcc.DB2Jcc -url jdbc:db2://myhost.bluemix.net:446/MYDB -user myuserid -password mydbpasswd
[jcc][10521][13706]Command : java com.ibm.db2.jcc.DB2Jcc -url jdbc:db2://myhost.bluemix.net:446/MYDB -user myuserid -password ********
[jcc][10516][13709]Test Connection Successful.
DB product version = DSN11015
DB product name = DB2
DB URL = jdbc:db2://myhost.bluemix.net:446/MYDB
DB Drivername = IBM Data Server Driver for JDBC and SQLJ
DB OS Name = Linux
$