本地Lambda(SAM LI)+ MySQL(Docker)环境构筑注意点

目录构成

mysql8
├─data
├─logs
└─docker
    ├─docker-compose.yml
    ├─.env
    ├─config
    	└─my.cnf

.env

DB_NAME=test_db
ROOT_DB_PASS=root_password
DB_USER=test_user
DB_PASS=test_password
DB_PORT=3306
TZ=Asia/Tokyo

docker-compose.yml

version: "3.6"
services:
  db:
    container_name: db
    image: mysql:8.0
    volumes:
      - ../data:/var/lib/mysql
      - ../logs:/var/log/mysql
      - ./config/my.cnf:/etc/mysql/conf.d/my.cnf
    environment:
      - MYSQL_DATABASE=${DB_NAME}
      - MYSQL_USER=${DB_USER}
      - MYSQL_PASSWORD=${DB_PASS}
      - MYSQL_ROOT_PASSWORD=${ROOT_DB_PASS}
      - TZ=${TZ}
    networks:
      - container-link
    ports:
      - ${DB_PORT}:3306

networks:
  container-link:
    name: docker.internal

DB启动
docker-compose up -d

DB停止
docker-compose down

数据确认

docker ps
docker exec -it container_id_or_name bash

mysql -uroot -proot_password
show variables like '%char%';
show databases;
use test_db
select * from test_table;

问题:

mysql: [Warning] World-writable config file '/etc/mysql/conf.d/my.cnf' is ignored.

解决

docker ps
docker exec -it container_id_or_name bash
cd /etc/mysql/conf.d
chmod 600 my.cnf
exit

sam 部署到docker,使用下面命令,让sam环境和mysql在同一网络里面

sam local start-api --docker-network docker.internal

python

def get_connect():
    return pymysql.connect(host='db',
                           port=3306,
                           user='test_user',
                           password='test_password',
                           database='test_db')

你可能感兴趣的:(mysql,docker,aws,sam)