How to create YOUR own cryptocurrency!!!

Anantha Perumal
Coinmonks

--

Part - 4: Creating a database and hosting our application

Welcome back, guys🤗🤗!!.. In my previous blogs, we discussed some basic introduction to blockchain, then how blockchain is made and at last, we discussed how data in blockchain are almost impossible to change. Those who missed it please have a look over my previous blogs so that you will enjoy reading here😊😊

pic source here

Okay, what’s new in this blog? what we are gonna learn here? we are gonna create a database to store data and will be hosting our application woohoo🎉🎉... trust me its a very simple task. Let’s get into the blog.

Setting up the database

I am using a MySQL database to store the data. Let's login to our database by executing below command, this will pop up for a password, once you entered

mysql -u root -p

password press enter to get started. Now you will be logged in to your application. Let’s execute the below command to create a new user.

Note: In the below commands replace <username>, <database_name> and <password> with the one you wish.

CREATE USER '<username>'@'%';

Let’s execute below command to grant permission to a particular database alone for the user along with setting up a password

GRANT ALL PRIVILEGES ON <database_name>.* To '<username>'@'%' IDENTIFIED BY '<password>';

GRANT - This is the command used to create users and grant rights to databases, tables, etc.

ALL PRIVILEGES - This tells that the user will have all standard privileges. However, this does not include the privilege to use the GRANT command.

<database_name>.* - This instructions MySQL to apply these rights for use in the entire database that we specified. You can replace the * with specific table names if you wish to give access to a particular table alone.

To '<username>'@'%' - Provides the privileges to the user.

IDENTIFIED BY '<password>’ - As you would have guessed this sets the password for that user.

Before executing the above command the <database_name> you will be specifing must be created using below command

create database <database_name>

Now execute the below command, so that the above command takes action

FLUSH PRIVILEGES;
exit;

Now login with the <username> and <password> by using the command I mentioned above.

We have already created a database, to use that database, we have to execute the below command so that when we create a table it will be inside the database.

use <databasename>

Let’s create a table to store the blockchain information,

create table blockchain(number bigint, hash varchar(64), previous varchar(64), data varchar(100), nonce bigint);

that’s all for now😅😅!!..

Hosting the application

We are gonna use Flask framework to host our application so kindly do read here.

The above blog I mentioned which provides what is a flask, how to set up the environment and a hands-on experience on simple flask application.

Now to host let's create a file app.py under the main directory where blockchain.py file is located and copy-paste the below code in it.

from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "<h1>Web app hosted</h1>"
if __name__ == '__main__':
app.run(host='0.0.0.0',port=7070,debug = True)

As discussed here, I have created a simple web application using Flask and hosting it in port 7070 on debug mode.

Now let's test our application by executing ‘python app.py’ in command prompt it will provide you following output

$ python app.py
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:7070/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 843-354-301

you can visit your server by the URL http://localhost:7070 if you are accessing in same machine else access it by http://<ipaddress>:7070.

Great job guys, we have hosted the application.

For those who want source code, you can access the updated code here.

Thank you so much for spending your valuable time in my blog. Please support me by doing claps at the rate of 1–50 by the way you feel it worth. If you have any doubts regarding this blog or suggestion to improve it please do comment below and share it to those who want to know about blockchain.

You can find next part here.

Follow me to get notified when I publish a new work. I will get you things as simple as you could understand😉😉.

Have a good day😊😊

Also, Read

--

--