1

I am learning web development and I am using python (flask) along with HTML and CSS with bits of JavaScript. I want to know how I can connect a database (MySQL) to my website and where and how it will be stored. I wish to know how I can incorporate MySQL with my website and also how the database will be stored and accessed over the internet. Thank you for sharing your knowledge.

1
  • This depends on your host, or if you roll your own server, on the server config you go with. Commented Jul 20, 2020 at 3:48

2 Answers 2

1

I am by no means an expert on the Flask framework, but I think I can offer some advice on the bigger picture of how Flask and MySQL work together. I'm not sure what operating system you are using to serve your website so I will use linux as an example.

Oversimplified, Flask is a microframework that uses python to abstract and connect your front end (HTML, CSS, JavaScript), your backend database (MySQL), as well as serve your website replacing the need for traditional server software such as Apache2.

Flask takes care of serving your webpage so you won't need to install separate software, but Flask does require traditional database software such as MySQL. Take a look at this page for information about how to install MySQL on Ubuntu.

You will first need to set up your database and tables using the command line or a GUI application to access MySQL running on your machine. Here is a MySQL command line cheat sheet. If you prefer interacting with your database using a GUI application, here is MySQL Workbench. For information on how to connect your Flask app to your freshly setup database, here is a tutorial you might find helpful. Following this tutorial, your database software and database data will run and be stored on the same machine that will be running your python Flask code.

I'm not entirely sure what you mean by "how the database will be stored and accessed over the internet" but here is some clarification that might be helpful. The visitors to your website will be interacting with your Flask python code which will in turn interact with MySQL. This is how your website can access the data stored in the database. You absolutely do not want your visitors to be able directly interact with the MySQL software running on your server. If you want to remotely access MySQL for administration purposes, here is how to set that up.

Hope this info helps!

1

I wish to know how I can incorporate MySQL with my website.

In short, you can install MySQL on whichever computer you want to run it on, just like any other application, for whatever operating system the computer is using. This means an MSI installer or .zip archive on Windows, likely a .dmg archive on MacOS and .deb, .rpm, tar.xz etc. files for Linux et al.

If you wish, you can download the Community version of MySQL here. Note that for non-Windows systems, however, it might be easier/better to use your platform's package manager to install MySQL (if possible). You will also likely need to install the MySQL connector for Python or another option to connect to MySQL from Python on the same computer as your Python/Flask installation.

Once MySQL is installed, you will want to ensure it is running as a service for the computer's OS. The port this service typically operates on is 3306. This allows third-party applications or scripts like Python/Flask to make requests to MySQL on that port (i.e. send database queries to MySQL and receive replies [data] back).

This means you will most likely be:

  • Writing code in Flask to make requests to MySQL from your Flask app when a visitor connects to your site (either on its own or on behalf of a visitor).

  • Writing more code to process the results of those requests.

  • Writing even more code to use the data you processed to return information to a visitor based on the request results.

Since you are using Flask, you may want to have a look at Flask-SQLAlchemy. I am not 100% certain, but this may obviate the need for other connectors (above).

MySQL vs. Python/Flask

To be clear, MySQL operates completely independently of Python or Flask. You can install and run MySQL without Python/Flask even being on the system. Though there is nothing wrong with running MySQL on the same computer as your Python/Flask installation, MySQL can also be run on a separate computer on your local network or even outside your network (though this last option isn't great for security). As long as Python/Flask can send queries to MySQL via ex. port 3306, it hardly matters where MySQL is actually installed.

MySQL Configuration

Be aware that MySQL will likely require some configuration before it will run the way you want it to or be available to Python/Flask.

On Windows, the MSI installer will walk you through MySQL's initial configuration during installation. Have a look at this question if you want some basic tips on what to possibly select in the setup wizard on Windows.

If you use the .zip version on Windows, use a non-Windows version or want to make any changes after initial installation, you will likely need to edit a file called mysql.ini. This is a simple text file but it holds many of the settings MySQL uses to run. If you make any changes to this file, you will need to restart the MySQL service to have your changes take effect. Note that the location of this file depends on your operating system.

As a minor caveat, MySQL may initally prevent connections from anything other than localhost. This might present an issue if you have MySQL installed on another computer or virtual machine. With that said, it is relatively simple to allow non-localhost connections as you require or see fit.

How will the database will be stored and accessed over the internet?

MySQL databases are stored on the server MySQL operates on. To reiterate, for security, you will want MySQL to run on a computer on your local network (whether or not it is on the same computer as your Python/Flask app).

Visitors will contact your Flask instance over the internet and your Python/Flask app will make a local request to MySQL for data. MySQL will give data back to your Flask app, your Flask app will process that data and then give that processed information back to the visitor (often in a web page).

3
  • Thank you for your answer, I want some more clarification on how the mysql database is stored once my website is deployed. Is the mysql still running using my system? or does the place where I deploy it have some sort of database manager.
    – rishi
    Commented Jul 18, 2020 at 7:09
  • Your welcome. Regarding your questions, the MySQL service is what ultimately manages your databases (reads from them and writes to them), so it has to be running on whatever computer it is installed on. The databases MySQL uses cannot be accessed directly by other applications (i.e. all database interactions must go through MySQL). Whether or not this consumes resources on your computer depends on if you installed MySQL on that computer (if you did, it does, if you didn't, it doesn't). Commented Jul 18, 2020 at 8:57
  • Note that while you can use third-party "managers" to interact with MySQL databases beyond Flask, they still make requests to MySQL on e.g. port 3306, just like your Python/Flask app. If you are using some service to run MySQL (ex. in the cloud), the service provider may supply a third-party application to interacte with MySQL, but it is still MySQL running things underneath. Also, as implied above, these types of services would not consume local resources (since MySQL wouldn't be running on the same computer as your Flask app). Commented Jul 18, 2020 at 9:00

Not the answer you're looking for? Browse other questions tagged .