12

I am trying to do local development setup for serverless architecture. Amazon provide SAM Local Beta for doing so. But in my project we are using mysql database. I am trying to make a connection to my local mysql server it failed saying.

module initialization error:
(2003, "Can't connect to MySQL server on 'localhost'
([Errno 99] Cannot assign requested address)")

Below is my python code:

import json
import pymysql


def lambda_sandbox_down_handler(event, context):
    rds_host = 'localhost'
    name = 'localhost'
    password = 'localhost'
    db_name = 'localhost'
    conn = pymysql.connect(rds_host,port=3306, user=name, passwd=password,
                           db=db_name,charset='utf8mb4',connect_timeout=5)
    return conn

I am able to make connection using command line as well as pycharm.

My question is this possible in SAM local.

I have installed a docker image of mysql and started it. After this I am still getting the same error.

sfjac
  • 7,119
  • 5
  • 45
  • 69
Tinkesh Kumar
  • 171
  • 1
  • 1
  • 4

6 Answers6

17

The SAM Local tool runs your Lambda function in a Docker container. localhost will not resolve to the host machine IP from inside the container.

If you are using Docker for Mac you can use the special DNS name docker.for.mac.localhost as the database host.

On other operating systems you can to look up the host's IP in the docker0 network interface and use that as the database host. For example see: From inside of a Docker container, how do I connect to the localhost of the machine?

kalmas
  • 831
  • 1
  • 8
  • 12
  • 1
    Thank you, this manifests itself as Error: connect ECONNREFUSED 127.0.0.1:3306. The rabbit hole is looking for why the db is not allowing connections, when in fact, localhost does not work from within docker. – toonsend Feb 28 '18 at 02:48
  • Gosh, been searching for it to run SAM LOCAL INVOKE with ArangoDB and your solution worked perfectly `http+tcp://docker.for.mac.localhost:8529` – Lazhar Mar 04 '19 at 17:27
17

I can verify that on Ubuntu 19.04, passing --docker-network host as parameter to sam local invoke solved the problem for me.

Example:

sam local invoke MyLambdaFunction --event sample-event.json --docker-network host

Some posts report that using host.docker.internal instead of localhost should work. Another recommendation was using the docker0 address (look it up using ip addr show on linux). This will often be 172.17.0.1. However, neither of these worked for me.

Jack
  • 16,506
  • 19
  • 100
  • 167
  • 1
    This should be the accepted response. You can use the flag `--docker-network host` in the `sam local start-api --port 5000 --skip-pull-image --docker-network host` and solve this problem for the api. – Stavros Zavrakas Dec 16 '19 at 12:05
  • Thanks for the additional info @stavros.zavrakas – Jack Dec 18 '19 at 21:44
4

No configuration need just add below host while connection to your mysql

for windows : docker.for.win.localhost for mac : docker.for.mac.localhost

const con = require('serverless-mysql')({
  config: {
   host     :  'docker.for.win.localhost',
   database : 'db-name',
   user     : 'root',
   connectTimeout : 5000,
   password : ''
 }
}); 
Somnath Rokade
  • 655
  • 1
  • 9
  • 27
4

Just throwing in my solution as a few mixes in here. I am developing in WSL2 ubuntu 20.04 LTS.

I have a MySQL database running locally not in a docker container. To connect to it I did have to enable docker for WSL see here.

After this I set my host to host.docker.internal instead of localhost. With my database now running in the background I was able to connect to it after running sam build and sam local invoke, no additional command line arguments where necessary.

A little snippet if you wanted to test it

import mysql.connector

cnx = mysql.connector.connect(user='XXX', password='AAA',
                              host='host.docker.internal',
                              port=3306,
                              database='YYY')
cnx.close()
WK123
  • 620
  • 7
  • 18
  • 1
    changing `localhost` to `host.docker.internal` worked for me on M1 chip Mac OS 11.5.2 while using `sam local invoke` to test a lambda function locally. – obayram Sep 08 '21 at 11:51
1

My question is this possible in SAM local: Yes, you can run Lambda etc in SAM local, SAM local provides the environment by running it inside the docker container.

So to make SAM build successful, you need to install docker in your local machine and do a SAM build.

Once SAM build is successful you can run you program. To connect to localhost in mac please use host.docker.internal in place of localhost if you are using docker 18.03 or greater, for previous versions of docker you can use docker.for.mac.localhost.

Also to connect to your mysql, you don't need to run mysql inside the container.

Sachan
  • 91
  • 1
0

use hostname -I command on the terminal then use the host IP address that shows instead of localhost or 127.0.0.1 If that doesn't work you can go ahead and enable access to MySQL port through the firewall eg sudo ufw allow 3306 then restart MySQL sudo systemctl restart mysql

Swaleh Matongwa
  • 698
  • 9
  • 16