2

So today I decided my website would run faster if I moved our databases to a different server, so I did, you can see how I am connecting below:

final String driver = "com.mysql.jdbc.Driver";
            final String url = "jdbc:mysql://external_ip_address_here:3306/dbname";
            final String username = "user";
            final String password = "pass";
            Class.forName(driver);

            Connection connection = DriverManager.getConnection(url, username, password);

This works fine when I am connecting to 127.0.0.1 / localhost, now I am getting this error:

java.net.ConnectException MESSAGE: Connection refused (Connection refused)

My MySQL server is running Ubuntu 18.04.

I added multiple firewall rules to try and allow all incoming connections to port 3306 - sudo ufw allow from any to any port 3306 proto tcp & I tried iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT, still not working, when i port scan the network it shows port 3306 as closed

FIX 1: This fixed the connection refused error - Step 1: edit file /etc/mysql/mysql.conf.d/mysqld.cnf Step 2: remove line bind-address = 127.0.0.1

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
MakeN
  • 39
  • 5
  • Is the external_ip_address_here reachable ? Try pinging if it is enabled. – Aman jangra Dec 23 '19 at 11:41
  • 1
    Most likely you didn't configure everything on DB server properly, check this question it might help your: https://stackoverflow.com/questions/2318250/how-to-connect-to-a-remote-mysql-database-with-java – FilipRistic Dec 23 '19 at 11:43
  • @Amanjangra it is reachable – MakeN Dec 23 '19 at 11:44
  • @Amanjangra its saying 3306 port is closed – MakeN Dec 23 '19 at 11:45
  • @MakeN did you tried connecting it though any sql developer IDE's from the box where you are running your application. – RamPrakash Dec 23 '19 at 12:34
  • ya, managed to actually start connecting to it, now I am getting another error ;( -> `java.sql.SQLException: Access denied for user 'user'@'hostname' (using password: YES)`, 99.99% sure I am using correct password. – MakeN Dec 23 '19 at 12:48

2 Answers2

0

Check the servers firewall configurations. It may be the reason for your problem. Add a new rule that allows connection from your URL in firewall settings.

Aman jangra
  • 268
  • 1
  • 16
  • I added multiple firewall rules to try and allow all incoming connections to port 3306 - `sudo ufw allow from any to any port 3306 proto tcp` & I tried `iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT`, still not working, when i port scan the network it shows port 3306 as closed – MakeN Dec 23 '19 at 12:31
  • Try restarting the iptables service afterwards, which OS are you using? – Aman jangra Dec 23 '19 at 12:48
  • ubuntu 18.04, this error is fixed now as you can see in the edited message, I am having a new problem - `java.sql.SQLException: Access denied for user 'user'@'hostname' (using password: YES)`, I am using correct details. – MakeN Dec 23 '19 at 12:58
  • See this, this might help https://stackoverflow.com/a/43088226/5804036 – Aman jangra Dec 23 '19 at 13:52
  • Sadly it did not, I will probably create a new question for this to get more answers, is there a way for me to close this? – MakeN Dec 23 '19 at 13:55
  • https://meta.stackexchange.com/a/96283 – Aman jangra Dec 23 '19 at 14:03
0

This fixed the connection refused error -

Step 1: edit file /etc/mysql/mysql.conf.d/mysqld.cnf

Step 2: remove line bind-address = 127.0.0.1

MakeN
  • 39
  • 5