Mysql connecting ok with local host but not connecting with ip address
I have a java program which takes its information from MySQL it works fine when I use localhost to connect to it but whenever i put ipaddress in it it does not work.
My connection code and exception are as follows.
package connection;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;
/**
*
* @author rpsal
*/
public class DBConnection {
public static Connection connect()//working on local host
{
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://"+getIpAddress()+"/ChatMaster";
conn = DriverManager.getConnection(url, "root", "");
} catch (Exception e) {
System.out.println("Exception in connect" + e);
}
return conn;
}
public static String getIpAddress() throws Exception {
InetAddress inetAddress = InetAddress.getLocalHost();
return inetAddress.getHostAddress();
}
}
When i use String url = "jdbc:mysql:///ChatMaster";
it works fine.
The exception i am getting is as follows.
Exception in connectjava.sql.SQLException: null, message from server: "Host 'Rp-Salh' is not allowed to connect to this MySQL server"
java mysql localhost ip-address
add a comment |
I have a java program which takes its information from MySQL it works fine when I use localhost to connect to it but whenever i put ipaddress in it it does not work.
My connection code and exception are as follows.
package connection;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;
/**
*
* @author rpsal
*/
public class DBConnection {
public static Connection connect()//working on local host
{
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://"+getIpAddress()+"/ChatMaster";
conn = DriverManager.getConnection(url, "root", "");
} catch (Exception e) {
System.out.println("Exception in connect" + e);
}
return conn;
}
public static String getIpAddress() throws Exception {
InetAddress inetAddress = InetAddress.getLocalHost();
return inetAddress.getHostAddress();
}
}
When i use String url = "jdbc:mysql:///ChatMaster";
it works fine.
The exception i am getting is as follows.
Exception in connectjava.sql.SQLException: null, message from server: "Host 'Rp-Salh' is not allowed to connect to this MySQL server"
java mysql localhost ip-address
I think the SQL Server is configured, that all request from outsite (except locahost) get blocked.
– Spirit
Nov 23 at 6:37
I need it to work for any systems on my network, that's the main problem.
– Nnnnn
Nov 23 at 6:42
You need to change the Database config.
– Spirit
Nov 23 at 6:45
add a comment |
I have a java program which takes its information from MySQL it works fine when I use localhost to connect to it but whenever i put ipaddress in it it does not work.
My connection code and exception are as follows.
package connection;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;
/**
*
* @author rpsal
*/
public class DBConnection {
public static Connection connect()//working on local host
{
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://"+getIpAddress()+"/ChatMaster";
conn = DriverManager.getConnection(url, "root", "");
} catch (Exception e) {
System.out.println("Exception in connect" + e);
}
return conn;
}
public static String getIpAddress() throws Exception {
InetAddress inetAddress = InetAddress.getLocalHost();
return inetAddress.getHostAddress();
}
}
When i use String url = "jdbc:mysql:///ChatMaster";
it works fine.
The exception i am getting is as follows.
Exception in connectjava.sql.SQLException: null, message from server: "Host 'Rp-Salh' is not allowed to connect to this MySQL server"
java mysql localhost ip-address
I have a java program which takes its information from MySQL it works fine when I use localhost to connect to it but whenever i put ipaddress in it it does not work.
My connection code and exception are as follows.
package connection;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;
/**
*
* @author rpsal
*/
public class DBConnection {
public static Connection connect()//working on local host
{
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://"+getIpAddress()+"/ChatMaster";
conn = DriverManager.getConnection(url, "root", "");
} catch (Exception e) {
System.out.println("Exception in connect" + e);
}
return conn;
}
public static String getIpAddress() throws Exception {
InetAddress inetAddress = InetAddress.getLocalHost();
return inetAddress.getHostAddress();
}
}
When i use String url = "jdbc:mysql:///ChatMaster";
it works fine.
The exception i am getting is as follows.
Exception in connectjava.sql.SQLException: null, message from server: "Host 'Rp-Salh' is not allowed to connect to this MySQL server"
java mysql localhost ip-address
java mysql localhost ip-address
asked Nov 23 at 6:33
Nnnnn
267
267
I think the SQL Server is configured, that all request from outsite (except locahost) get blocked.
– Spirit
Nov 23 at 6:37
I need it to work for any systems on my network, that's the main problem.
– Nnnnn
Nov 23 at 6:42
You need to change the Database config.
– Spirit
Nov 23 at 6:45
add a comment |
I think the SQL Server is configured, that all request from outsite (except locahost) get blocked.
– Spirit
Nov 23 at 6:37
I need it to work for any systems on my network, that's the main problem.
– Nnnnn
Nov 23 at 6:42
You need to change the Database config.
– Spirit
Nov 23 at 6:45
I think the SQL Server is configured, that all request from outsite (except locahost) get blocked.
– Spirit
Nov 23 at 6:37
I think the SQL Server is configured, that all request from outsite (except locahost) get blocked.
– Spirit
Nov 23 at 6:37
I need it to work for any systems on my network, that's the main problem.
– Nnnnn
Nov 23 at 6:42
I need it to work for any systems on my network, that's the main problem.
– Nnnnn
Nov 23 at 6:42
You need to change the Database config.
– Spirit
Nov 23 at 6:45
You need to change the Database config.
– Spirit
Nov 23 at 6:45
add a comment |
6 Answers
6
active
oldest
votes
As the error tells you, that the ip Adress hasn't the rights to access this database, I think it is not your code which is wrong.
I don't know if it is the same for MySQL, but for postgresql I
needed to define in the database to allow remote connections.
thanks man for pointing me to the right direction +1 for that.
– Nnnnn
Nov 23 at 8:37
add a comment |
I think inetAddress.getHostAdress()
will return the host name (suh as Rp-Salh)
So I recommend you to use this method
inetAddress.getLocalHost()
But I need to use my database for everyone on my network. And database is only on one system. What I think local Host may not work
– Nnnnn
Nov 23 at 7:20
add a comment |
As I can see from the error log. The InetAddress.getLocalHost();
is not returning the correct IP address.
Please try connection it by providing hard-coded IP address (Just for testing to get sure).
You can get system IP address in windows by typing ipconfig
on CMD.
I checked what you said and there isn't any problem with getting the right ipaddress. I double checked first by manually putting the ipaddress and then checking what my function was returning.
– Nnnnn
Nov 23 at 8:03
add a comment |
You need to make sure 2 things.
Check MySQl port
3306
is already opened or not. Here are sample remote connection String
String url = "jdbc:mysql://192.168.1.121:3306/ChatMaster";
Check database user name and password is correct.
Port 3306 is open and username is also correct as it works with localhost and I haven't put any passwords yet.
– Nnnnn
Nov 23 at 7:22
add a comment |
Update mysql config file (probably in server file directory etc/mysql/my.conf) check if it is configured with 127.0.0.1(default) as a host address and change it to your IP.
add a comment |
As it turns out @Jan. St 's pointed me to the right direction as the problem wasn't in my code or any of getting ipaddress problem it was just that by default remote root access is disabled in mysql. I just followed the answer in the following link and it worked.
How to allow remote connection to mysql
Note: make sure you also follow 2nd answer in the same post if first answer on its own did not work.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441646%2fmysql-connecting-ok-with-local-host-but-not-connecting-with-ip-address%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
As the error tells you, that the ip Adress hasn't the rights to access this database, I think it is not your code which is wrong.
I don't know if it is the same for MySQL, but for postgresql I
needed to define in the database to allow remote connections.
thanks man for pointing me to the right direction +1 for that.
– Nnnnn
Nov 23 at 8:37
add a comment |
As the error tells you, that the ip Adress hasn't the rights to access this database, I think it is not your code which is wrong.
I don't know if it is the same for MySQL, but for postgresql I
needed to define in the database to allow remote connections.
thanks man for pointing me to the right direction +1 for that.
– Nnnnn
Nov 23 at 8:37
add a comment |
As the error tells you, that the ip Adress hasn't the rights to access this database, I think it is not your code which is wrong.
I don't know if it is the same for MySQL, but for postgresql I
needed to define in the database to allow remote connections.
As the error tells you, that the ip Adress hasn't the rights to access this database, I think it is not your code which is wrong.
I don't know if it is the same for MySQL, but for postgresql I
needed to define in the database to allow remote connections.
answered Nov 23 at 6:39
Jan. St.
213
213
thanks man for pointing me to the right direction +1 for that.
– Nnnnn
Nov 23 at 8:37
add a comment |
thanks man for pointing me to the right direction +1 for that.
– Nnnnn
Nov 23 at 8:37
thanks man for pointing me to the right direction +1 for that.
– Nnnnn
Nov 23 at 8:37
thanks man for pointing me to the right direction +1 for that.
– Nnnnn
Nov 23 at 8:37
add a comment |
I think inetAddress.getHostAdress()
will return the host name (suh as Rp-Salh)
So I recommend you to use this method
inetAddress.getLocalHost()
But I need to use my database for everyone on my network. And database is only on one system. What I think local Host may not work
– Nnnnn
Nov 23 at 7:20
add a comment |
I think inetAddress.getHostAdress()
will return the host name (suh as Rp-Salh)
So I recommend you to use this method
inetAddress.getLocalHost()
But I need to use my database for everyone on my network. And database is only on one system. What I think local Host may not work
– Nnnnn
Nov 23 at 7:20
add a comment |
I think inetAddress.getHostAdress()
will return the host name (suh as Rp-Salh)
So I recommend you to use this method
inetAddress.getLocalHost()
I think inetAddress.getHostAdress()
will return the host name (suh as Rp-Salh)
So I recommend you to use this method
inetAddress.getLocalHost()
answered Nov 23 at 6:39
Jin
920221
920221
But I need to use my database for everyone on my network. And database is only on one system. What I think local Host may not work
– Nnnnn
Nov 23 at 7:20
add a comment |
But I need to use my database for everyone on my network. And database is only on one system. What I think local Host may not work
– Nnnnn
Nov 23 at 7:20
But I need to use my database for everyone on my network. And database is only on one system. What I think local Host may not work
– Nnnnn
Nov 23 at 7:20
But I need to use my database for everyone on my network. And database is only on one system. What I think local Host may not work
– Nnnnn
Nov 23 at 7:20
add a comment |
As I can see from the error log. The InetAddress.getLocalHost();
is not returning the correct IP address.
Please try connection it by providing hard-coded IP address (Just for testing to get sure).
You can get system IP address in windows by typing ipconfig
on CMD.
I checked what you said and there isn't any problem with getting the right ipaddress. I double checked first by manually putting the ipaddress and then checking what my function was returning.
– Nnnnn
Nov 23 at 8:03
add a comment |
As I can see from the error log. The InetAddress.getLocalHost();
is not returning the correct IP address.
Please try connection it by providing hard-coded IP address (Just for testing to get sure).
You can get system IP address in windows by typing ipconfig
on CMD.
I checked what you said and there isn't any problem with getting the right ipaddress. I double checked first by manually putting the ipaddress and then checking what my function was returning.
– Nnnnn
Nov 23 at 8:03
add a comment |
As I can see from the error log. The InetAddress.getLocalHost();
is not returning the correct IP address.
Please try connection it by providing hard-coded IP address (Just for testing to get sure).
You can get system IP address in windows by typing ipconfig
on CMD.
As I can see from the error log. The InetAddress.getLocalHost();
is not returning the correct IP address.
Please try connection it by providing hard-coded IP address (Just for testing to get sure).
You can get system IP address in windows by typing ipconfig
on CMD.
answered Nov 23 at 6:43
DK Ansh
488
488
I checked what you said and there isn't any problem with getting the right ipaddress. I double checked first by manually putting the ipaddress and then checking what my function was returning.
– Nnnnn
Nov 23 at 8:03
add a comment |
I checked what you said and there isn't any problem with getting the right ipaddress. I double checked first by manually putting the ipaddress and then checking what my function was returning.
– Nnnnn
Nov 23 at 8:03
I checked what you said and there isn't any problem with getting the right ipaddress. I double checked first by manually putting the ipaddress and then checking what my function was returning.
– Nnnnn
Nov 23 at 8:03
I checked what you said and there isn't any problem with getting the right ipaddress. I double checked first by manually putting the ipaddress and then checking what my function was returning.
– Nnnnn
Nov 23 at 8:03
add a comment |
You need to make sure 2 things.
Check MySQl port
3306
is already opened or not. Here are sample remote connection String
String url = "jdbc:mysql://192.168.1.121:3306/ChatMaster";
Check database user name and password is correct.
Port 3306 is open and username is also correct as it works with localhost and I haven't put any passwords yet.
– Nnnnn
Nov 23 at 7:22
add a comment |
You need to make sure 2 things.
Check MySQl port
3306
is already opened or not. Here are sample remote connection String
String url = "jdbc:mysql://192.168.1.121:3306/ChatMaster";
Check database user name and password is correct.
Port 3306 is open and username is also correct as it works with localhost and I haven't put any passwords yet.
– Nnnnn
Nov 23 at 7:22
add a comment |
You need to make sure 2 things.
Check MySQl port
3306
is already opened or not. Here are sample remote connection String
String url = "jdbc:mysql://192.168.1.121:3306/ChatMaster";
Check database user name and password is correct.
You need to make sure 2 things.
Check MySQl port
3306
is already opened or not. Here are sample remote connection String
String url = "jdbc:mysql://192.168.1.121:3306/ChatMaster";
Check database user name and password is correct.
answered Nov 23 at 7:00
Sai Ye Yan Naing Aye
4,89173456
4,89173456
Port 3306 is open and username is also correct as it works with localhost and I haven't put any passwords yet.
– Nnnnn
Nov 23 at 7:22
add a comment |
Port 3306 is open and username is also correct as it works with localhost and I haven't put any passwords yet.
– Nnnnn
Nov 23 at 7:22
Port 3306 is open and username is also correct as it works with localhost and I haven't put any passwords yet.
– Nnnnn
Nov 23 at 7:22
Port 3306 is open and username is also correct as it works with localhost and I haven't put any passwords yet.
– Nnnnn
Nov 23 at 7:22
add a comment |
Update mysql config file (probably in server file directory etc/mysql/my.conf) check if it is configured with 127.0.0.1(default) as a host address and change it to your IP.
add a comment |
Update mysql config file (probably in server file directory etc/mysql/my.conf) check if it is configured with 127.0.0.1(default) as a host address and change it to your IP.
add a comment |
Update mysql config file (probably in server file directory etc/mysql/my.conf) check if it is configured with 127.0.0.1(default) as a host address and change it to your IP.
Update mysql config file (probably in server file directory etc/mysql/my.conf) check if it is configured with 127.0.0.1(default) as a host address and change it to your IP.
answered Nov 23 at 8:36
Kumar Gaurav Sharma
59112
59112
add a comment |
add a comment |
As it turns out @Jan. St 's pointed me to the right direction as the problem wasn't in my code or any of getting ipaddress problem it was just that by default remote root access is disabled in mysql. I just followed the answer in the following link and it worked.
How to allow remote connection to mysql
Note: make sure you also follow 2nd answer in the same post if first answer on its own did not work.
add a comment |
As it turns out @Jan. St 's pointed me to the right direction as the problem wasn't in my code or any of getting ipaddress problem it was just that by default remote root access is disabled in mysql. I just followed the answer in the following link and it worked.
How to allow remote connection to mysql
Note: make sure you also follow 2nd answer in the same post if first answer on its own did not work.
add a comment |
As it turns out @Jan. St 's pointed me to the right direction as the problem wasn't in my code or any of getting ipaddress problem it was just that by default remote root access is disabled in mysql. I just followed the answer in the following link and it worked.
How to allow remote connection to mysql
Note: make sure you also follow 2nd answer in the same post if first answer on its own did not work.
As it turns out @Jan. St 's pointed me to the right direction as the problem wasn't in my code or any of getting ipaddress problem it was just that by default remote root access is disabled in mysql. I just followed the answer in the following link and it worked.
How to allow remote connection to mysql
Note: make sure you also follow 2nd answer in the same post if first answer on its own did not work.
answered Nov 23 at 8:36
Nnnnn
267
267
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441646%2fmysql-connecting-ok-with-local-host-but-not-connecting-with-ip-address%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I think the SQL Server is configured, that all request from outsite (except locahost) get blocked.
– Spirit
Nov 23 at 6:37
I need it to work for any systems on my network, that's the main problem.
– Nnnnn
Nov 23 at 6:42
You need to change the Database config.
– Spirit
Nov 23 at 6:45