LoginSignup
1
1

More than 5 years have passed since last update.

【2018年12月実施】 MySQLのデータインポート手順

Posted at

前提条件

ローカル環境(MAMP)で作成したWordPressサイトを本番環境(Linux)に移行しました。phpmyadminからエクスポートしたデータ(**.sql)を、MySQLにインポートするまでの手順を紹介します。

MySQLのバージョン

$ mysql --version
$ mysql Ver 14.14 Distrib 5.6.28, for Linux (x86_64) using EditLine wrapper

MySQLコマンド

<今回設定するDB情報>
ユーザ名:test
パスワード:test
ホスト名:localhost
データベース:testdb
SQLデータ:swp.sql

①MySQLにログイン

//$ mysql -u [user_name] -p

$ mysql -u root -p

②ユーザの追加

//mysql > create user [user_name]@[host_name] IDENTIFIED BY ['password'];

mysql > create user test@localhost IDENTIFIED BY 'test';

③データベースの追加

//mysql > create database [db_name];

mysql > create database testdb;

④ユーザにDB権限付与

//mysql > grant [権限] on [password].[table] to [user_name]@[host_name] IDENTIFIED BY['password'];

mysql > grant all on test.* test@localhost IDENTIFIED BY 'test';

⑤ MySQLデータのインポート

//$ mysql -u [user_name] -p [db_name] < /[sql_data]

$ mysql -u root -p testdb < /tmp/wp.sql
Enter password:

⑥SQLデータが正常にインポートされたか?を確認

⑥-① MySQLにログイン

//mysql -u [user_name] -p
mysql -u root -p

⑥-② データベースを選択

//mysql > use [db_name];
mysql > use testdb;

⑥-③ データベースのテーブルを表示

mysql > show tables; //一覧表示
mysql > show table status; //詳細表示

おわり

show tablesコマンドでインポートしたデータが表示されたら、インポート完了です。
mysqlのデータインポート手順はQiitaにいくつもありますが、検索で上位で出るのは3年以上前の記事が多かったので改めて作成しました。

お願い

コマンドの間違いや内容の誤りがあれば、ご指摘ください。随時修正します。

おまけ

①よく出るエラー「ERROR 1064(42000)」

ERROR 1064(42000): You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near ‘*****‘ at line 1

//SQLの文法に誤りがあるときに表示される。
//文字列を「'(シングルコーテーション)」でくくらなかったり、コマンドの最後に;(セミコロン)を入れ忘れると出る。

②参考サイト

「MySQLでユーザーを作成し、権限を設定する方法」
http://proengineer.internous.co.jp/content/columnfeature/6638

「MySQLのデータインポート・エクスポート」
https://qiita.com/rato303/items/2e614f23e5feee150ffc

1
1
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
1
1