0

I have a MySql database that i created on aws rds. In my MySql workbench, the server status says "on". I also have my MySQL shell open.

I have a simple react app on the frontend, being served by flask in the backend. In main.py, i have to set these variables, but i dont know what to:

app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'root'
app.config['MYSQL_DB'] = 'MyDB'

should the mysql host by just the endpoint provided in RDS? And the user being my Administrator IAM thing?

Im also aware that i should use this command in the sql terminal:

CREATE TABLE MyUsers ( firstname VARCHAR(30) NOT NULL,  lastname VARCHAR(30) NOT NULL);

but im not sure where to run the above script. Do i have to open MYSQL shell in my backend folder?

For context, here is the react being served by the flask:

import React from 'react';
import './App.css';

function App() {
  return (
    <div className="App" style={{color:'magenta'}}>
    pca app - channel 2
    <p>from flask = {window.token}</p>
    <form method="POST" action="">
    <center>
    <h1 style={{letterSpacing:'4px'}}>Enter Details : </h1> <br/>
    First Name <input type = "text" name= "fname" /> <br/>
    Last Name <input type = "text" name = "lname" /> <br/>
    <input type = "submit"/>
    </center>
    </form>
    </div>
  );
}

and here is my main.py, where i should be setting the config variables:

app = Flask("__main__")

@app.route("/", methods=['GET', 'POST'])
def my_index():
    return render_template("index.html", flask_token="Allahuabha+")

app.run(debug=True)

tldr: i would like to know what i have to set those app.config variables to.

neutrino
  • 894
  • 8
  • 23
  • You can use any connectors python `mysql-connector` or make use of [SQLAlchemy](https://flask-sqlalchemy.palletsprojects.com/en/2.x/quickstart/) which I prefer. – coldy Jun 24 '20 at 18:02
  • could you provide an example? I'm sorry to ask, but i am very new to anything sql or flask – neutrino Jun 24 '20 at 18:04
  • https://stackoverflow.com/questions/20744277/sqlalchemy-create-all-does-not-create-tables this might be useful. – coldy Jun 24 '20 at 18:21
  • is the key idea here that i should sub those variables out for the single app.config['SQLALCHEMY_DATABASE_URI'] variable, and set it as its being set in the example i.e. 'mysql+mysqldb:://Administrator:@database-2.<>.us-east-2.rds.amazonaws.com' – neutrino Jun 24 '20 at 18:27
  • Yes, but note that it is not a good practice to hardcode all of the values in the code. Rather use `os.environ` to get the values from the environment and use __init.py__ where all the connections are setup. So when the flask runs, the first step it does is run the init where the setting of connection takes place, and then you can use the connection object throughout the application. You can look for some project structures to get a better idea such as [Flask Todo](https://github.com/amaridev/Flask-todo-app) – coldy Jun 24 '20 at 19:07

0 Answers0