No entries in pg_hba.conf - java

Ok first of all i'm french so sorry for the possible incomprehension or bad translate of error messages.
I have a Raspberry, with a Tomcat server on. Also, i use Postgresql too.
On my website, The server side connect to the localhost to access to the database with the JDBC plugin, all works great.
But now i'm trying to developp a Java Program, which one need to connect to the sql database.
But the program have as target to being deployed. So, I can't use "localhost" to connect to the database. On my computer, i'm trying to connect to my Raspberry's database with the JDBC and the few following code :
<code>
public class DB {
Connection co;
PreparedStatement ps;
public DB() {
try {
Class.forName("org.postgresql.Driver");
co = DriverManager.getConnection("jdbc:postgresql://192.168.1.47:5432/database?sslmode=require", "username", "password");
}
catch (ClassNotFoundException e) {System.out.println(e);}
catch (SQLException e) {System.out.println(e);}
}
}
</code>
But when i try to connect, i get the following error :
" org.postgresql.util.PSQLException: FATAL: aucune entr?e dans pg_hba.conf pour l'h?te << 192.168.1.73 >>, utilisateur << username >>,
base de donn?es << database >>, SSL actif "
Which means "No entries in pg_hba.conf for the host..." but i tried to add entries in pg_hba.conf, like this :
host all postgres 192.168.0.0/24 trust
host databaseName userName 192.168.0.0/24 md5
host all all 0.0.0.0/24 md5
but this didn't worked (maybe bad row ? how can i just authorize the user "username" with a password ?)
I added the listen_addresses='*' in postgres.conf too.
Someone know how i need to configure all ?
My computer is on the same WIFI than my Raspberry. Does i need to connect with an external computer on an external WIFI ?
Maybe i did another mistake ?
Thanks for your attention.

OK I solved it by myself, i added these line to pg_hba.conf :
host all all 0.0.0.0/0 md5
now i have to work on the security. Thanks by the way.

Related

Trying to connect to a mysql database, getting exception saying Must specify port number after:

I’m trying to connect to a MySQL database on my website from java.
Currently I’m getting a exception that says
Must specify port number after:”
I Google stack overflow and found the default MySQL port is 3306.
But I can't find any information about how I add it to my url, which now looks like
jdbc:mysql://http://www.findmeontheweb.biz/database name"+
“user=findmeon_bitcoin&password=password
code:
try {
// this will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// setup the connection with the DB.
Connection connect = DriverManager.getConnection("jdbc:mysql://http: //www.findmeontheweb.biz//findmeon_bitcoin//"+ "user=findmeon_bitcoin&password=oreo8157");
} catch (Exception e) {
System.out.println("Exception...." );
}
1) Hope the space in your code was only a copy/paste error here
2) You need to remove the http in the mysql uri
Connection connect = DriverManager.getConnection("jdbc:mysql://http: //www.findmeontheweb.biz//findmeon_bitcoin//"+ "user=findmeon_bitcoin&password=oreo8157");
will be
Connection connect = DriverManager.getConnection("jdbc:mysql://findmeontheweb.biz/findmeon_bitcoin/"+ "user=findmeon_bitcoin&password=oreo8157");
And just in case you are running it on a custom port, you can specify port by
Connection connect = DriverManager.getConnection("jdbc:mysql://findmeontheweb.biz:3306/findmeon_bitcoin/"+ "user=findmeon_bitcoin&password=oreo8157");
replace 3306 with your custom port.
Also I hope that is not your real username and password!

MySQL denied access in java, workbench access granted

