How to connect python and MySQL using Docker Compose

Поделиться
HTML-код
  • Опубликовано: 3 дек 2024

Комментарии • 34

  • @bencusb
    @bencusb Год назад +3

    A suggested change would be not to just depend on the db container but only execute if a health check is passed making sure the db container is up and running.

    • @Randomcode_0
      @Randomcode_0  Год назад

      That is a good and interesting and addition which could be added in most use cases. Thanks for sharing!

  • @jorgebotelho8494
    @jorgebotelho8494 Год назад

    Cara, você mostrou o que eu precisava. Resolveu dois problemas de uma vez. Parabéns!

  • @KarimChihani-l1k
    @KarimChihani-l1k Год назад

    Amazing video, helped a lot! keep up the good work!!

  • @nickwebb3798
    @nickwebb3798 2 года назад

    This is really helpful and succinct!

  • @rohitnagar3157
    @rohitnagar3157 Год назад +1

    why I am getting empty result ?
    pythonapp_1 | DB connected
    pythonapp_1 | []
    project_pythonapp_1 exited with code 0

  • @spiffylogic
    @spiffylogic 4 месяца назад

    It looks like this is for setting up a database with initial data, but how do you deal with the evolving state of the data over time? i.e. as the application is used by users and simultaneously iterated on by developers.

    • @Randomcode_0
      @Randomcode_0  4 месяца назад +1

      You are correct, this is a quick way to start a database for local development. In a real-world production environment, changes are typically managed with migration files named sequentially or with timestamps. Each file contains database changes, and the system tracks which migrations have been applied, allowing new updates by running pending files. Sometimes, a single file is used, divided into sections for each update. The main idea is to incrementally update the database rather than recreating it each time.

  • @eliteengineer7340
    @eliteengineer7340 Год назад

    Hey, i new to docker and i have a few questions, I've learned some docker and i managed to write a more complex project now, it's purely to learn, i run docker compose up and it works fine, however, how would i manage this as an always running application? tecniclly this is a discord bot, i want it to run 24/7, and by adding the -d to the docker-compose i should be able to, but how do i update changes only to the python folder? do i have to docker-compose for every little change? also, is it ok to compose it again i ncase i change something in the dockerfiles ? would it do something weird to the mySQL database?

    • @Randomcode_0
      @Randomcode_0  Год назад

      Using -d is an okay way to keep running your setup for a smaller project. If you make major changes in the Dockerfiles, you need to rebuild your entire docker-compose project. You can restart individual services using "docker-compose restart ", but any major changes will probably require a full restart with "docker-compose up -d --build". In a more proper setup, your services should be stateless, meaning it should not matter if they are being reset or not. You can define a volume for your database setup, allowing the data to be persisted on the server; this enables you to restart any database service (Look into "volumes", it is quite simple).
      In larger modern software architecture, this would be handled by some cloud provider, but it is basically the same concept. Hope this helps. Otherwise, feel free to ask more questions.

  • @davyeblue
    @davyeblue Год назад

    is it possible to connect it to phpmyadmin too?

  • @geetikapaliwal4041
    @geetikapaliwal4041 4 месяца назад

    i want to make changes in database, but the database is not accepting changes. how to do it if I want to insert more values?
    the compose run one time and then it is not allowing any changes in database. why it is so?

    • @Randomcode_0
      @Randomcode_0  4 месяца назад +1

      It sounds like something is wrong. You should be able to connect to the database if you have an open port. Using a password and user, you should then be able to make changes.

    • @geetikapaliwal4041
      @geetikapaliwal4041 4 месяца назад

      Yes I figured it out what wrong input I was giving. Thank you.
      Also mysql is not working on localhost:3306 using docker compose. Without docker compose it is running. How to fix it

    • @Randomcode_0
      @Randomcode_0  4 месяца назад

      @geetikapaliwal4041 If you run a service like MySQL locally, its open ports will by default be available on your computer. If it is run inside a container, the ports are only available inside the container, so you need to define in your docker-compose file which ports should be accessible from outside the containers created by docker-compose.

    • @geetikapaliwal4041
      @geetikapaliwal4041 4 месяца назад

      @@Randomcode_0 I did port binding as 3306:3306 in docker compose.yml file like you mentioned in video. Still it is not working

  • @tomerfata
    @tomerfata Год назад

    Is there any option to keep the python container running?
    I tried adding tty: true and stdin_open: true to the yaml file but it still immediately exists..

    • @Randomcode_0
      @Randomcode_0  Год назад

      The simple version is to run something in the container that keeps running like any backend server. If you want to keep any Linux container running you could run a tail "-f /dev/null" command which will keep the process open.
      A docker-compose setup could then look like this:
      version: '3'
      services:
      python:
      build:
      context: .
      dockerfile: Dockerfile
      command: tail -f /dev/null

  • @SuperWaanzin
    @SuperWaanzin 2 года назад +2

    Very nice video, but sharing your code on git would be really helpfull. Then you can just copy-paste instead of typing all of this. PS: I had an issue that my port 3306 was already occupied on localhost. In this case just change the YML to 3307:3306

    • @Randomcode_0
      @Randomcode_0  2 года назад

      I agree sharing the code would be easier. But I think you learn way more writing the code yourself when it is smaller projects like this. I always share code with larger projects and I was on the fence regarding this project. I can share the code if you like?

    • @Randomcode_0
      @Randomcode_0  2 года назад

      And good to hear you found a solution when you encountered a problem!

  • @beytullahyayla7401
    @beytullahyayla7401 6 месяцев назад

    It doesnt accept the host 😢

  • @RetroCode0011100100110111
    @RetroCode0011100100110111 2 года назад

    Thank you man

  • @andressahelum5071
    @andressahelum5071 2 года назад

    Nossa, perfeito! Agradeço demais o vídeo. Assistindo do Brasil!

  • @paibroadcastingchannel-the579
    @paibroadcastingchannel-the579 Год назад

    _mysql_connector.MySQLInterfaceError: Table 'db.students' doesn't exist getting the error

    • @Randomcode_0
      @Randomcode_0  Год назад

      Your python script can not connect to the student database table, because the table has not been created in the given database. Just added a link in the description to all then files if you want to have a closer look at my setup.

    • @paibroadcastingchannel-the579
      @paibroadcastingchannel-the579 Год назад +1

      @@Randomcode_0 mysql.connector.errors.DatabaseError: 2005 (HY000): Unknown MySQL server host 'mysql' (-3) have you faced this? even if I give 50s sleep

    • @frenzydf
      @frenzydf Год назад +1

      I have the same error

    • @paibroadcastingchannel-the579
      @paibroadcastingchannel-the579 Год назад +1

      ​@@frenzydf I got the issue when you map the port , in compose you will be using the container port only not the port mapped to the local machine.
      By default anything you copy to initd folder does not have execute permissions so give chmod 777 to the .sql file CMD in mysql Dockerfile.

    • @aaditkrishnaar763
      @aaditkrishnaar763 Год назад +1

      @@paibroadcastingchannel-the579 I got this error ''Can't connect to MySQL server on 'mysql:3306" any ideas on how to solve this

  • @aliummundas5361
    @aliummundas5361 5 месяцев назад

    Is it possible to get a script to run indefinitely? I've tried with a while true looping script but it keeps exiting with exit code 0.