🐈

mysqlshでstgのデータをlocalにインポートする

2024/04/24に公開

https://dev.mysql.com/doc/refman/8.0/en/mysqlpump.html

mysqlpump is deprecated as of MySQL 8.0.34; expect it to be removed in a future version of MySQL. You can use such MySQL programs as mysqldump and MySQL Shell to perform logical backups, dump databases, and similar tasks instead.

というリファレンスを見つけまして、これからは頑張ってMySQL Shellを使っていこうかなと思いました。

また、下記を見ると
https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html

Consider using the MySQL Shell dump utilities, which provide parallel dumping with multiple threads, file compression, and progress information display, as well as cloud features such as Oracle Cloud Infrastructure Object Storage streaming, and MySQL HeatWave Service compatibility checks and modifications

と書いてあるので、dumpの方が強化されたりはしないみたいです。

それで、ステージングもしくは本番環境でもいいですが、ダンプしたデータをlocal環境のDBにインポートしたい時があると思います。その時にステージングとlocalのDB名が違う場合は、local環境に新しくDBが作成されたりして、うまくいかない場合があります。
また、local環境にステージングのDBができても大丈夫だという場合も、railsのアプリであれば、database.ymlを修正したり、面倒です。

本題

dumpするときはこんな感じのスクリプトになります。
https://github.com/na8esin/mysqlsh/blob/main/dump_stg.py

インポートするときはこんな感じです。
https://github.com/na8esin/mysqlsh/blob/main/load_local.py

mysqlshはデフォルトだとjsですが、日時系のクラスが扱いづらかったので、pythonで書いてみました。

その他の注意事項はreadmeに書いてあります。
https://github.com/na8esin/mysqlsh/blob/main/README.md

しくみのテックブログ

Discussion