ブログタイトル募集中の新人SE記

ネットの片隅で非力を嘆く

DB周りのメモ(mysql)

DB設定

mysqlに接続

  • 接続
$ mysql -u <UserName> -p
  • 'Enter password:'と聞かれるのでパスワードを入力する
Enter password: <Password>
  • 切断
mysql> quit;

ユーザ

  • ユーザの確認
mysql> select User,Host from mysql.user;
+---------+-----------+
| User    | Host      |
+---------+-----------+
| root    | 127.0.0.1 |
| root    | ::1       |
| root    | localhost |
+---------+-----------+
3 rows in set (0.00 sec)
  • ユーザ作成 ホスト名を省略すると'<ユーザ名>'@''%(ワイルドカード)'と記述したものとして扱われる
mysql> create user '<UserName>'@'<HostName>' identified by '<Password>';
  • ユーザの削除 間違えたコマンドで不要なユーザが大量生産されたので削除しましたw
mysql> drop user '<UserName>'@'<HostName>';
  • 権限付与
GRANT <操作名> ON <データベース名>.<テーブル名>TO <ユーザ名>@<ホスト名> IDENTIFIED BY '<パスワード>';

データベース

  • データベースの確認
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
  • データベース作成
mysql> create database <DatabaseName>;
  • 操作するデータベースを選択
mysql> use <DatabaseName>;

テーブル

  • テーブルの確認
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
=============================
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
28 rows in set (0.00 sec)

マイグレーション

$ php artisan make:migration <マイグレーションファイル名> --create=<作成したいテーブル名>
<?php
 
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
 
class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     * テーブル作成の手続きを記述
     * 
     * @return void
     */
    public function up()
    {
        Schema::create('<作成したいテーブル名>', function (Blueprint $table) {
            //下記に作成したいカラムを記述
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }
 
    /**
     * Reverse the migrations.
     * upメソッドの内容を元に戻す手続きを記述
     * 
     * @return void
     */
    public function down()
    {
        Schema::drop('<作成したいテーブル名>');
    }
}
$ php artisan migrate

シーダー

  • db:seedコマンドで実行されるdatabase/seeds/DatabaseSeeder.phpを編集
<?php
 
use Illuminate\Database\Seeder;
 
class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //テーブルごとのSeederクラスをcallメソッドで呼ぶ
        $this->call(UsersTableSeeder::class);
    }
}
  • 各テーブルごとのシーダーファイル作成
$ php make:seeder <シーダーファイル名>
  • 作成されたシーダーファイル内のrunメソッドにデータ挿入処理を記述
<?php
 
use Illuminate\Database\Seeder;
 
class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //データクリア
        DB::table('users')->delete();
        //データ挿入処理
        $users = [];
        for($i  = 1; $i <= 20; $i++){
            $users[] = [
                'name' => '名前'.$i,
                'email' => 'mail'.$i.'@email.com',
                'password' => 'pass'.$i
            ];
        }
        foreach($users as $user){
            DB::table('users')->insert($user);
        }
    }
}
  • シーダーの実行
$ php artisan db:seed
Seeded: UsersTableSeeder
  • なにしても動かなかったから下記コマンド実行したら動いためも
$ composer dump-autoload