I've been using java on a computer (lets call it PC1) to connect to a database on a server, and until recently it was working with no errors.
By working, I mean I could connect to the server using java on PC1, and access the info I needed from the tables using select statements.
The only changes that have been made are the ip addresses on PC1 and the server.
After changing the IP addresses, I then updated the grant table in mysql, and yet, I get the following error:
java.sql.SQLException: Access denied for user 'robot'#'aa-PC' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:885)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3421)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1247)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2775)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at WriteToMySql.connection1(WriteToMySql.java:26)
at WriteToMySql.main(WriteToMySql.java:259)
The strange part however, is that I am able to connect to the server's database using the mySQL workbench and access all data on them.
here's the java code:
String host = "jdbc:mysql://PC1IPAdress:3306/users";
String user= "robot";
String password="mypassword";
public void connect()
{
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("worked"); //this gets printed
connect = DriverManager.getConnection(host, user, password);
System.out.println("works"); // this does not get printed due to error
stmt = connect.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
note: "users" is the name of the database
Any help will be greatly appreciated.
Thank you.
EDIT:
for testing purposes I tried turning off the firewall, but it did not help.
I think there may be an error in the code sample - it connects to PC1IPAddress when you mention that PC1 should be connecting to a server earlier in the post. Just want to make sure before we continue that it was a typo, as otherwise PC1 would be connecting to itself.
If you have administrative access to the server, connect to MySQL as root and use this query to show configured users and ensure the host field is correct: SELECT user, host FROM mysql.user WHERE user='robot';
If the above checks out, I would suggest looking into Windows user authentication. The fact that MySQL returned the Windows computer name ('aa-PC') and not its IP address seems to indicate it may be attempting to authenticate using Windows domain credentials: http://dev.mysql.com/doc/refman/5.5/en/windows-authentication-plugin.html
If you change the ip address of the client (PC1) make sure that you updated the host field as well when granting new rights to user "robot". Check the table "db", "user" and "host".

How to connect Java to Microsoft SQL server

I made a Java application to connect to a MySQL database.
The connection was made ​​in this way:
public class Connection {
public static Connection getConexao() throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
//System.out.println("Conectado");
return DriverManager.getConnection("jdbc:mysql://localhost/world","root", "rootadmin");
} catch (ClassNotFoundException e) {
throw new SQLException(e.getMessage());
}
}
}
Now I needed to change the connection from MySQL to Microsoft SQL Server 2012.
Can anyone help me change the connection to the database?
Thank you all very much.
First of all you will need JDBC drivers for MS SQL Server. Either from Microsoft or there are other options like jTDS.
Then you should use a connection string like jdbc:sqlserver://ServerName:Port;databaseName=;user=username;password=password;
Of course your SQL Server should be in mixed mode so you can connect with username and password created on server.
Applets run on users' computer, therefore you should open your SQL Server ports to all visitors which is a BAD idea.
Make database URL like :
jdbc:mysql://IP address:DatabasePort/DatabaseName,username, password
public class Connection {
public static Connection getConexao()throws SQLException{
try{
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/world","root", "rootadmin");
}catch(ClassNotFoundException e) {
throw new SQLException(e.getMessage());
}
}
}
This answer is presented for next visitors on this kind of question. Configuring the java driver connection for SQL Server can
be quite confusing for new users. I'll guide you here through SQL Management Studio (SMSS):
There're 2 kinds of authentification accepted on SQL Server. They are Windows & SQL Server authentification.
In this answer I'll active "sa" (syst. administrator) account for quick setup demonstration over the connection.
To enable the "sa" account (you can skip this if it had already been there):
Login as usual using default window authentification mode
Right click on the server name (i.e MYCOMPUTER223\SQLEXPRESS) > Security > go enable the SQL Server & Window authentification mode > ok
On the left tree menu, click Security > Logins > right click that "sa" > Properties > set up your "password" for this "sa" account
and then on the left menu there is the "Status"> enable the "Login:"
restart the SQL Server service
now login as "sa" through "SQL Server authentification mode" on the SMSS . Using the password we've just set up.
Enable the TCP/IP for the conn. instance (this is by default is disabled particularly on sql express editions):
Open "Sql Server Configuration Manager". This is installed along the installation of SQL Server engine.
"SQL Server Network Configuration" > "Protocol for SQLExpress" > enable the "TCP/IP"
right click that "TCP/IP" > "IP Address" > scroll down till you find "IPAll" and then just fill the "port" field with 1433
You can now use this credential for the SMSS:
username : sa
password : ...the password you've just set up above..
Or you can now use this credential on your external java based clients/data or BI tools/sql management tools such as Pentaho, Heidi SQL, DB Weaver or any particular java framework conn. manager descriptor, etc. :
hostname : localhost (or any custome host domains)
database name : your database name..
instance name : i.e SQLEXPRESS (this can be found through the SMSS, right click the server name > view connection properties)
port : 1433
username : sa
password : ...the password you've just set up above..
or via url/uri for the Java connection manager/factory:
String Connectionurl="jdbc:sqlserver://localhost:1433;DatabaseName=Yourdatabasename;user=sa;password=yourSApassword";
public Connection createConnection() throws NoSuchAlgorithmException {
System.out.println("Creating SQL Server DataBase Connection");
Connection connection = null;
try {
// Provide the java database driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Provide URL, database and credentials according to your database
// .getConnection ("url/namadatabase, user, password")
String Connectionurl="jdbc:sqlserver://localhost:1433;DatabaseName=DummyDatabase;user=sa;password=YourSAaccountpassword";
connection = DriverManager.getConnection(Connectionurl);
} catch (Exception e) {
e.printStackTrace();
return null;
}
if (connection != null) {
System.out.println("Connection created successfully..");
}
return connection;
}

