Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect mysql from docker container?

I have created a docker-compose file it has two services with Go and Mysql. It creates container for go and mysql. Now i am running code which try to connect to mysql database which is running as a docker container. but i get error.

docker-compose.yml

version: "2"
services:
  app:
    container_name: golang
    restart: always
    build: .
    ports:
      - "49160:8800"
    links:
      - "mysql"
    depends_on:
      - "mysql" 

  mysql:
    image: mysql
    container_name: mysql
    volumes:
      - dbdata:/var/lib/mysql
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=testDB
      - MYSQL_USER=root
      - MYSQL_PASSWORD=root
    ports:
      - "3307:3306"
volumes:
  dbdata:

Error while connecting to mysql database

golang   | 2019/02/28 11:33:05 dial tcp 127.0.0.1:3306: connect: connection refused
golang   | 2019/02/28 11:33:05 http: panic serving 172.24.0.1:49066: dial tcp 127.0.0.1:3306: connect: connection refused
golang   | goroutine 19 [running]:

Connection with MySql Database

func DB() *gorm.DB {
    db, err := gorm.Open("mysql", "root:root@tcp(mysql:3306)/testDB?charset=utf8&parseTime=True&loc=Local")
    if err != nil {
        log.Panic(err)
    }
    log.Println("Connection Established")
    return db
}

EDIT:Updated docker file

FROM golang:latest 
RUN go get -u github.com/gorilla/mux
RUN go get -u github.com/jinzhu/gorm
RUN go get -u github.com/go-sql-driver/mysql
COPY ./wait-for-it.sh .
RUN chmod +x /wait-for-it.sh
WORKDIR /go/src/app
ADD . src
EXPOSE 8800
CMD ["go", "run", "src/main.go"]

I am using gorm package which lets me connet to the database

like image 717
TechChain Avatar asked Feb 28 '19 11:02

TechChain


People also ask

How do I connect to a MySQL Docker container?

Here are the steps you can follow to install the Dockerhub MySQL Container: Step 1: Pull the Docker Image for MySQL. Step 2: Deploy and Start the MySQL Container. Step 3: Connect with the Docker MySQL Container.

Can't connect to Connection refused MySQL?

normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket file name or TCP/IP port number when trying to connect to the server. You should also check that the TCP/IP port you are using has not been blocked by a firewall or port blocking service.


1 Answers

depends_on is not a verification that MySQL is actually ready to receive connections. It will start the second container once the database container is running regardless it was ready for connections or not which could lead to such an issue with your application as it expects the database to be ready which might not be true.

Quoted from the documentation:

depends_on does not wait for db and redis to be “ready” before starting web - only until they have been started.

There are many tools/scripts that can be used to solve this issue like wait-for which sh compatible in case your image based on Alpine for example (You can use wait-for-it if you have bash in your image)

All you have to do is to add the script to your image through Dockerfile then use this command in docker-compose.yml for the service that you want to make it wait for the database.

What comes after -- is the command that you would normally use to start your application

version: "2"
services:
  app:
    container_name: golang
    ...
        command: ["./wait-for", "mysql:3306", "--", "go", "run", "myapplication"]
    links:
      - "mysql"
    depends_on:
      - "mysql" 

  mysql:
    image: mysql
  ...

I have removed some parts from the docker-compose for easier readability.

Modify this part go run myapplication with the CMD of your golang image.

See Controlling startup order for more on this problem and strategies for solving it.


Another issue that will rise after you solve the connection issue will be as the following:

Setting MYSQL_USER with root value will cause a failure in MySQL with this error message:

ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'root'@'%'

This is because this user already exist in the database and it tries to create another. if you need to use the root user itself you can use only this variable MYSQL_ROOT_PASSWORD or change the value of MYSQL_USER so you can securely use it in your application instead of the root user.

Update: In case you are getting not found and the path was correct, you might need to write the command as below:

command: sh -c "./wait-for mysql:3306 -- go run myapplication"
like image 194
Mostafa Hussein Avatar answered Oct 17 '22 04:10

Mostafa Hussein