pythonamazon-web-servicesflasksqlalchemyrds

invalid literal for int() with base 10: 'PORT'


I am trying to connect to an RDS instance for a web app project. I am getting this error:

ValueError: invalid literal for int() with base 10: 'PORT'

With the traceback:

.conda/envs/src/lib/python3.6/site-packages/sqlalchemy/engine/
url.py", line 60, in __init__

Here is the format of my RDS request

DATABASE_URL: mysql+pymysql://USER:PASSWORD@HOST:PORT/DBNAME

I run this in terminal:

export DATABASE_URL=mysql+pymysql://USER:PASSWORD@HOST:PORT/DBNAME
export HOST=myhost
export USER=myuser
export PASSWORD=mypassword
export DBNAME=mydbname
export PORT=3306

python create_db.py

So my PORT is not reading correctly, and I don't know why. All the research I've done has said that this issue was fixed in Oracle last year, but I'm using MySQL.

Here are my create_db.py and schema.py for RDS

#  create_db.py
from schema import db
if __name__=='__main__':
# create a new table scheme as defined in the schema script
db.create_all()

# schema.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import os

class Config(object):
"""using the global variable saving in command line"""
SECRET_KEY = os.environ.get('SECRET_KEY') or 'secret-key'
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')
SQLALCHEMY_TRACK_MODIFICATIONS = False

application = Flask(__name__)
application.config.from_object(Config)
db = SQLAlchemy(application)

class Patient(db.Model):
    __tablename__ = 'Patient'

    id = db.Column(db.Integer, primary_key=True,autoincrement=True)
    ...
def __repr__(self):
    return '<User %r'> % self.id

Solution

  • Your connection string in the environment variable is completely invalid - it will not be replaced by the values you are additionally adding. Change it to:

    export DATABASE_URL=mysql+pymysql://myuser:mypassword@myhost:3306/mydbname
    

    And substitute the values with the real ones. The end result should look like this more or less:

    export DATABASE_URL=mysql+pymysql://test_user:test_password@localhost:3306/testdb