Setting up MySql for Part 2 of the gRPC using Go

January 29, 2019

If you don’t want to setup or run mySql on your laptop you can run it in docker; as to not pollute your system. It is up to you, if you prefer to just install mySql locally you can skip this tutorial.

This entry will go over setting up a docker instance of mySql. We will need this database for Part 2 of our tutorial. I am not going to go over the details of Docker or mySql; so I am assuming some comfort level with these technologies. But this is pretty simple, so as long as you know how to install Docker, the rest should be relatively simple.

  • Docker 18.09.1-ce
  • MySql I am using the latest image from docker hub

 

Pull MySql Docker Image

First you need to create a Docker Hub Account; once that is completed just run the following command

> docker pull mysql/mysql-server

Start the Docker Image of MySql  

> docker run --name mysql -d mysql/mysql-server:latest

Retrieve the Generated Password

  You will need the password for the next step

> docker logs mysql 2>&1 | grep GENERATED

Go into container and run mysql

  Enter the password from the previous step when prompted

> docker exec -i -t -u root mysql mysql -p  

If you run into any errors like “No database selected” you will need to run the following

  • show databases; // this will show you what databases/schemas exist
  • for this particular example choose mysql by typing the following command
> use mysql;
  • now you should be able to alter the root password and create the new user and grant permissions
  • change password
> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';

mysql will require you to grant permissions to users to access the database from outside it’s localhost. So if you try to use the root and its password from an app, you will get refuse connection error. I just created a new user and granted it permissions.

To create a new user and grant permissions, enter the container and mysql app as we did above or if you are still at the mysql prompt even better. Run this command using whatever user and password you like, I am using chris and password as the password

> CREATE USER 'chris'@'%' IDENTIFIED BY 'password';

Grant permissions to the user you just created above

> grant all privileges on *.* to 'chris'@'%' with grant option;      

Create the database and table for Part 2 of the gRPC tutorial

Firsty create the database. So if you are not in mySql in the docker container log back in as explained above.

Create the database

> create DATABASE customers;

Choose the database

> use customers;

Now we will create a trivial “user” table. Run the following sql in the customers database

CREATE TABLE `User` (   `ID` bigint(20) NOT NULL AUTO_INCREMENT,   `FirstName` varchar(200) DEFAULT NULL,  `LastName` varchar(200) DEFAULT NULL, `Address` varchar(1024) DEFAULT NULL, PRIMARY KEY (`ID`),   UNIQUE KEY `ID_UNIQUE` (`ID`) );