Saturday, April 18, 2020

Docker Assignments #8: Docker, MySQL


  1. Pull mysql:5.7 docker image into host machine
  2. Write docker run command by setting following options
  • enable detached mode
  • set container name as "testmysql"
  • map host port number to 3500
  • set MYSQL_ROOT_PASSWORD as “mysql123”
      3. Check testmysql container up and running
       4. Enter container using docker exec
       5. Connect to database using mysql CLI client with root password 
       6. Create database with name “hr”
       7. Create table with name employee and “id, name” columns under “hr” database
       8. Insert sample data into employee table
      9. Connect to database using mysql UI client i.e. ( workbench or Dbeaver)
      10. Check “HR” database and “Employee” table with data
      11. Get testmysql container IP-address
      12. run another mysql container named as "testmysqlclient" and connect to "testmysql" server
      13. Create user defined bridge network (i.e. testnetwork )
      14. run mysql container named as "testmysql2" by attaching testnetwork
      15. run another mysql container named as "testmysqlclient2" and connect to "testmysql2" server
      16. check mysql container logs


Step 1: 
     > docker pull mysql:5.7

Step 2: 
> docker run -d
  -p 3500:3306 
  -e MYSQL_ROOT_PASSWORD=mysql123 
  --name testmysql mysql:5.7

Step 3: 
     > docker ps

Step 4: 
     > docker exec -it testmysql mysql -u root -p

Step 6: 
     mysql> create database hr;
     mysql> use hr;
     mysql> create table employee(id int,name varchar(10));
     mysql> insert into employee values(1,'rama');
     mysql> select * from employee;

Step 11: 
     > docker inspect testmysql

Step 12: 
     > docker run -it --name testmysqlclient mysql:5.7 mysql -h 172.17.0.2 -u root -p
    mysql> show databases; 
  • -h --> mysql server hostname (i.e. first container IP-address )
  • -u --> mysql user name
  • -p --> mysql password
Step 13: 
     > docker network create testnetwork

Step 14:   
> docker run -d
   -p 3500:3306 
   -e MYSQL_ROOT_PASSWORD=mysql123 
   --network testnetwork 
   --name testmysql2 mysql:5.7

Step 15:
> docker run -it
   --network testnetwork 
   --name testmysqlclient2 
   mysql:5.7 mysql -h testmysql2 -u root -p
  • -h --> mysql server hostname i.e. both containers are connected under user-defined bridge network.So, we can use hostname as IP-address or container name
Step 16: 
     > docker logs testmysql2


Happy Coding :)


No comments:

Post a Comment