Skip to content

Error Connecting my Express API to Mysql (Container) In CodeBuild

0

Hello, please i need help i have error connecting my Express API to Mysql (Container) In CodeBuild

Below is the error

[Container] 2024/07/04 07:54:16.657953 Running command npm run test:dbconn > gen-report.txt || eval $GEN_FH_CMD
Error connecting to MySQL: Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16)
Error: {"errno":-111,"code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":3306,"fatal":true}
Connection details - {"host":"127.0.0.1","port":"3306","user":"root","password":"password","database":"testdb"}
/codebuild/output/src2026375168/src/backend/expressapi/app/test-dbconn.js:18
    throw err; // Directly throw the original error object
    ^
Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 3306,
  fatal: true
}

Here is my codebuild configuration

      Environment:
        Type: LINUX_CONTAINER #[ARM_CONTAINER|LINUX_CONTAINER|WINDOWS_CONTAINER]
        Image: aws/codebuild/amazonlinux2-x86_64-standard:5.0 #public.ecr.aws/sam/build-nodejs20.x
        ComputeType: small #[BUILD_GENERAL1_SMALL|BUILD_GENERAL1_MEDIUM|BUILD_GENERAL1_LARGE]

asked 2 years ago518 views

1 Answer
2
Accepted Answer

Hello.

Looking at the error, it looks like MySQL is not running on localhost.
Is your buildspec.yml supposed to install and run MySQL?
For example, is the following command added to buildspec.yml and configured to run mysqld?

service mysql start
EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

  • am running it on a docker conatiner not on the localhost

    docker run -d --name mysql_test -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=testdb -p 3306:3306 mysql:8.0

  • docker run -d --name mysql_test -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=testdb -p 3306:3306 mysql:8.0

    Is the above command executed correctly? Please try changing the "host" specification in the application to "localhost" instead of "127.0.0.0". Also, in addition to localhost, try using CodeBuild's local IP address, etc. For example, you can check by adding the following command to buildspec.yml.

    hostname -I
    
  • Yes this code executed successfully

    docker run -d --name mysql_test -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=testdb -p 3306:3306 mysql:8.0

    and i get this error while executing this (hostname -I)

    [Container] 2024/07/04 08:47:05.998200 Running command hostname -I /codebuild/output/tmp/script.sh: line 4: hostname: command not found

  • Can you check the container's IP address with the following command?

    docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql_test
    
  • I got an IP address : 172.1*..

    I used it as the DB HOST and it worked Thank You so Much (Respect for the time and dedication)

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.