Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql data base connection inside Sam local

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.

like image 799
Tinkesh Kumar Avatar asked Nov 22 '17 17:11

Tinkesh Kumar


Video Answer


1 Answers

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.

like image 131
Sachan Avatar answered Oct 01 '22 21:10

Sachan