Connecting Python with mysql docker container

Posted by
python docker mysql container
python docker mysql container

Introduction Python mysql docker container:

Knowing how to integrate Python mysql docker containers is essential while building large Python applications .The python programming language can be integreted with many technologies, docker is no different. This is integration is very important rather usefull because it reliefs us from many headaches like manually installing Mysql or any other database in our working machine.

If we could successfully integrate these three technologeis(python,container and mysql) then we shall solve and learn multple concepts in blow.

Get the youtube tutorial here.

Importance of Docker container with mysql:

First and foremost, we shall learn how different technologies communicate among them. Ofcourse, the answer is ports and ips. Second is the confidence of knowing the technology. Third, delete containers at one command. All these in a nutshell called microservices, where we can plug and play different technologies.

Commands of Mysql container:

Download mysql image –> docker pull mysql:9

Run mysql container –>

(windows and linux)

docker run –name mysql_test1 -d  -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123 mysql:latest

(mac)

docker run –name mysql_9 -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123 mysql:9

Command to start docker container –> docker start mysql_test9

Getting into mysql prompt –> mysql -h 127.0.0.1 -P 3306 -u root -p

Get the version of the mysql –> select version();

Get the list of users in mysql database –> select user from mysql.user;

Create a new user dev1 inside mysql –> CREATE USER ‘dev1’@’%’ IDENTIFIED BY ‘dev1’;

(in the above command username and password both are dev1)

Give all permission to dev1 –> GRANT ALL PRIVILEGES ON * . * TO ‘dev1’@’%’;

Flush all the commands  from the RAM –>FLUSH PRIVILEGES;

Command to exit from mysql prompt –> exit;

Importing Sakila database into mysql container :

Command to get inside mysql container using dev1 user –>  mysql -h 127.0.0.1 -P 3306   -u dev1 -p

We have to create empty database before we do any kind of import operation.

create database sakila –> create database sakila;

Download and keep the sakila database file a specific folder. Open the command prompt from that folder. Once you have done that, first we have to import sakila-schema .sql file and then sakila-data .sql file.

Command to import sakila schema –>

mysql -h 127.0.0.1 -P 3306 -u dev1 -p sakila < sakila-schema.sql

Command to import sakila tables –>

mysql -h 127.0.0.1 -P 3306 -u dev1 -p sakila < sakila-data.sql

Conclusion of Python mysql docker container connection:

In this article we have explored the process of connecting mysql docker cotainers. Similary, we can apply almost same kind of process for other database containers as well.

In Python regular expression, there is a corner of this which is rearely visited that is look around expression. If you find my blogs interesting have a took on that too!

However, there are many subtopics of dockers and python needed to be explored. In my future we shall explore those as well.

 

Leave a Reply

Your email address will not be published. Required fields are marked *