I would like to connect with my local MYSQL data base, which is installed along with XAMP server. I created a new User ie, " NewUser " and its password is "password". I given all PRIVILEGES to this user.
I write the code to connect with data base by using user "root" (No password for this user).
Its connected . Like bellow..
return DriverManager.getConnection("jdbc:MySql://localhost/database_name","root","");
Now I wrote the code to connect with same data base by another user ie, "NewUser" and its pasword "password"
return DriverManager.getConnection("jdbc:MySql://localhost/database_name","NewUser","password");
but its not connected.
The error in console is
java.sql.SQLException: Access denied for user 'NewUser'#'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:925)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1704)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1250)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.GeneratedConstructorAccessor207.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.createConnection(JDBCConnection.java:214)
at org.eclipse.datatools.connectivity.DriverConnectionBase.internalCreateConnection(DriverConnectionBase.java:105)
at org.eclipse.datatools.connectivity.DriverConnectionBase.open(DriverConnectionBase.java:54)
at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.open(JDBCConnection.java:73)
at org.eclipse.datatools.enablement.internal.mysql.connection.JDBCMySQLConnectionFactory.createConnection(JDBCMySQLConnectionFactory.java:28)
at org.eclipse.datatools.connectivity.internal.ConnectionFactoryProvider.createConnection(ConnectionFactoryProvider.java:83)
at org.eclipse.datatools.connectivity.internal.ConnectionProfile.createConnection(ConnectionProfile.java:359)
at org.eclipse.datatools.connectivity.ui.PingJob.createTestConnection(PingJob.java:76)
at org.eclipse.datatools.connectivity.ui.PingJob.run(PingJob.java:59)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)
I give the host type while providing PRIVILEGES to this user, as " any host" ie."%".
If I change this to " localhost " or "127.0.0.1" its working.
So How can i use my database with " anyhost " PRIVILEGES to the particular user like "NewUser" .
If I got success here then I successes in connection to client live Data base..
Thanks to all and please let me out from this one.....
Since the first specified code works and also based on the reported trace, i'm pretty sure the problem is on the database, not the code syntax.
Based on the Mysql version, please try as an alternative to set privileges without specifying the any host (%) as based on Mysql documentation,
The simple form user_name is a synonym for user_name#'%'
Also flush privileges immediately after using FLUSH PRIVILEGES;
Just to make sure everything is correct, also run a
SHOW GRANTS FOR NewUser; and check if NewUser appears in the list with the corresponding permissions.
your URL is meant to be all lowercase -- `jdbc:MySql://localhost/database_name"
Is your database really called database_name?
Please try this syntax
return DriverManager.getConnection("jdbc:mysql://localhost/database_name?user=NewUser&password=your_password_here");
(just replace your password where it says "your_password_here")
EDIT - SECOND GUESS:
Its possible you have firewalled yourself by blocking 192.168.x.x range.
i assume you running windows. open up command prompt, type "ipconfig" press enter. see your ipv4 adress (should be something like 192.168.1.x)
Be sure your antivirus/firewall program permits connection from 192.168.1.x (which is yourself) and then try using that instead of "%" or "localhost"
If this doesn't work, close all your firewall/antivirus and try again.
Also try reloading privileges either by:
restarting xampp
FLUSH PRIVILEGES;
In order to give users access to your database, you need to specify a host where they should be allowed to connect from.
But please be careful: Even if you use a wildcard (%) as the host name, the user cannot connect from localhost. They can connect from ANY host, but not from localhost.
When connecting from localhost, I assume your mysql installation assumes you to be an anonymous user.
In order to allow a user to connect from localhost, you need to add a separate user whith "localhost" in the host field.
For more details, please refer to the MySQL documentation
Two of the accounts have a user name of monty and a password of
some_pass. Both accounts are superuser accounts with full privileges
to do anything. The 'monty'#'localhost' account can be used only when
connecting from the local host. The 'monty'#'%' account uses the '%'
wildcard for the host part, so it can be used to connect from any
host.
It is necessary to have both accounts for monty to be able to connect
from anywhere as monty. Without the localhost account, the
anonymous-user account for localhost that is created by
mysql_install_db would take precedence when monty connects from the
local host. As a result, monty would be treated as an anonymous user.
The reason for this is that the anonymous-user account has a more
specific Host column value than the 'monty'#'%' account and thus comes
earlier in the user table sort order. (user table sorting is discussed
in Section 6.2.4, “Access Control, Stage 1: Connection Verification”.)
GO to users/privileges/edit privileges/
change password or select no password
update the password in hibernate config file and restart the server.
Related
I'm writing a method that make it possible for my Java program to create a database connection that will eventually make me able to access it from other classes/methods.
public class DatabaseConnection
{
private Connection databaseLink;
public Connection getConnection()
{
String url = "jdbc:mysql://localhost/DBname";
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
databaseLink = DriverManager.getConnection(url, "fakeUsr", "fakePsw"); //these are not the real username/password
}
catch (Exception e)
{
e.printStackTrace();
}
return databaseLink;
}
}
I've got a couple of issues:
1)people not using my computer will not be able to get into my server since I wrote "localhost":
String url = "jdbc:mysql://localhost/DBname";
2)I've typed the real username and password instead of "fakeUsr" and "fakePsw".
The thing is: I'm quite sure that the average user of my program should NOT be able to access that information. Is there any other way to permit access to a DB without making username and password readable by virtually anyone getting access to my source code?
For issue n. 1: I tried to type my IP address instead of "localhost" here:
String url = "jdbc:mysql://localhost/DBname"; //changed localhost to my IP address
but then I get "Communications link failure".
For issue n. 2: I have literally no idea how to solve this. I've never coded a program that needs access to a DB so I had to improvise a bit for that.
About Issue #2:
There is no secure way of storing the password inside the code itself. You can of course try to encrypt the password, but then your code has to decrypt it when the connection is established and therefore the encryption key is visible virtually "to all that have access to your source code". With this key, it is possible to get to the real password, just a little bit more complicated.
The only secure way is to have the user enter the login credentials by his own. Either low level (program arguments when starting your application) or by some form of "login dialog", if the application has a GUI.
A third option would be to create a technical user with restricted DB access, depending on the application you are working on. But this usually causes security issues.
You could create your application such that it sends an https request and authenticate itself against a webserver. What you use to authenticate is up to you: Client IP, username, password, client certificates, ...
Once authenticated, your webserver could transfer a one-time username/password that the client uses to login into your database.
The advantage here is that you can still control whether the user gets full or restricted access, or gets no password any more for whatever reason. And there is no security hole in your application.
1) Most Internet providers don’t allow ordinary users to accept incoming socket connections, both for security reasons and because the network traffic can quickly overwhelm consumer grade networks. You will have to either purchase a commercial Internet connection which allows incoming connections, or look for a server you can lease or borrow. I’m afraid I don’t know what options are available.
2) As MrFreeze correctly pointed out, there is no way to safely embed credentials in an application. No matter what you do to obscure your database login credentials, someone can always decompile your program and figure out how you are decrypting those credentials. The only truly safe solution is to tell users you trust what the credentials are, then write your application so the user must enter them.
Side note: Class.forName("com.mysql.cj.jdbc.Driver"); has not been needed for many years. You can remove that line.
I need to write a program that let's an authenticated user to change its password on Active Directory at Windows Server 2003 functional level.
By requirements of our security manager my program must use LDAP, but it can't bind with an administrator account nor a delegated account.
I need to bind to LDAP with credentials provided by the user and with that connection modify the password.
I'm using java with UnboundID. Code as follow:
LDAPConnection connection = new LDAPConnection(new SSLUtil(new TrustAllTrustManager()).createSSLSocketFactory("SSLv3"), "dc.mydomain.loc", 636);
Modification modification = new Modification(ModificationType.REPLACE, "unicodePwd", ('"' + newPassword + '"').getBytes("UTF-16LE"));
connection.bind(userDN, oldPassword);
connection.modify(userDN, modification);
I'm sure userDN and oldPassword are correct as the bind operation terminates successfully. But when I run the modification I get the following error:
LDAPException(resultCode=50 (insufficient access rights), diagnosticMessage='00000005: SecErr: DSID-031A0F44, problem 4003 (INSUFF_ACCESS_RIGHTS), data 0', ldapSDKVersion=4.0.4, revision=27051)
at com.unboundid.ldap.sdk.LDAPConnection.modify(LDAPConnection.java:2881)
This question may seems duplicate, but it’s really not. I have read many of other questions and answers from stackoverflow and also other website but non would match to my requirement.
In most of proxy questions, the matter is just to connect from X => Z going through Y as proxy.
My requirement is to change the query coming from X in proxy(Y) and send it to Z and then getting the result back to the X.
Here is a little more explanation:
I want to make a daemon service which act as proxy (localhost:3310) for MySQL database(localhost:3306) no clients know the address of database, they just know the ip address of proxy thinking it’s the database. So they try connection to it by hostname(localhost:3310) and valid MySQL username and password.
Now when a query comes to the port 3310, the proxy which is to be in java just changes the query say for example (select name from employee; => select substr(name,1,3) from employee;) and send the changed query to the MySQL Server and get the reply back to the user application (Desktop/Web).
(How) Is it possible to do so in Java?
I have 2 database: harrington, which I created myself and pacsdb which I inherited. Both of these 2 are located in the same MySQL database. I am using Netbeans to debug things. In Netbeans I can connect to the database by
jdbc:mysql://192.168.0.100:3306/harrington?zeroDateTimeBehavior=convertToNull [fiji on Default schema]
jdbc:mysql://192.168.0.100:3306/pacsdb?zeroDateTimeBehavior=convertToNull [pacs on Default schema]
jdbc:mysql://localhost:3306/pacsdb?zeroDateTimeBehavior=convertToNull [pacs on Default schema]
To my own database, harrington, I have no problems either in Netbeans or in my own Java program. Pacsdb will not connect using 192.168.0.100, but it will connect using localhost. In my program it gives
java.sql.SQLException: Access denied for user 'pacs'#'192.168.0.100' (using password: YES)
To check what is going on I queried the sql database table 'user'. I got that both users use '%' as Host which is what I would expect from the code
sql = "create user 'pacs'#'%' identified by 'pacs'";
OK1 = executeStatement(stm1, sql);
if(OK1) {
sql = "grant all on pacsdb.* to 'pacs'#'%' identified ";
sql += "by 'pacs1' with grant option";
executeStatement(stm1, sql);
sql = "flush privileges";
executeStatement(stm1, sql);
}
I did find a difference when I queried the sql 'db' table. There I got 2 entries
Host Db User
% harrington fiji
localhost pacsdb pacs
I tried to delete the entry for pacsdb with localhost and insert a new entry for pacsdb with Host set to '%'. Apparently that isn't way to change from localhost, because I had difficulty to reach pacsdb. I deleted the entry from 'db' and inserted a new entry with '%' set back to 'localhost'. That got me back on line with the access denied error.
So my question is: how do I change from 'localhost' to '%'?
The problem is not the query, but the connection to the database.
MySql has the possibility to limit the access from users accessing it from an IP different from localhost.
Probably you have not the right to access the database from your ip.
Try to add the correct right with the following command
GRANT ALL PRIVILEGES
ON database.*
TO 'user'#'*'
IDENTIFIED BY 'newpassword';
Obviously you can't connect remotely to do that command. You need to enter via SSH on the remote machine and launch that command locally.
Is it possible to connect to a MySQL database without specifying the username and password in Java code :
Connection con=DriverManager.getConnection("database url","username","password");
or is there any way to change the username and password using Java?
There is another method of the same name to connect anonymously (without username or password):
Connection con=DriverManager.getConnection("database url");
Source: https://docs.oracle.com/javase/7/docs/api/java/sql/DriverManager.html
I don't know if you can setup a default user, but you can most certainly set up a user with no password.
As for the second question, assuming your connection has adequet rights in MySQL, you can most certainly set the password on any user. The most basic way to do that is to do:
update user set password=password('new_pass') where user = 'someuser';
Optionally you may want to specify the host field as well in the where clause. This needs to be ran in the msql database.