MySQL: could not create connection to database server
MySQLNonTransientConnectionException: Could not create connection to database serverShort answer
These messages are wrappers. The real reason is in the last 'Caused by' line of the stack trace. In practice it is one of three things: the MySQL server is not reachable on that host and port, the server rejects the user or host, or the app starts before the database is ready. Read the last cause, then match it below.
Fix it fast
1Find out which cause you have
Find the deepest cause in the stack trace and test the same host, port and user outside Java with the mysql client. If the client fails too, the problem is the server or the account, not your code.
grep -n 'Caused by' app.log | tail -n 3 mysql -h 127.0.0.1 -P 3306 -u app_user -p2Match what you see, then jump to its fix
- 1
The last cause names a refused connection or a communications failure, and the mysql client also cannot connectThe server is not running or not reachable on that host and portFix - 2
The last cause says Access denied, or that the host is not allowed to connectThe server refuses this user, password or client hostFix - 3
Works on a second start; the failure is only at boot, usually with Docker ComposeThe app starts before the database is readyFix
- 1
What does the error look like?
MySQLNonTransientConnectionException: Could not create connection to database serverCould not create connection to database server. Attempted reconnect 3 times. Giving up.Cannot create PoolableConnectionFactory
Cause 1 of 3
The server is not running or not reachable on that host and port
1Check
MySQL's own guide lists the usual reasons: mysqld is not running, skip_networking blocks TCP/IP, bind_address=127.0.0.1 accepts only local connections, or a firewall blocks port 3306. Connector/J needs TCP/IP.
ps xa | grep mysqld
mysqladmin -h 127.0.0.1 --port=3306 version2Fix
Start the server, and for a remote database make it listen on a reachable address and open the port to the app host. Then put the same host and port in the JDBC URL.
# application.properties
spring.datasource.url=jdbc:mysql://db.example.com:3306/appdb3Verify
The mysqladmin command prints version information, and the app starts.
mysqladmin -h db.example.com --port=3306 versionConfirmed by dev.mysql.com
Cause 2 of 3
The server refuses this user, password or client host
1Check
MySQL matches both the user name and the client host. A password typed with a wrong value, an old password in an option file, or an account that exists only for localhost all fail. A mysql client that works while the Java program fails points at the program's own settings.
mysql --no-defaults -h db.example.com -u app_user -p appdb
mysql -u root -p -e "SELECT user, host FROM mysql.user WHERE user='app_user';"2Fix
Create or correct the account for the host the app connects from, and give it rights on the database. Copy the exact user name and password into the application settings.
CREATE USER 'app_user'@'10.0.0.%' IDENTIFIED BY 'change-me';
GRANT ALL PRIVILEGES ON appdb.* TO 'app_user'@'10.0.0.%';3Verify
Log in from the app host with the mysql client, then start the app.
mysql -h db.example.com -u app_user -p appdb -e 'SELECT 1'Confirmed by dev.mysql.com
Cause 3 of 3
The app starts before the database is ready
1Check
Compose depends_on alone waits only until the database container is running, not until it accepts connections. A connection pool that creates connections at start (DBCP's initialSize) fails the whole app start when the database is not ready yet.
docker compose ps2Fix
Add a healthcheck to the database service and make the app wait for it with condition: service_healthy.
services:
app:
depends_on:
db:
condition: service_healthy
db:
image: mysql:8.4
healthcheck:
test: ["CMD-SHELL", "<your database readiness command>"]
interval: 10s
retries: 5
start_period: 30s3Verify
Bring the stack up from scratch and check that the app container stays up.
docker compose up -d && docker compose psConfirmed by docs.docker.com
How do I stop it coming back?
- Log the full cause chain at startup so the root cause is never hidden behind the wrapper.
- Keep the database host, port, user and password in one place and test them with the mysql client before a deploy.
- Give every database container a healthcheck and make dependants wait for it.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
Related fixes
Databases
JDBC cannot talk to MySQL: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago.
Communications link failure means the TCP link to MySQL could not be opened or was closed.
3 causes, each with a fixDatabases
EntityNotFoundException when loading an entity that has a missing related row
ConstraintViolationException on an orphaned foreign key
EntityNotFoundException means Hibernate followed an id and no row was there.
3 causes, each with a fixDatabases
Hibernate fails on a CREATE or ALTER TABLE statement at startup
Error executing DDL "alter table events drop foreign key FKg0mkvgsqn8584qoql6a2rxheq" via JDBC Statement
Hibernate generated a CREATE, ALTER or DROP statement and MySQL refused it.
3 causes, each with a fixDatabases
MySQL Connector/J: EOFException, expected to read 4 bytes, read 0 bytes
Communications link failure
The MySQL server or something between you and it closed the connection, and the driver found out only when it tried to use it.
3 causes, each with a fixDatabases
The MariaDB or MySQL container ignores the root password you set
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
The official MariaDB and MySQL images apply their password variables only while initialising an empty data directory.
3 causes, each with a fixDatabases
Java cannot log in to MySQL 8: authentication plugin caching_sha2_password not supported
ERROR 2061 (HY000): Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
MySQL 8 accounts use the caching_sha2_password plugin by default, and Connector/J releases before 8.0.9 cannot use it.
3 causes, each with a fix