'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.
Solution 1:[1]
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?
Solution 2:[2]
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.
Solution 3:[3]
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 : ''
}
});
Solution 4:[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()
Solution 5:[5]
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.
Solution 6:[6]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Jack |
| Solution 3 | Somnath Rokade |
| Solution 4 | WK123 |
| Solution 5 | Sachan |
| Solution 6 |
