com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
So, you have a
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
java.net.ConnectException: Connection refused
I'm quoting from this answer which also contains a step-by-step MySQL+JDBC tutorial:
If you get a
SQLException: Connection refused
orConnection timed out
or a MySQL specificCommunicationsException:
, then it means that the DB isn't reachable at all. This can have one or more of the following causes:
Communications link failure
- IP address or hostname in JDBC URL is wrong.
- Hostname in JDBC URL is not recognized by local DNS server.
- Port number is missing or wrong in JDBC URL.
- DB server is down.
- DB server doesn't accept TCP/IP connections.
- DB server has run out of connections.
- Something in between Java and DB is blocking connections, e.g. a firewall or proxy.
To solve the one or the other, follow the following advices:
- Verify and test them with
ping
.- Refresh DNS or use IP address in JDBC URL instead.
- Verify it based on
my.cnf
of MySQL DB.- Start the DB.
- Verify if mysqld is started without the
--skip-networking option
.- Restart the DB and fix your code accordingly that it closes connections in
finally
.- Disable firewall and/or configure firewall/proxy to allow/forward the port.
See also:
- How should I connect to JDBC database / datasource in a servlet based application?
- Is it safe to use a static java.sql.Connection instance in a multithreaded system?
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
So, you have a
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
java.net.ConnectException: Connection refused
I'm quoting from this answer which also contains a step-by-step MySQL+JDBC tutorial:
If you get a
SQLException: Connection refused
orConnection timed out
or a MySQL specificCommunicationsException:
, then it means that the DB isn't reachable at all. This can have one or more of the following causes:
Communications link failure
- IP address or hostname in JDBC URL is wrong.
- Hostname in JDBC URL is not recognized by local DNS server.
- Port number is missing or wrong in JDBC URL.
- DB server is down.
- DB server doesn't accept TCP/IP connections.
- DB server has run out of connections.
- Something in between Java and DB is blocking connections, e.g. a firewall or proxy.
To solve the one or the other, follow the following advices:
- Verify and test them with
ping
.- Refresh DNS or use IP address in JDBC URL instead.
- Verify it based on
my.cnf
of MySQL DB.- Start the DB.
- Verify if mysqld is started without the
--skip-networking option
.- Restart the DB and fix your code accordingly that it closes connections in
finally
.- Disable firewall and/or configure firewall/proxy to allow/forward the port.
See also:
- How should I connect to JDBC database / datasource in a servlet based application?
- Is it safe to use a static java.sql.Connection instance in a multithreaded system?
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
This is more a docker question than a MySQL/JDBC connections. Your second docker container can't reach your first because the 127.0.0.1 there points to the container itself, and not the first container.
First of all, you shouldn't configure the bind_address
in the first container to be 127.0.0.1
. You either need to leave it out or set it to 0.0.0.0
. You probably didn't restart MySQL, otherwise you wouldn't have been able to connect from MySQL Workbench on your own machine either.
The ability to connect through 127.0.0.1
from your machine to an application running within docker is only available on your machine, not between containers.
Your command line shows you linked the myApp
container to the mysql
container (using --link mysql
). That means that as the connection URL you should use:
jdbc:mysql://mysql:3306/mydatabase
That is: replace 127.0.0.1
with mysql
as that is used as the host name of your mysql container to communicate between those two containers.
Related Topics
How to Unescape HTML Character Entities in Java
How to Pass an Object from One Activity to Another on Android
How to Render a Pdf File in Android
How to Write to a Folder on an Sd Card in Android
How to Change the Decimal Separator of Decimalformat from Comma to Dot/Point
Running Jmap Getting Unable to Open Socket File
How to Run a Command At Terminal from Java Program
How to Append Text to an Existing File in Java
What Is a Serialversionuid and Why Should I Use It
How to Invoke a Java Method When Given the Method Name as a String
Variable Might Not Have Been Initialized Error
How to Get the Insert Id in Jdbc
Getresourceasstream() VS Fileinputstream
How to Print a Double Value Without Scientific Notation Using Java
What Causes and What Are the Differences Between Noclassdeffounderror and Classnotfoundexception