17 Mart 2022 Perşembe

mysqldump komutu

Giriş
Söz dizimi şöyle
mysqldump -u [user name] –p [password] [options] [database_name] [tablename]
  > [dumpfilename.sql]
mysql dump yerine Alternatif
Açıklaması şöyle
You can back up and restore the entire database way faster by copying the data files instead of dumping SQL

As your database grows larger dumping sql files gets slower and restoring it would even take longer, for example a 100GB database the restoring part might take around 1 day or even more which is not ideal.

Things to consider before doing this: 

- You MUST stop the database before copying, and it SHOULD be gracefully, for example if you’re using docker consider using the -t option for increasing the timeout, for example docker stop -t 8000
- you SHOULD have your database configuration files and the exact version of MySQL backed up as well for restoring, docker also helps a lot with this as you can easily have them as code.

--databases seçeneği
Belirtilen veri tabanlarının yedeğini alır. Eğer --all-databases seçeneğini kullanırsak tüm veri tabanlarının yedeğini alır.

--host seçeneği
Bağlanmak istenilen veri tabanı belirtilir.

Örnek
Şöyle yaparız.
mysqldump --host=localhost  -uroot -p"mypassword"  my_db_name > file.sql
--master-data seçeneği
Şöyle yaparız
mysqldump -uroot -p --host=127.0.0.1 --port=3306 --all-databases \
  --master-data=2 > replicationdump.sql
Açıklaması şöyle
Here we use the option --master-data=2 in order to have a comment containing a CHANGE MASTER statement inside the backup file. That comment indicates the replication coordinates at the time of the backup, and we will need those coordinates later for the update of master information in the slave instance. Here is the example of that comment:
Yani yedek dosyasında şöyle bir satır vardır.
--
-- Position to start replication or point-in-time recovery from
--

-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=349;
Bu satırdaki bilgi replica sunucuya şöyle yazılır. Böylece replica sunucu artık hangi satırdan başlayacağını bilir.
mysql> CHANGE MASTER TO
  -> MASTER_HOST='127.0.0.1',
  -> MASTER_USER='replication',
  -> MASTER_PASSWORD='replication',
  -> MASTER_LOG_FILE='mysql-bin.000001',
  -> MASTER_LOG_POS=349;
--no-data seçeneği
Açıklaması şöyle. Tablodaki veriyi yedeklemez.
If you want to generate the backup of the database structure, then you must use the --no-data option in the mysqldump command.
Örnek
Şöyle yaparız
mysqldump -h 127.0.0.1 -P 15306 -u user --no-data ADV
--where seçeneği
Filtre olarak kullanılacak SQL belirtirlir.
Örnek
Şöyle yaparız.
mysqldump -u root -p  dbName tableName --where="id>=10000 AND id<20000" > file.sql

Hiç yorum yok:

Yorum Gönder

Soft Delete

Giriş Açıklaması  şöyle When using the soft delete mechanism on the database, you might run into a situation where a record with a unique co...