-1

This and similar questions don't solve my question.

I'm trying to connect my application to a MySQL server to no avail. I can't seem to find the error that causes this connection failure.

This is the full error log: https://pastebin.com/t54pVb58

This is my docker-compose.yml:

version: '3.8'
services:
  mysql-ricette:
    container_name: mysqlRicette
    image: mysql
    volumes:
      - ./mysql:/docker-entrypoint-initdb.d
    ports:
      - 3307:3306
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: ricette
      MYSQL_USER: user
      MYSQL_PASSWORD: password
  ricette-service:
    container_name: ricette
    build: ./ricette
    depends_on:
      - consul
      - mysql-ricette
      - kafka
      - zookeeper

This is my application.yml for ricette-service: https://pastebin.com/PNhF532g

And this is the Entity I'd like to save in the database:

@Entity 
@Data @NoArgsConstructor
public class RicettaCompleta {

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id; 
    private String autore; 
    private String titolo; 
    private String preparazione; 
    
    public RicettaCompleta(String autore, String titolo, String preparazione) {
        this(); 
        this.autore = autore; 
        this.titolo = titolo; 
        this.preparazione = preparazione; 
    }
    
}

I know that this error means that something is wrong with the datasource configuration. But I have no idea what. Yes the mysql-ricette DB is actually running.

Please help my fix this error that I keep getting.

iLikeKFC
  • 169
  • 13

2 Answers2

0

you need to link mysql-ricette to ricette-service. updated yml file looks like this. also replace your mysql url in java code as well, ie. your application.yml or application.properties whichever you are using with jdbc:mysql://mysql-ricette:3306/ricette?serverTimezone=UTC

version: '3.8'
services:
  mysql-ricette:
    container_name: mysqlRicette
    image: mysql
    volumes:
      - ./mysql:/docker-entrypoint-initdb.d
    ports:
      - 3307:3306
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: ricette
      MYSQL_USER: user
      MYSQL_PASSWORD: password
  ricette-service:
    container_name: ricette
    build: ./ricette
    depends_on:
      - consul
      - mysql-ricette
      - kafka
      - zookeeper
    links: 
      - mysql-ricette
Thakur Amit
  • 357
  • 1
  • 4
  • 12
0

Changing com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver and mysql to mysql:5.7 did the trick. I understand the first change because the driver is deprecated, but I don't get the mysql one.

iLikeKFC
  • 169
  • 13
  • It may be that driver isn't the latest either. Using "mysql" in the image will use latest version which currently is 8.x, but if the driver you are using is "Connector/J 5.1" I don't believe that supports mysql 8.x. Take a look at this: https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-whats-new.html. – Carlos Aug 28 '20 at 20:20
  • Switching from `com.mysql.jdbc.Driver` to `com.mysql.cj.jdbc.Driver` cannot have fixed this problem, as loading `com.mysql.jdbc.Driver` will load `com.mysql.cj.jdbc.Driver`. – Mark Rotteveel Aug 29 '20 at 13:04