ucan-lab / docker-laravel

🐳 Build a simple laravel development environment with Docker Compose.
https://dev.to/ucan_lab/how-to-install-and-set-up-laravel-s-local-development-environment-with-docker-compose-5bcf
MIT License
1.17k stars 377 forks source link

please add mysql local volume #270

Closed vahidalvandi closed 1 month ago

vahidalvandi commented 5 months ago

With this method, the data will not be lost after resetting the container

    volumes:
      - ./data/mysql:/var/lib/mysql:rw
ucan-lab commented 5 months ago

The top-level volume is specified in compose.yaml. Even if you destroy a container, the data in the volume remains.

volumes:
  db-store:

  db:
    volumes:
      - type: volume
        source: db-store
        target: /var/lib/mysql
        volume:
          nocopy: true
vahidalvandi commented 5 months ago

Is it possible to move the volume to another server? If it is not possible, it is better to save it locally

ucan-lab commented 5 months ago

@vahidalvandi I can't think of any use case where I would like to store the volume on another server. I don't think you should directly touch files under /var/lib/mysql except in special cases. I set it as a volume to prevent users from touching /var/lib/mysql.

If you want to extract MySQL data, I recommend using mysqldump to back up and restore it.

Backup

$ docker compose exec db bash -c 'mysqldump --no-tablespaces -u $MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE > /tmp/dump.sql'
$ docker compose cp db:/tmp/dump.sql /tmp/dump.sql

Restore

$ docker compose cp /tmp/dump.sql db:/tmp/dump.sql
$ docker compose exec db bash -c 'mysql -u $MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE < /tmp/dump.sql'
$ rm /tmp/dump.sql
vahidalvandi commented 5 months ago

i use this container for automatic backup

  backup:
    image: ghcr.io/realorangeone/db-auto-backup:latest
    restart: unless-stopped
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./backups:/var/backups
    environment:
      - SUCCESS_HOOK_URL=
      - INCLUDE_LOGS=true
ucan-lab commented 1 month ago

I have considered it, but have no plans to introduce it in this repository.