Mysql connecting ok with local host but not connecting with ip address












1















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"









share|improve this question























  • I think the SQL Server is configured, that all request from outsite (except locahost) get blocked.

    – Spirit
    Nov 23 '18 at 6:37











  • I need it to work for any systems on my network, that's the main problem.

    – Nnnnn
    Nov 23 '18 at 6:42











  • You need to change the Database config.

    – Spirit
    Nov 23 '18 at 6:45
















1















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"









share|improve this question























  • I think the SQL Server is configured, that all request from outsite (except locahost) get blocked.

    – Spirit
    Nov 23 '18 at 6:37











  • I need it to work for any systems on my network, that's the main problem.

    – Nnnnn
    Nov 23 '18 at 6:42











  • You need to change the Database config.

    – Spirit
    Nov 23 '18 at 6:45














1












1








1








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"









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 6:33









NnnnnNnnnn

268




268













  • I think the SQL Server is configured, that all request from outsite (except locahost) get blocked.

    – Spirit
    Nov 23 '18 at 6:37











  • I need it to work for any systems on my network, that's the main problem.

    – Nnnnn
    Nov 23 '18 at 6:42











  • You need to change the Database config.

    – Spirit
    Nov 23 '18 at 6:45



















  • I think the SQL Server is configured, that all request from outsite (except locahost) get blocked.

    – Spirit
    Nov 23 '18 at 6:37











  • I need it to work for any systems on my network, that's the main problem.

    – Nnnnn
    Nov 23 '18 at 6:42











  • You need to change the Database config.

    – Spirit
    Nov 23 '18 at 6:45

















I think the SQL Server is configured, that all request from outsite (except locahost) get blocked.

– Spirit
Nov 23 '18 at 6:37





I think the SQL Server is configured, that all request from outsite (except locahost) get blocked.

– Spirit
Nov 23 '18 at 6:37













I need it to work for any systems on my network, that's the main problem.

– Nnnnn
Nov 23 '18 at 6:42





I need it to work for any systems on my network, that's the main problem.

– Nnnnn
Nov 23 '18 at 6:42













You need to change the Database config.

– Spirit
Nov 23 '18 at 6:45





You need to change the Database config.

– Spirit
Nov 23 '18 at 6:45












6 Answers
6






active

oldest

votes


















1














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.






share|improve this answer
























  • thanks man for pointing me to the right direction +1 for that.

    – Nnnnn
    Nov 23 '18 at 8:37



















1














I think inetAddress.getHostAdress() will return the host name (suh as Rp-Salh)
So I recommend you to use this method



inetAddress.getLocalHost()





share|improve this answer
























  • 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 '18 at 7:20



















0














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.






share|improve this answer
























  • 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 '18 at 8:03



















0














You need to make sure 2 things.





  1. 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";



  2. Check database user name and password is correct.







share|improve this answer
























  • 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 '18 at 7:22



















0














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.






share|improve this answer































    0














    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.






    share|improve this answer























      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      1














      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.






      share|improve this answer
























      • thanks man for pointing me to the right direction +1 for that.

        – Nnnnn
        Nov 23 '18 at 8:37
















      1














      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.






      share|improve this answer
























      • thanks man for pointing me to the right direction +1 for that.

        – Nnnnn
        Nov 23 '18 at 8:37














      1












      1








      1







      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.






      share|improve this answer













      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.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 23 '18 at 6:39









      Jan. St.Jan. St.

      214




      214













      • thanks man for pointing me to the right direction +1 for that.

        – Nnnnn
        Nov 23 '18 at 8:37



















      • thanks man for pointing me to the right direction +1 for that.

        – Nnnnn
        Nov 23 '18 at 8:37

















      thanks man for pointing me to the right direction +1 for that.

      – Nnnnn
      Nov 23 '18 at 8:37





      thanks man for pointing me to the right direction +1 for that.

      – Nnnnn
      Nov 23 '18 at 8:37













      1














      I think inetAddress.getHostAdress() will return the host name (suh as Rp-Salh)
      So I recommend you to use this method



      inetAddress.getLocalHost()





      share|improve this answer
























      • 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 '18 at 7:20
















      1














      I think inetAddress.getHostAdress() will return the host name (suh as Rp-Salh)
      So I recommend you to use this method



      inetAddress.getLocalHost()





      share|improve this answer
























      • 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 '18 at 7:20














      1












      1








      1







      I think inetAddress.getHostAdress() will return the host name (suh as Rp-Salh)
      So I recommend you to use this method



      inetAddress.getLocalHost()





      share|improve this answer













      I think inetAddress.getHostAdress() will return the host name (suh as Rp-Salh)
      So I recommend you to use this method



      inetAddress.getLocalHost()






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 23 '18 at 6:39









      JinJin

      9311323




      9311323













      • 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 '18 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 '18 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 '18 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 '18 at 7:20











      0














      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.






      share|improve this answer
























      • 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 '18 at 8:03
















      0














      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.






      share|improve this answer
























      • 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 '18 at 8:03














      0












      0








      0







      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.






      share|improve this answer













      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.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 23 '18 at 6:43









      DK AnshDK Ansh

      281216




      281216













      • 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 '18 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 '18 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 '18 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 '18 at 8:03











      0














      You need to make sure 2 things.





      1. 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";



      2. Check database user name and password is correct.







      share|improve this answer
























      • 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 '18 at 7:22
















      0














      You need to make sure 2 things.





      1. 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";



      2. Check database user name and password is correct.







      share|improve this answer
























      • 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 '18 at 7:22














      0












      0








      0







      You need to make sure 2 things.





      1. 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";



      2. Check database user name and password is correct.







      share|improve this answer













      You need to make sure 2 things.





      1. 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";



      2. Check database user name and password is correct.








      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 23 '18 at 7:00









      Sai Ye Yan Naing AyeSai Ye Yan Naing Aye

      4,96173558




      4,96173558













      • 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 '18 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 '18 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 '18 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 '18 at 7:22











      0














      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.






      share|improve this answer




























        0














        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.






        share|improve this answer


























          0












          0








          0







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 8:36









          Kumar Gaurav SharmaKumar Gaurav Sharma

          69112




          69112























              0














              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.






              share|improve this answer




























                0














                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.






                share|improve this answer


























                  0












                  0








                  0







                  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.






                  share|improve this answer













                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 8:36









                  NnnnnNnnnn

                  268




                  268






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      Origin of the phrase “under your belt”?