public Connection conn() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException
{
Connection all_connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
all_connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/dbname","username","password");
return all_connection;
}
Above code is my connection code. Its working fine. If i replace localhost by my ip address, its not working. Actually i want to replace localhost by ip address. Replacing localhost by 127.0.0.1 works fine. Why replacing ip address is not working????
This is probably an issue with your database user. (I can't tell for sure because you didn't post the actual error)
You'll need to verify that the user you're logging in as is allowed to access is allowed to access the database from an external IP. It seems likely that you're using the root user, which is allowed access from localhost, 127.0.0.1, and ::1, but not from external IPs.
If you are, make sure you set up a new DB user with only the required access, and allow access from the specific IP, or % (any host)
I have faced same issue while I am new to this.
IP address means other DB server's IP ?
If yes then check firewall that is it allow Incoming connection for 3306 port ?
If it is not then ad new rule in that server machine with 3306 connection post allow.
and which OS in server machine ?
I have done like following in postgresql :
jdbc:postgresql://192.168.1.9:5432/dbName
In your case it would be :
jdbc:mysql://192.168.1.9:3306/dbName
I suggest you to first of all check that machine's firewall port rules then go for next.
And be sure that you have written true user name and password.
First check whether your machine is configured for the IP address you are trying.
Try to ping it from other machine.
Try to nslookup for the same IP address.
If both the results are positive, means that your machine is configured for the IP address,
If any one fails, try to configure your ip, and then try again.
Connection object = DriverManager.getConnection("jdbc:mysql://127.xx.xx.x:3306/dbname","username","password");
use above code after configuration.
By default in some database server remote access is disabled. You have to enable remote access.
If you are using mySQL here is the solution.
1.Go to my sql bin folder or add it to PATH
2.Login to root by mysql -uroot -proot (or whatever the root password is.)
3.On success you will get mysql>
4.Provide grant access all for that user.
query to enable access.
GRANT ALL PRIVILEGES ON *.* TO 'username'#'IP' IDENTIFIED BY 'password';
Here IP is IP address for which you want to allow remote access, if we put % any IP address can access remotely.
Related
I am frustrated seeing this error and not knowing the solution. I am trying to connect to a mysql db with a server url but it is giving my mysqlException(stacktrace below). The code works fine till here:
String dbUrl = "jdbc:mysql://server_url/db_name";
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
String user = "user";
String password = "password";
conn = DriverManager.getConnection(dbUrl,user,password);
This is the error I'm getting
java.sql.SQLException: null, message from server: "Host '172.23.251.154' is not allowed to connect to this MySQL server"
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1070)
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:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
Is this because I'm using a different version of mysql-connector jar?
Please help me.
From Chapter 6. SQL Questions: How Do I Enable TCP Connections to MySQL?,
By default, MySQL won't allow ANY users access to any of the databases if they connect over a TCP connection. In order to permit the connection, you must create a entry in the user table of the mysql database (make sure you select the PASSWORD function to encrypt your password). In particular, the Host field needs to indicate which host(s) are permitted to connect. If you specify % (which I would not recommend), then a user would be able to connect from any host.
What about firewall on port 3306?
May be the user you are trying to access doesn't have enough privileges to access the database from the given server.
So try providing GRANTS to the user.
GRANT ALL PRIVILEGES ON database_name TO 'user'#'hostname' IDENTIFIED BY PASSWORD <password>;
FLUSH PRIVILEGES;
here hostname can be your host address "172.23.251.154". Dont forget to flush privileges and then try connecting the server.
If your MySql server is located on your host provider there is option in your control panel where you can set ip addresses from which you can connect to mysql server in your case 172.23.251.154 if this is your ip address
I am trying to connect to MySQL from JDBC via localhost. But the connection fails. In the exception, I see that JDBC is trying to connect to 127.0.0.1
String connectionString = "";
try {
loadProperties();
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connectionString = "jdbc:mysql://" + properties.getProperty("host") + "/" + properties.getProperty
("database") + "?user=" + properties.getProperty("user") + "&password=" + properties
.getProperty
("password");
connect = DriverManager
.getConnection(connectionString);
logger.debug("Connected to " + properties.getProperty("host"));
} catch (Exception e) {
logger.error("Database Connection failed with connection string - " + connectionString,e);
}
From the log:
Database Connection failed with connection string - jdbc:mysql://localhost/testdb?user=testuser&password=testpass
java.sql.SQLException: Access denied for user 'testuser'#'127.0.0.1' (using password: YES)
Why is it replacing localhost with 127.0.0.1? I have configured login only for localhost.
I stumbled across this question when encountering the same issue.
To answer the question "Why is it replacing localhost with 127.0.0.1?":
From the MySQL docs, using localhost in your connection URL implies that you want to connect to a socket. Using 127.0.0.1 implies that you want to connect through TCP/IP.
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. ... To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1
According to this answer, it appears that by default JDBC only supports TCP/IP connections, at least for some Java versions. Original source: http://lists.mysql.com/java/8749:
Java itself doesn't support unix domain sockets
So, I'd guess that since JDBC only connects through TCP/IP, it converts localhost to 127.0.0.1 internally.
To solve the problem in my case:
I granted permission in MySQL for user#127.0.0.1
I changed localhost to 127.0.0.1 in my connection URL.
The IP address 127.0.0.1 is a special purpose address reserved for use on each computer. 127.0.0.1 is conventionally a computer's loopback address.
Network software and utilities can use 127.0.0.1 to access a local computer's TCP/IP network resources. Messages sent to loopback IP addresses like 127.0.0.1 do not reach outside to the local area network (LAN) but instead are automatically re-routed by the computer's own network adapter back to the receiving end of the TCP/IP stack. In simple words, localhost can also be referred as 127.0.0.1. There is a problem with MySql access privileges. This link would help you resolve it
I have two computers on one network. I have entered the ip of another computer, it works fine in the browser, but when using this ip on java I found the database connected to my localhost database not from the other computer!
my code jdbc
public Connection MakeConnect() throws ClassNotFoundException, SQLException{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection(
"jdbc:oracle:thin:http://192.168.1.109:5560/isqlplus", "school",
"sch"); // first : user(hr) second pass(hr) .!
return connection ; // return connetion of database
I think your JDBC connection string may be in a wrong format. You should provide this in the following way:
jdbc:oracle:thin:#[HOST][:PORT]:SID
jdbc:oracle:thin:#//[HOST][:PORT]/SERVICE
Instead it looks you specified ISQLPLus link. I haven't tested it but my guts say me it is the reason. I think in your case it should be something like that:
jdbc:oracle:thin:#192.168.1.109/SERVICEorSID
And you have to find the service name by yourself. In a generic case it is a name of the database. By default it is usually set to ORCL but possibly you set it to another name.
Make sure you read a following document: http://www.orafaq.com/wiki/JDBC
If the connection succeeds without error and if this 192.168.1.109 ip address is not of your local machine then it must connect to some other machine.
What made you think it's connecting to the local DB instance?
Check firewall settings of the other computer. Sometimes it may not allows for the external connections
If you want to allow a specific client ip-address (for example: 192.168.1.4) to access the mysql database running on a server, you should execute the following command on the server that is running the mysql database.
$ mysql -u root -p
Enter password:
mysql> use mysql
mysql> GRANT ALL ON *.* to root#'192.168.1.4' IDENTIFIED BY 'your-root-password';
mysql> FLUSH PRIVILEGES;
Also, update firewall rules to make sure port# 3306 is open on the server that is running the mysql database.
I have a remote mysql database server setup on a machine myuniversity.edu and server is named 'localhost'. On it I have the database named 'MyDatabase'.
I want to connect it through Java.
The connection urls that I have tried are:
jdbc:mysql://myuniversity.edu/localhost
jdbc:mysql://myuniversity.edu/localhost/MyDatabase
jdbc:mysql://myuniversity.edu:3306/MyDatabase
but I get the `Connection refused: connect` exception.
Could someone please tell what the connection url should be in this case?
Not really sure if your machine name is myuniversity.edu, you can instead try the IP Address with the connection string, Localhost is the name for loopback network interface and accessible on that machine only. Also make sure if your default port for mysql (may be 3306) is open. With IP address your connection string would look like:
jdbc:mysql://192.168.0.123/MyDatabase
With IP and port it would be:
jdbc:mysql://192.168.0.123:3306/MyDatabase
(You need to replace your IP in the above string)
I'ts impossible to connect remotely without (IP) address
try this approach
if you want to connect it via internet :
OPEN CMD on your computer
in CMD write ping myuniversity.edu (for example ping google.com)
then you will get an ip address of the website and you can copy the ip
then try this approach :
Connection con;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://THE IP ADDRESS :3306/DatabaseName");
System.out.println("CONNECTED");
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
Ok so here's what I did to fix the issue:
In my.cnf file, I changed the bind-address from '127.0.0.1' to the
'host ipaddress'. This allows connecting to the remote mysql server
but would not allow access for any remote host trying to connect to
it.
To fix that, I added an entry in user table with host '%'. This allows remote hosts to connect to the database.
Now I can connect to the database with jdbc:mysql://serverIpAddress:3306/MyDatabase
I am trying to connect to MySQL database over a network. I have installed MySQL, and the service is running on the default port. I have also install the SQL connector to the jar file and added java JDK to the server machine. I am able to connect to my local database using the code:
private String dbUrl = "jdbc:mysql://localhost/DatabaseName";
private String dbClass = "com.mysql.jdbc.Driver";
But when I try and connect to it over the network, with the IP address (eg: 192.168.1.45):
private String dbUrl = "jdbc:mysql://192.168.1.45/DatabaseName";
private String dbClass = "com.mysql.jdbc.Driver";
I get the connection error:
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Does anyone know what this issue is? Do I need to add a different address?
I have added the default port with the address but cannot get it to work.
Thanks for any help.
By default, MySQL doesnt allow remote access and only allow local access.
You will have to modify your /etc/mysql/my.cnf config (on Linux) with:
bind-address = 192.168.1.45 // Your Server IP address
# skip-networking // This should be commented .
See the whole procedure here.
Check the my.cnf [mysqld] settings for the parameters port, bind-address, socket, to make sure these aren't causing problems.
Check the files /etc/hosts, /etc/hosts.deny to make sure everything is ok.
Check firewall applications
Check to make sure whatever directory mysqld's sockets are have the appropriate permissions.
Check to make sure that security settings within the mysql database (user table) permit access from your remote host.
Make sure you can telnet OK to localhost 3306, 127.0.0.1 3306, and whatever other IP address your machine is configured to (use ifconfig to find out).
You can test the server setup using the MySQL Workbench or mysql client which will narrow down the problem. It's also sometimes useful to just see if the server's there:
telnet host 3306
It'll tell you the version number of the server and some other binary junk. Enough to know your host is listening.