LoginSignup
0
0

複数のダンプファイル(.sql)をまるごと仮想マシンのDBへインポートする手順 [Vagrant MySQL]

Posted at

やりたいこと

  • mysqlの複数のdumpデータをローカルの新規DBにまるごと取り込みたい

インポート方法

準備

  • Dumpfile_20230101 ディレクトリ直下に複数.sqlファイルが存在する想定
  • vagrant sshしてから、 mysql -u root -p でmysqlに入る
  • CREATE DATABASE new_database; でimportさせる新規DBを作成しておく
  • ホストマシンとゲストマシンとの間でディレクトリを同期させるため、Vagrantfileに以下記述を追加
    • MacOSの /Users/ユーザー名/Downloads/Dumpfile_20230101/ と
      仮想マシンの /home/vagrant/Dumpfile_20230101/ ディレクトリが同期される設定を追加
Vagrantfile
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # 省略
  
  # 以下1行を追加 (ホストマシンとの同期)
  config.vm.synced_folder "/Users/ユーザー名/Downloads/Dumpfile_20230101/", "/home/vagrant/Dumpfile_20230101/"
end

インポート手順

  • ゲストマシン(Vagrant)に入る
vagrant reload
vagrant ssh
  • 以下のスクリプトを実行する
  • pass はmysqlのパスワード
for sql_file in /home/vagrant/Dumpfile_20230101/*.sql; do mysql -u root -ppass new_database < "$sql_file"; done
  • 実行中、エラーがなければ新規DBにテーブルが追加される
Warning: Using a password on the command line interface can be insecure.
Warning: Using a password on the command line interface can be insecure.
Warning: Using a password on the command line interface can be insecure.
Warning: Using a password on the command line interface can be insecure.
Warning: Using a password on the command line interface can be insecure.
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0