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
$
Related
I have a test container for Mysql and I need to import the dump file after the container started. I've tried two options below.
public class AbstractTest {
public static MySQLContainer<?> mySQLContainer = new MySQLContainer<>("mysql:5.7");
static {
mySQLContainer
.withDatabaseName("myDatabase")
.withCopyFileToContainer(
MountableFile.forClasspathResource("init.sql", 0744),
"init.sql")
.withUsername("root")
.withPassword("root")
.start();
}
#PostConstruct
#SneakyThrows
public void init() {
option 1 // mySQLContainer.execInContainer("mysql -u root -proot myDatabase < init.sql");
option 2 // mySQLContainer.execInContainer("mysql", "-u", "root", "-proot", "myDatabase", "<", "init.sql");
}
////
}
and still no success - it looks like mysql can't parse my command properly cause I get next as an answer:
mysql Ver 14.14 Distrib 5.7.35, for Linux (x86_64) using EditLine wrapper
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Usage: mysql [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
--auto-rehash Enable automatic rehashing. One doesn't need to use
'rehash' to get table and field completion, but startup
////.....
If use next command
option 2 // mySQLContainer.execInContainer("mysql", "-u", "root", "-proot");
it works fine, but this is not what I wanted
mysql -u root -proot mydatabase < init.sql command works fine if I just connect to the container via bash from cli.
So my question - how to import SQL dump file in MySQLContainer in JUnit test containers by executing the command in image?
UPDATE:
I figured out that there is some thing wrong with parsing of "<" sign.
So, basically this works fine from CLI:
docker exec -i mycontainer mysql -uroot -proot myDatabase < init.sql
But this is not working from Java:
mySQLContainer.execInContainer("mysql","-uroot","-proot","myDatabase","<","init.sql");
MySQL can load a dump file automatically if you put it at a special path.
From the docs of the MySQL Docker image:
When a container is started for the first time, a new database with
the specified name will be created and initialized with the provided
configuration variables. Furthermore, it will execute files with
extensions .sh, .sql and .sql.gz that are found in
/docker-entrypoint-initdb.d. Files will be executed in alphabetical
order. You can easily populate your mysql services by mounting a SQL
dump into that directory and provide custom images with contributed
data. SQL files will be imported by default to the database specified
by the MYSQL_DATABASE variable.
So the easiest is to copy the file there with something like:
.withCopyFileToContainer(MountableFile.forClasspathResource("init.sql"), "/docker-entrypoint-initdb.d/schema.sql")
You need an applications.properties suitable for MySql, something like:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name =com.mysql.jdbc.Driver
#spring.jpa.show-sql: true
Then you can add a data.sql in your /src/test/resources which will automatically be run.
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
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.
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.
Needed Some help, I installed the newrelic mysql plugin, and created a user from script but when I start the agent using java -jar newrelic_mysql_plugin-1.2.0.jar it gives me
com.newrelic.metrics.publish.binding.Context | SEVERE | Unable to
obtain a new database connection: jdbc:mysql://localhost:3306/
root/PASSWORD_FILTERED, check your MySQL configuration settings.
Access denied for user 'root'#'127.0.0.1' (using password: YES)
I tried to use it using root credentials for mysql but same error. i can connect from localhost shell using root credentials. and my mysql.instance.json is:
[ { "name" : "MyApp", "host" : "localhost:3306", "metrics" :
"status,newrelic", "user" : "root", "passwd" : "mypassword" }, ]
Reference for Plugin: http://newrelic.com/plugins/new-relic-inc/52
found the issue, the default script didn't create the user#'127.0.0.1' just created user#localhost. i created it manually and now its working like charm. just posting the issue hope it will help others.
You can see if user is created or not using
SELECT User FROM mysql.user;
Or to Create a User with limited rights in mysql shell just use.
CREATE USER newrelic#localhost IDENTIFIED BY 'yourpass';
GRANT PROCESS,REPLICATION CLIENT ON . TO newrelic#localhost;
CREATE USER newrelic#127.0.0.1 IDENTIFIED BY 'yourpass';
GRANT PROCESS,REPLICATION CLIENT ON . TO newrelic#127.0.0.1;
and Define your password in Clean test in mysql json.