Connect to the MySQL Database
Explore how to set up persistent storage for Go backend applications by connecting to a MySQL database with the GORM ORM. Learn to manage database credentials securely using .env files and the GoDotEnv library. Understand implementing JWT authentication and password encryption with bcrypt. By the end, you will be able to create a secure, connected backend with database migration and authentication features.
We'll cover the following...
Introduction
The storage used so far is not persistent, and the authentication feature has not been available. Let’s replace it with persistent storage using the MySQL database and add the authentication feature using the JSON Web Tokens (JWT) mechanism.
Before implementing those features, some additional libraries are added:
Before implementing those features, some additional libraries are added:
- GORM: For object-relational mapping mechanism to the relational database.
- GoDotEnv: For reading
.envfiles. - JWT: For authentication mechanism.
- Bcrypt: For password encryption.
We need to install these additional libraries with the following commands when running it independently. However, it has already been set up for you on the Educative platform.
We install the GORM library and its driver to perform query operations with the relational database.
go get -u ...