I hosted Apache server with xampp and created local MYSQL database on my PC1.
PC1 IS CONNECTED TO NETWORK1.
I developed simple Java program which is connecting to the database this way:
static final String USERNAME="[myusername]";
static final String PASSWORD="[password]";
static final String CONN_STRING="jdbc:mysql://[myIP]:3306/[database_name]";
con=DriverManager.getConnection(CONN_STRING , USERNAME, PASSWORD);
This is working absolutely fine on my PC1, even though I changed the link with my IP rather than ‘localhost’.
It worked after executing this command in database:
GRANT ALL PRIVILEGES ON *.* TO @[myIP] IDENTIFIED BY '[password]' WITH GRANT OPTION
So I am trying to do the same with my PC2 which is also storing this JAVA program but it is connected to another network – NETWORK2. Is it possible this to happen – to connect PC2 from external network to mySQL database hosted on PC1 through my JAVA program?
3
Answers
Sure, why not? You just have to make sure that your
PC1
is accessible from the other network and that the necessary ports (3306
) are open.You can just ping
PC1
fromPC2
to check if this is possible.You need to update the
bind-address
from localhost to IP of your mysql server.Find the mysql conf file and edit:
Location of mysql conf file would be either
/etc/my.cnf
or/etc/mysql/my.cnf
or/usr/local/mysql/etc/my.cnf
Also make sure port 3306 from mysql-server machine is open and accessible from other PC. Then add the required privileges.
Yes, you can do it through connecting both computers in same LAN connection.After this check ip address of PC on which mysql is installed. You could find out it by simply typing ifconfig(linux)/ipcofing(window) command on terminal. Ensure that your taking local ip address (appears as 192.168.1.144) not public one.
Use that IP address for binding in my /usr/local/mysql/etc/my.cnf file and also use this IP address in your java program. Your code will run successfully.