A simple microservice with MySQL database connection. It contains parts.
1. Go 1.14
2. MySQL 8.0+ for Database
3. Make (optional but make larger commands in short)
4. Docker
I hope GO and MySQL is already installed.
Install Chocolatey (Chocolatey is a software management solution)
- Open PowerShell with Adminitrator
- Run
Get-ExecutionPolicy
If it return Restricted
then run
Set-ExecutionPolicy AllSigned
OR
Set-ExecutionPolicy Bypass -Scope Process
- Install by this command
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
- Install make
choco install make
Unix like OS already comes with make preinstalled so no worries.
It's a a bit long process but in short you need to install Docker from https://hub.docker.com/editions/community/docker-ce-desktop-windows/ and it also need specefic version of Windows and WSL2, so check online for for further help.
After Cloning it, download all depencies once
go mod download
Run the command and make sure to set config.yaml file for configuration
make run
OR
go run test
Create a DB with any name and make sure to give the set DB_NAME in Config file and create a table users with following fields.
- id AUTOINCREMENT PRIMARY KEY
- username UNIQUE VARCHAR (20)
- password VARCHAR (20)
- marks VARCHAR (20) NULL(by Defualt)
- Buiding the image Syntax :- docker build -t Example :-
docker build -t Test .
docker run -d -p 8080:8080 --name TesingMyApp test
Extra Flags can also be added check them out by
docker run --help
-d for running conatainer in detach mode (running in background) --rm for removing the conatiner after stopping it 3. For strating the container
docker start <container ID> or <container name>
- For stopping the container
docker stop <container ID> or <container name>
- For listing the running and stopped container
docker ps -a
- For listing Docker images
docker images
- For inspecting the Conatainer
docker inspect <name of container>
-d for running conatainer in detach mode --rm for removing the conatiner after stopping it
- Pull the MySQL image from docker hub
docker pull mysql/mysql-server:latest
- To make sure the data persists even after stopping the container Create Volume in docker Syntax:
docker create -v <location> --name <nameOfTheImahe> <ImageName>
Example :-
docker create -v /var/lib/mysql --name mysqldata mysql/mysql-server:latest
- Running the MySQL Container Before running it, it required some ENV variable and config to set, atleast root password is required. You can check from here https://hub.docker.com/_/mysql. Syntax :-
docker run -p <internalPort>:<externalPort> -e <ENV variable name>=<value> --volumes-from <volume location> --name=<name of Container> <image name>
Example :-
docker run --rm -p 3306:3306 -d -e MYSQL_ROOT_PASSWORD=pwd --volumes-from mysqldata --name=mysqldb mysql/mysql-server:latest
- A database could have been created when running the WebApllication but here will create by another way. Syntax :-
docker exec -it <container ID> <bash or any shell or command>
Example :-
docker exec -it mysqldb bash
- Login into mysql as Root Syntax :-
mysql -u <username> -p
Example :-
mysql -u root -p
- You will get a error related to Access denied to solve that issue Create a new user with host with IP or % for any IP access to DB Syntax :-
CREATE USER '<username>'@'<IP or %>' IDENTIFIED BY '<password>';
Giving privilages (For Learing purpose give all) Example :-
CREATE USER 'rajiv'@'%' IDENTIFIED BY 'pwd';
GRANT ALL PRIVILEGES ON *.* TO 'rajiv'@'%';
FLUSH PRIVILEGES;
- Create the DB as stated at #4
- A postman collection has been shared, import in the Postman to see the API endpoints.
- For any help reguarding the docker type
docker <commandName> --help