mysql data base connection inside Sam local
Asked Answered
R

6

13

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.

Royo answered 22/11, 2017 at 17:32 Comment(4)
Does this address your problem? #6885664 (specify the IP rather than using 'localhost')Roughen
it gives me the same errorRoyo
any solution found for this issue?Pollster
No Yet, But I have contacted AWS Lambda customer support, they were calming that it is possible.Royo
N
19

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?

Nata answered 23/1, 2018 at 3:3 Comment(2)
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.Toastmaster
Gosh, been searching for it to run SAM LOCAL INVOKE with ArangoDB and your solution worked perfectly http+tcp://docker.for.mac.localhost:8529Eberly
R
18

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.

Rustice answered 18/8, 2019 at 12:42 Comment(2)
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.Fateful
Thanks for the additional info @stavros.zavrakasRustice
L
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 : ''
 }
}); 
Lir answered 16/7, 2020 at 6:22 Comment(0)
W
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()
Whirlabout answered 30/3, 2021 at 10:9 Comment(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.Crenelation
A
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.

Ailina answered 19/7, 2019 at 5:0 Comment(0)
N
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

Nurture answered 14/3, 2022 at 7:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.