1

i'm using jdbc to connect MySql from java and using Apache server.

Class.forName("org.gjt.mm.mysql.Driver");
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/" + Db, dbuser, dbpasswd);
            stmt = con.createStatement();

i can even access it with 127.0.0.1 either

everything went well until i tried to connect it in java with my own LAN IP Address.

My LAN IP : 192.168.0.12

so i want to able to access it like this

    Class.forName("org.gjt.mm.mysql.Driver");
            con = DriverManager.getConnection(
                    "jdbc:mysql://192.168.0.12:3306/" + Db, dbuser, dbpasswd);
            stmt = con.createStatement();

however i can access it in browsers like this

http://192.168.0.12/phpmyadmin/

Iqbal Tawakkal
  • 445
  • 4
  • 11

3 Answers3

1

for someone who curious about the solution, i found a way to do it.

1) find my.ini file in mysql config, look for bind_address

2) fill it with your address to allow (in my case i allow every host, so i put 0.0.0.0)

3) add grant to host, there's 2 option i believe. first, using this Sql command,

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

second, if you are using phpmyadmin, you can go to privileges tab on your database menu, add all privileges and grant to user/type of user.

Iqbal Tawakkal
  • 445
  • 4
  • 11
0

By default freshly installed MySQL servers are accessible only from localhost.

Check the file /etc/mysql/my.cnf on your SQL server. If it has a line like "bind-address = 127.0.0.1", comment out that line and restart the MySQL server.

You might need to add access to remote IPs - see this guide:

http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html

0

Make sure you have the mysql driver library added to your classpath.

On the machine at 192.168.0.12, double check that you have the 3306 port unblocked.

You didn't include the full URL in the example code there, but I'm assuming it would look something like this after that "+ Db".

String URL = "jdbc:mysql://192.168.0.12/mydatabase";
String username = "username";
String password = "password";
Connection con = DriverManager.getConnection(URL, username, password);

The port does not need to be included in the URL, as you are using the default port, but if you want to be explicit, feel free.

I should also mention that the ability for you to access the website at http://192.168.0.12/phpmyadmin/ doesn't mean you can access it from other computers. The phpmyadmin software is access localhost, and your computer is not access localhost to view that database.

James
  • 787
  • 1
  • 7
  • 15
  • mysql driver library added already i disable the firewall which means all port is unblocked (i'm not sure about this). The code maybe not as neat as yours but it works. i can access http://192.168.0.12/phpmyadmin/ from another computer i can even manipulating the data. what do you think? – Iqbal Tawakkal Apr 14 '16 at 10:18
  • You're correct. All ports are unblocked and incoming connections are allowed. Do you get any kind of error when you try to connect with JDBC? – James Apr 14 '16 at 10:21
  • yes i do, it says that i can't access the table. but i can access it with 127.0.0.1 and localhost. but can't with my own IP LAN. – Iqbal Tawakkal Apr 14 '16 at 10:23
  • Are 192.168.0.12 and 127.0.0.1 the same computer? – James Apr 14 '16 at 10:28
  • You initial question is unclear. Are you able to connect with 127.0.0.1? – James Apr 14 '16 at 11:02