MySQL database connected

MySQL database connected

Recently I studied MySQl database and connected it to my Your favorite places project. Now all the information about places is kept in this database. The first question I had to solve is where physically to put my database in the Internet. My usual Google Cloud Platform asks for 1$/day for database hosting – close to nothing for commercial use but quite much for just for fun. I began to search for completely free hosting. Many of them have some drawbacks which I could not put up with. But finally I found a proper variant – The only thing which I should remember is to make some changes in database at least ones a month. Otherwise it will be deleted.

I promise not to forget=)

So how it works? It looks like you can not have a direct access to the MySQL database from the browser. Probably it is because you should hardcode your database login and password in your JS code. In the result everybody will see it.

That’s why you need some intermediate server which will hold you password and login, take requests from the browser and send them to the database. I created this server using Express.JS which is hosted on the Google Cloud Platform. It hosts the application itself and additionally MySQL Node.js library (or module) which can connect to remote database. This MySQL module connects to the database on website and retrieves the information needed.

When user pushes a button (for example “Add a new place”) browser sends AJAX requests to Express server with information that this new place should be added to the database. Express server passes this information to the MySQL Node.js library which makes request to remote server which keeps my database and retrieve or add requested information from it. Than information is passed back to Express server and the server sends it to the application itself in the form of JSON. So it looks like to make your application work with database you need two servers: one Node.JS Express to host your application and the second - MySQL server to host the database.

MySQL and Angular picture