mysql connecting to host

we are trying to connect to the host using the code shown below:
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://sql3.000webhost.com/a17644_cc","chat_cc", "pass");
ResultSet rs;
if(!con.isClosed())
{
Statement st = con.createStatement();
rs= st.executeQuery("SELECT * FROM user_info");
while(rs.next()){
t1.append(rs.getString(3));
}
}
} catch(Exception e) {
t1.setText(e.toString());
//e.printStackTrace();
} finally {
try {
if(con != null)
con.close();
}catch (java.sql.SQLException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
t1.setText(e.toString());
}
we have given internet permission also in the manifest file.
But getting the following error:
java.sql.exception: data source rejected establishment of connection, message from server:"Host '182.71.248.226. is not allowed to connect to this MySQL server"
This is the following details i got: please tell which name we must give in the connection
string
Domain chitchat.site90.net
Username a1740644
Password *
Disk Usage 0.14 / 1500.0 MB
Bandwidth 100000 MB (100GB)
Home Root /home/a1740644
Server Name server19.000webhost.com
IP Address 31.170.160.83
Apache ver. 2.2.19 (Unix)
PHP version 5.2.
MySQL ver. 5.1
Activated On 2012-05-01 02:14
Status Active
That error is telling you that the user does not have rights to connect and select the database. You need to either grant rights to all hosts, or hosts from the specific IP address you are using. To grant to all hosts, you'd have to issue this as an administrative user:
GRANT ALL ON a17644_cc.* TO 'chat_cc'#'%'
or alternatively
GRANT ALL ON a17644_cc.* TO 'chat_cc'#'182.71.248.226'
Assuming that the IP in question is static, and you want to constrain the connection by IP.
As pointed out by https://stackoverflow.com/a/1559992/700926 this is probably a security precaution. Check out the accepted answer to that question.
What is the port number of MySQL server?Maybe you left it away.
Alternitavely to the answer from gview you can set priviliges in MySQL Workbench.Navigate to Security -> Users and Priviliges.

A problem connecting to a MySQL DB using JDBC

Here's how I'm trying to connect:
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e) {
throw new DbConnectionException();
}
try {
connection = DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
e.printStackTrace();
throw new DbConnectionException();
}
I'm 100% sure that the url, username, password strings are correct. I've already connected successfully using an external tool (MySQL query browser).
This is the error I receive:
com.mysql.jdbc.CommunicationsException:
Communications link failure due to
underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.SocketException MESSAGE:
java.net.ConnectException: Connection
refused
...
Possibly a url issue. If your code is pointing to MySQL localhost, try changing localhost to 127.0.0.1 on your url.
E.g.:
jdbc:mysql://localhost:3306/MY_DB
to
jdbc:mysql://127.0.0.1:3306/MY_DB
And see if this works.
did you run the mysql browser from the same machine where the code is running? What I am getting at is the permissions in mysql can be host-specific, and depending on how you set them up you might not be able to connect from the machine where the code is running.
Also, you might want to double check the url, name, pword again, perhaps with log statements or a debugger to make sure there are no typos, trailing whitespaces, etc...
Double check the format of your url. It should start with "jdbc:mysql:". Make sure you are using a current version for the driver as well.
Check that you can connect to the database from the mysql admin tool, that will drive out whether your mysql is running and that the port is open.
In my case the problem was that I was using a connection from emulator to localhost.
If you use emulator to localhost don't use localhost value in connection String but use 10.0.2.2 instead:
jdbc:mysql://10.0.2.2:3306/MY_DB
Hope this helps.

Categories

Resources