traccar is not connecting to mysql in docker-compose setup

bmeier3 years ago

Hi,

is anyone here with some docker / docker-compose experience? I am trying to get traccar connected to mysql db, unfortunately traccar can not connect to the mysql db amd I can't see the error. Maybe it is independent from docker

docker-compose.yml

version: "3"
services:
    db:
        image: mysql:8.0.20
        container_name: db
        command: --default-authentication-plugin=mysql_native_password
        restart: always
        volumes:
            - /opt/traccar/mysql-data:/var/lib/mysql
            - /opt/traccar/mysql:/etc/mysql/conf.d
        ports:
            - "3306:3306"
        environment:
        - MYSQL_ROOT_PASSWORD=myrootpw

    traccar:
        image: traccar/traccar:latest
        container_name: traccar
        depends_on:
           - db
        restart: always
        volumes:
            - /opt/traccar/traccar.xml:/opt/traccar/conf/traccar.xml:ro
            - /opt/traccar/logs:/opt/traccar/logs:rw
        ports:
            - "5000-5150:5000-5150"
            - "8082:8082"
        environment:
           - MYSQL_DATABASE=tc
           - MYSQL_USER=tc
           - MYSQL_PASSWORD=mypw

volumes:
   mysql-data:
   mysql:
   logs:

traccar.xml

<entry key='database.driver'>com.mysql.cj.jdbc.Driver</entry>
<entry key='database.url'>jdbc:mysql://localhost:3306/tc?serverTimezone=UTC&amp;useSSL=false&amp;allowMultiQueries=true&amp;autoReconnect=true&a>
<entry key='database.user'>tc</entry>
<entry key='database.password'>mypw</entry>

logs traccar docker

Exception in thread "main" java.lang.RuntimeException: com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
        at org.traccar.Main.run(Main.java:147)
        at org.traccar.Main.main(Main.java:106)
Caused by: com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
        at com.zaxxer.hikari.pool.HikariPool.throwPoolInitializationException(HikariPool.java:596)
        at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:582)
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:100)
        at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81)
        at org.traccar.database.DataManager.initDatabase(DataManager.java:131)
        at org.traccar.database.DataManager.<init>(DataManager.java:90)
        at org.traccar.Context.init(Context.java:298)
        at org.traccar.Main.run(Main.java:120)
        ... 1 more
Caused by: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
        at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:898)
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:823)
        at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448)
        at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
        at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
        at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:359)
        at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201)
        at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:470)
        at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561)
        ... 7 more
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
        at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89)
        at com.mysql.cj.NativeSession.connect(NativeSession.java:120)
        at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:842)
        ... 16 more
Caused by: java.net.ConnectException: Connection refused (Connection refused)
        at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
        at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
        at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.base/java.net.Socket.connect(Socket.java:609)
        at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:156)
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)
        ... 18 more

tracker-server.log

2022-04-22 23:18:06  INFO: HikariPool-1 - Starting...
2022-04-22 23:18:11 ERROR: HikariPool-1 - Exception during pool initialization. - Connection refused (Connection refused) - ConnectException (... < DataManager:131 < *:90 < Context:298 < Main:120 < ...)
2022-04-22 23:18:11 ERROR: Main method error - Connection refused (Connection refused) - ConnectException (... < DataManager:131 < *:90 < Context:298 < Main:120 < ...)
Jérôme3 years ago

good morning. I also use it in docker and I have the same error... no way and I didn't find any solution :(

Andrew Cumming3 years ago

I had the same error and eventually got it working when I released I couldn't use 127.0.0.1 or localhost. Keep in mind that each container looks like a seperate machine and you therefore have 2 sets of 127.0.0.1

You need to use a non local (either the host machines IP as I did if your mysql is set with -p 3306:3306 or with the IP address that docker assigned to the network (I dont use this method so I may have described it incorrectly but hopefully you get the idea).

In my case, my host machine had an IP of 192.168.1.112 and the mysql docker instance exposed itself to that IP's ports. My traccar docker instance then connects to 192.168.1.112:3306 which worked fine. Like I suspect you have done, I initially used 127.0.0.1 and found it wouldnt connect.

Hope this helps.

Jérôme3 years ago

No I don't understand my docker is in bridge and therefore uses the same IP as my Synology if I understand correctly, only the port to connect to it changes. That's why I put localhost

Andrew Cumming3 years ago

I also run mine on a synology NAS with 1 container for mysql and another for traccar. My docker network is also a bridge. I got mine to work after exposing port 3306 on my mysql container to the host machine, then putting the host IP into the traccar.xml file with this line

<entry key='database.url'>jdbc:mysql://192.168.1.112:3306/traccar?serverTimezone=UTC&amp;useSSL=false&amp;allowMultiQueries=true&amp;autoReconnect=true&amp;useUnicode=yes&amp;characterEncoding=UTF-8&amp;sessionVariables=sql_mode=''</entry>

if I used 127.0.0.1 or localhost, then traccar was not able to connect to mysql.

If your are using docker-compose you can use the hostname of your mysql service in this case "db" so your configuration must be

<entry key='database.url'>jdbc:mysql://db:3306/tc?serverTimezone=UTC&amp;useSSL=false&amp;allowMultiQueries=true&amp;autoReconnect=true&a>

if you use localhost as the hostname it is pointing to the localhost of the traccar container and the database does not existis in that container.
it is not recommended to use ip address because it may change every time you run your compose.
if you use your external ip address you should expose the port 3306 and your database should accept external connections, not recomended but posible.
For using the service name as host you should include the network bridge in the definition of the docker-compose, your docker-compose-yml should be something like this (only added the network code)

version: "3"

networks:
  app-tier-traccar:
    driver: bridge

services:
    db:
        image: mysql:8.0.20
        container_name: db
        command: --default-authentication-plugin=mysql_native_password
        restart: always
        volumes:
            - /opt/traccar/mysql-data:/var/lib/mysql
            - /opt/traccar/mysql:/etc/mysql/conf.d
        ports:
            - "3306:3306"
        environment:
            - MYSQL_ROOT_PASSWORD=myrootpw
        networks:
            - app-tier-traccar


    traccar:
        image: traccar/traccar:latest
        container_name: traccar
        depends_on:
           - db
        restart: always
        volumes:
            - /opt/traccar/traccar.xml:/opt/traccar/conf/traccar.xml:ro
            - /opt/traccar/logs:/opt/traccar/logs:rw
        ports:
            - "5000-5150:5000-5150"
            - "8082:8082"
        environment:
           - MYSQL_DATABASE=tc
           - MYSQL_USER=tc
           - MYSQL_PASSWORD=mypw
        networks:
            - app-tier-traccar

volumes:
   mysql-data:
   mysql:
   logs:
Mihail2 years ago

Hello,

I don't know if you already fixed your issue, but I had a similar one, and it turned out that it was caused by the database server enforcing the use of an SSL connection.

Your problem could be fixed by just switching useSSL=true inside your JDBC URL.