Steps to access MySQL in different server via remote machine

Steps to access MySQL in different server via remote machine:

Give grant permission to the IP where MySQL is running:
o   CREATE USER 'ktpot'@'10.232.74.86' IDENTIFIED BY 'openktpot';
o   GRANT ALL PRIVILEGES ON *.* TO 'ktpot'@'10.232.74.86' WITH GRANT OPTION;

Give grant permission to the remote host IP:
o   CREATE USER 'ktpot'@'10.232.56.14' IDENTIFIED BY 'openktpot';
o   GRANT ALL PRIVILEGES ON *.* TO 'ktpot'@'10.232.56.14' WITH GRANT OPTION;

Also, we can also use the below GRANT commands:


GRANT ALL PRIVILEGES ON *.* TO 'ktpot'@'localhost' WITH GRANT OPTION;


GRANT USAGE ON *.* TO 'ktpot'@'localhost'


GRANT CREATE,SELECT, INSERT, UPDATE , DELETE, DROP,ALTER ON *.* TO 'ktpot'@'localhost';
Run the below code to check the MySQL connection

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class MySQLConnection {

      /**
      * @param args
      */
      public static void main(String[] args) {
            // TODO Auto-generated method stub

            Connection conn = null;
            try{
                  Class.forName("com.mysql.jdbc.Driver").newInstance();
                  String url = "jdbc:mysql://10.232.74.75:3306/ktpot";
                  String user = "ktpot";
                  String password = "openktpot";
                  conn =  DriverManager.getConnection(url, user, password);
                  System.out.println("CONNECTED to ktpot");


            }
            catch (SQLException e) {
                  e.printStackTrace();
            }
            catch(ClassNotFoundException e){
                  e.printStackTrace();
            }catch(InstantiationException e){
                  e.printStackTrace();
            }catch(IllegalAccessException e){
                  e.printStackTrace();
            }
      }

}

0 comments: