how to connenct remote mysql connection in cpanel - java

I am trying to connect MySQL database remotely from my local pc since 2 weeks but I failed . www.hdvoiz.com this is where I created my database and I am using Cpanel admin panel . please help me .
here is the Error msg
java.sql.SQLException: Access denied for user 'hdvoiz_user'#'95.218.172.137' (using password: YES)
here is my code
package rmotedb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
public static void main(String[] args) {
System.out.println("-------- MySQL JDBC Connection Testing ------------");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;
try {
String url = "jdbc:mysql://hdvoiz.com:3306/hdvoiz_loy";
String username = "hdvoiz_user";
String password = "**********";
connection = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}

You are using cPanel so you will have to add your ISP ip in remote mysql access list to access your mysql databases from your local PC.
Login your cPanel >> Database >> Remote MySQL >> Add your ISP IP
If you are still facing same issues then contact your hosting provider and ask them to enable mysql port in server firewall.

Verify that mysql are hearing to internet. From you local server 95.218.172.137 run telnet remote_server_or_remote_ip 3306. If telnet show timeout, your server block 3306 (Put 3306 or Port used by mysql if you are changed this port) port. It's a firewall question.
If point 1 show a "Connected to " but can't work your program verify that user are on mysql.user table, with permission for acces from local server IP. You can see this on shell with mysql command.
$ mysql
mysql> SELECT Host,user FROM mysql.user WHERE User LIKE 'user_mysql';
On Cpanel user, you must see, edit or add, GLOBAL permision for all users of this account to access mysql server, usin IP, or IP plus comodin %. If you add a IP or wildcard IP all your user on this account has permission to access remotely orm this Ip or one IP of range wildcard. Also you can verify this with
$ mysql
mysql> SELECT Host,user FROM mysql.user WHERE `Host` LIKE 'IP';

Related

Connect to a remote database server instead of localhost

I'm trying to connect to my WAMP sever by using its IP address in my LAN. So far I have written the following code, and I don't understand why it does not establish the connection.
public class DBConnect {
public static Connection connect()
{
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con=(Connection) DriverManager.getConnection("jdbc:mysql://192.168.1.2:3306/employee101?","root","");
JOptionPane.showMessageDialog(null, "Connection Success");
} catch (Exception e) {
System.out.println("inter.DBConnect.connect()");
}
return con;
}
}
The root mysql account is configured to only allow a connection from the PC running the MySQL Server. For obvious security reasons.
I suggest you go to the WAMPServer machine and login to MySQL as root, then create a new account, that is NOT a Super User account and HAS password. Using phpMyAdmin for this is probably easiest.
Then configure the domain for this account to allow connections from your network only (192.168.1) note only 3 of the 4 quartiles used will allow access from any IP in your network, but only from within your network. Allocate only the required privileges on the specific databases required, to the new account.
Then use this new account in your Java code.

How can I share my database using IP Address?

import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
public class Database {
public static Connection con = null;
public static Connection connectDB() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://192.168.1.8:3306/registerdb", "root", "");
//JOptionPane.showMessageDialog(null, "Connection Successful");
return con;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
How I make my finish program share to others locally or using the internet and how to use the IP address for it? I'm using NetBeans for my java programming and XAMPP for my database.
Please help me.. THanks
Install db server on any computer, get servers IP address and port and pass that information to DriverManager.getConnection("jdbc:mysql://IP:port/dbname")
Its best to use property file to store IP and port and read it when creating connection so it can be easily changed without changing the code.
Your program should work as it is on multiple machines as long as they are in the same local network (as you are using local IP address).
If they are not able to connect to DB, following can be the possible reasons:
DB is not configured to allow connection from another machine. You
can grant it using following syntax:
grant all on db-name.* to username#'%' identified by 'password'
Your firewall is blocking income 3306 port requests. Open the port
for incoming requests.

Connect Java remotely to MYSQL DB on another network

I want to write a java application that uses a mysql database to store and retrieve information. I am still just a beginner and I do not have a lot of knowledge on web hosting providers and server architecture. In this application, several clients will have to access this remote database located on a server machine maybe.
Currently, I can connect to the database from the same computer that runs the mysql server (I am using workbench by the way). Any suggestions on what should I do?
public static Connection openDatabase()
{
Connection myConn;
try {
myConn =
DriverManager.getConnection(Configs.getProperty("DatabaseURL"));
return myConn;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
The url is:
jdbc:mysql://Atomic-PC:3306/test?autoReconnect=true&rewriteBatchedStatements=true&useSSL=false&user=root&password=password
First of all you have to find connection URL, port and credential from MySql workbench you are using. If hosting provider provides a public IP for DB server you should be able to access it from anywhere by using following code in Java.
import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
public static void main(String[] argv) throws Exception {
String driver = "com.mysql.jdbc.Driver";
String connection = "jdbc:mysql://localhost:3306/YourDBName";
String user = "root";
String password = "root";
Class.forName(driver);
Connection con = DriverManager.getConnection(connection, user, password);
if (!con.isClosed()) {
con.close();
}
}
}
For this reason you have to import mysql connection driver to your application first.
If you are trying to connect to remote database then yo need to change the database url from localhost to remote server ip address.
jdbc:mysql://Atomic-PC:3306/test
to
jdbc:mysql://<db-server-ip-address>:<db-server-port>/<db-name>
Assuming, remote server ip address is 10.234.05.123 and database port number is 3300 and database name is remoteDB. Then,
jdbc:mysql://10.234.05.123:3300/remoteDB
your web hosting company should be able to provide you with the url:port which you can use in your connection string to connect to MySQL(or any other remote db for that matter)

Java connection to sql server

Ok so I have this code:
package com.andrewxd.banksystem;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Interface
{
public static void main(String[] args)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1443;user=Andrew;password=andrei23;database=BankSystem");
System.out.println("test");
Statement sta = conn.createStatement();
String Sql = "select * from Clients";
ResultSet rs = sta.executeQuery(Sql);
System.out.println(rs.next());
} catch (Exception e){
e.printStackTrace();
}
}
}
but it gives me this error can somebody help me?:
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1443 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
I've tried searching but I didn't really understand much.
from what i understand the port is incorrect but how do i find the correct ip/port?
1) Open SQL Server configuration manager and check to see if the TCP/IP under Network configuration protocols is enabled.
2) Under properties of the SQL serve under Connections check to see if Allow remote connections to this server is allowed.
3) Check to see if you can connect via SSMS and query the database.
4) In SQL Server Configuration manager check to see if the SQL Server Browser service is running. (this is not enabled by default nor is it set up to start up automatically by default).
5) If all those are set up then I would check the firewall.
(For anyone that might come across this the solution was to allow SQL Server and windows authentication)
Make sure your sql server configuration
-has TCP configured, and configured at that IP address.
You can test a couple things:
turn off Windows Firewall, from services.msc.
try connecting to SQL Server through the IP address,username,pwd the
application is trying to use. (open enterprise manager, connect, enter IP address etc)
Try using jtds driver for SQLServer.
Installing Oracle JDBC driver from Here
And with your code looking like this:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class OracleJDBC {
public static void main(String[] argv) {
System.out.println("-------- Oracle JDBC Connection Testing ------");
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your Oracle JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("Oracle JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:BankSystem", "Andrew","andrei23");
}
catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}
It should probably work, if not, what does the output look like now ? Still equal to what it was before ? Did you check firewalls ? Connectivity to server allowed ?
The standard port for sqlserver is 1433 and not 1443 as far as I recall it right.

Connecting to MySQL Server JDBC

I am trying to connect to a MySQL Server using JDBC tool in java (using eclipse). I was just wondering how to enter 2 user/password combinations. The first one is the one I use to connect to the server(for example when I ssh into the server) and the second one I enter into phpmyadmin. As of now, I am putting in the phpmyadmin password only into the jdbc connection properties and it's not connecting. This is my current statement:
conn = DriverManager.getConnection("jdbc:mysql://[IP of server]:3306/[Database Table name]", "[UserName (same as phpmyadmin)]","[Password (same as phpmyadmin)]");
I am getting a
java.sql.SQLException: null, message from server: "Host '[My computer's full host name]' is not allowed to connect to this MySQL server"
I was just wondering if I needed to enter my server login/password (the one I use for ssh) as well in addition to the phpmyadmin username/pwd. I am new to JDBC and MySQL server, so I would appreciate any tips.
Just for background, I am able to connect successfully through ssh and I can login to the server through phpmyadmin.
Here's how you can create an account that can access your server from another client machine:
CREATE USER 'bobby'#'localhost' IDENTIFIED BY 'some_password';
That creates the user, and says he can connect from localhost.
If he is on the machine 192.168.0.5, you'd do something like this:
CREATE USER 'bobby'#'192.168.0.5' IDENTIFIED BY 'some_password';
Then of course, you have to grant privileges appropriately:
GRANT ALL PRIVILEGES ON databasename.* TO 'bobby'#'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON databasename.* TO 'bobby'#'192.168.0.5' WITH GRANT OPTION;
That's been my experience anyway.
You're probably better off reading this section on how to specify MySQL accounts.
When you log in from PHPMyAdmin, the web server is located on the same server that hosts the Mysql database (in your case). Mysql, by default, does not allow connections from remote hosts; only the local machine may access the database.
I'll take a shot in the dark and say that the computer you're running the java code on is not the same machine that is hosting the mysql server. You must configure Mysql to allow connections from remote hosts via the config file and then change the Host row of the mysql.users table for the specified user to allow connection from your IP address (or, if security isn't your concern, any IP address.)
To configure mysql to allow connections from remote hosts you must remove the "bind-address=" line from the configuration file.
To allow any host to log on to a specific mysql user, you must set the mysql.users Host` column to "%".
Both of these must be done.
public class MysqlConnect{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Categories

Resources