Apa cara aman untuk menghentikan mysql docker container?

Saya menjalankan wadah mysql menggunakan Docker. Saya memulainya dengan like

sudo docker -d --name mysql -p 3306:3306 -v /var/lib/mysql:/var/lib/mysql mysql_image 

Saya menduga bahwa menghentikan mysql dengan menghentikan docker tidak aman. Apa aku salah?

sudo docker stop mysql

Apakah lebih aman untuk menghentikan mysql di dalam wadah terlebih dahulu?

sudo docker exec mysql /usr/bin/mysqladmin shutdown

terlihat aman, dari docs:

Proses utama di dalam wadah akan menerima SIGTERM, dan setelah masa tenggang, SIGKILL.

jadi jika proses utamanya adalah mysqld, itu akan memiliki kesempatan yang layak untuk menyiram semuanya.

Sepertinya menghentikan wadah docker tanpa mematikan MySQL di dalam menciptakan volume data MySQL yang rusak. Jadi itu perlu untuk menjalankan MySQL shutdown sebelum berhenti kontainer agar MySQL untuk flush semua perubahan ke disk.

Ini adalah log memulai mysql pada volume, dibuat dari container, dihentikan oleh docker. Memperhatikan xa kecelakaan pemulihan langkah ada.

2021-05-18 06:34:51+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL             Server 8.0.25-1debian10 started.2021-05-18 06:34:54+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'2021-05-18 06:34:54+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.25-1debian10 started.2021-05-18T06:34:54.844455Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.25) starting as process 12021-05-18T06:34:54.887402Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.2021-05-18T06:35:00.523370Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.2021-05-18T06:35:11.094092Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock2021-05-18T06:35:11.181732Z 0 [System] [MY-010229] [Server] Starting XA crash recovery...2021-05-18T06:35:11.198947Z 0 [System] [MY-010232] [Server] XA crash recovery finished.2021-05-18T06:35:11.375917Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.2021-05-18T06:35:11.376529Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.2021-05-18T06:35:11.397202Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.2021-05-18T06:35:11.628776Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.25'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

Saat menggunakan

docker exec tu-live-db /usr/bin/mysqladmin -uroot -proot shutdown

sebelum mematikan wadah, pemulihan kerusakan XA tidak dimulai dan volume data valid untuk wadah berikutnya mulai dengan itu

2021-05-18 06:36:44+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.25-1debian10 started.2021-05-18 06:36:47+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'2021-05-18 06:36:47+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.25-1debian10 started.2021-05-18T06:36:48.040045Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.25) starting as process 12021-05-18T06:36:48.082740Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.2021-05-18T06:36:53.426493Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.2021-05-18T06:36:57.611953Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock2021-05-18T06:36:57.827561Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.2021-05-18T06:36:57.828060Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.2021-05-18T06:36:57.845291Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.2021-05-18T06:36:58.014550Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.25'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
  1. Masuk ke dalam host tamu menggunakan terminal perintah Anda

    $ docker exec -it mariadb /bin/sh
  2. Masuk ke dalam SQL_command_Line

    # mysql -u root -p
  3. Perintah shutdown database di dalam sana.

    > shutdown;

Anda dapat memeriksa log dari wadah mysql

docker container logs mysql

untuk mengkonfirmasi apakah prosedur shutdown aman atau tidak

untuk melihat baris terakhir jika menunjukkan:

[Catatan] mysqld: Shutdown selesai

itu harus aman