Lavarel-MySQL建表之路

环境:Windows64,xampp
(Linux同理)

参考: Laravel 5.5快速搭建博客api系统-1

一 创建项目
  • 创建Lavarel工程

    laravel new VitaHomeBackend
    
  • 查看是否安装成功,

    php artisan serve
    

    执行完后打开http://localhost:8000, 如果能看到laravel的欢迎页面则表示安装成功

二 创建数据库
  • 修改工程根目录下.env文件,以下设置了连接数据库需要密码(默认不需要):

    APP_NAME=VitaHomeBackend
    APP_ENV=local
    APP_KEY=base64:rUsy/Zp6nAE0uOrrQK6HckRSfgefhDLX6xcGO5cYfWQ=
    APP_DEBUG=true
    APP_LOG_LEVEL=debug
    APP_URL=http://localhost
    
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=vitahomebackend
    DB_USERNAME=root
    DB_PASSWORD=root123456
    
    BROADCAST_DRIVER=log
    CACHE_DRIVER=file
    SESSION_DRIVER=file
    SESSION_LIFETIME=120
    QUEUE_DRIVER=sync
    
    REDIS_HOST=127.0.0.1
    REDIS_PASSWORD=null
    REDIS_PORT=6379
    
    MAIL_DRIVER=smtp
    MAIL_HOST=smtp.mailtrap.io
    MAIL_PORT=2525
    MAIL_USERNAME=null
    MAIL_PASSWORD=null
    MAIL_ENCRYPTION=null
    
    PUSHER_APP_ID=
    PUSHER_APP_KEY=
    PUSHER_APP_SECRET=
    

    解释:

    • DB_DATABASE=vitahomebackend:设置了数据库库名为vitahomebackend,需要在MySQL中建立该数据库,详情见后面介绍
    • DB_USERNAME=root:设置了访问数据库时的用户名
    • DB_PASSWORD=root123456:设置了访问数据库时的密码

    注意:config/database.php不需要更改

此时,在lavarel工程中的相关配置已更改完成,后面要进行的是建库建表操作,在Lavarel中建表之前请确保MySQL中已建好对应的数据库(如,这里的数据库名为vitahomebackend

  • 建立vitahomebackend数据库

    本地环境:xampp

    参考: 本地建站xampp软件如何创建数据库

    ​ 修改xampp默认sql密码

    1. 下载xampp安装包,注意版本,推荐xampp-win32-7.1.4-0-VC14-installer,安装

    2. 如果之前有配置过PHP的环境变量,需要将环境变量改为xampp下的PHP路径,如:D:\xampp\php;如果没有,xampp会自行配置

    3. 安装完后打开图形界面,Start Apache 和 MySQL,未报错即成功

    4. 创建数据库

      • Start MySQL 后,点击Admin,就会打开phpMyAdmin数据库页面
      • 点击选项栏中的“数据库”,输入数据库名:vitahomebackend,创建
    5. 修改访问数据库的用户密码

      • 点击选项栏中的“权限”,点击用户“root@localhost”的“修改权限”

      • 选择上方的“修改密码”

      • “Enter”和“重新输入”框中键入前面配置文件中的密码root123456,点击“执行”

        注意:这里不需要点击“生成”,否则密码就是生成的那个字符串了

        执行成功后刷新页面,会出现错误页面,原因是没有修改配置文件,你的密码已经改为123了,系统依然按旧密码登陆,所以显示登陆失败

      • xampp安装目录下的phpMyAdmin文件夹中(D:\xampp\phpMyAdmin\),找到config.inc.php文件,并打开

      • 修改$cfg['Servers'][$i]['password'] = 'root123456';,保存退出,重启MySQL,再次打开phpMyAdmin即可访问成功

  • articles

    工程根目录下执行:

    php artisan make:migration create_articles_table
    

    运行这条命令会在工程根目录下database\migrations\下生成一个migration文件,根据需要修改该migration文件,如:

    increments('id');
                $table->string("title")->comment("文章标题");
                $table->text("body")->comment("文章内容"); 
                $table->dateTime("post_time")->comment("文章发布时间");
                $table->integer("user_id")->unsigned()->comment("作者id");
                $table->integer("last_comment_user_id")->unsigned()->default(0)->index()->comment("最后评论用户id");
                $table->integer("category_id")->unsigned()->default(0)->index()->comment("所属类别");
                $table->integer("view_count")->unsigned()->default(0)->index()->comment("查看此次数");
                $table->integer("comments_count")->unsigned()->default(0)->comment("评论数");
                $table->integer("likes_count")->unsigned()->default(0)->comment("点赞数");
                $table->enum("close_comment", [0, 1])->default(0)->index()->comment("是否关闭评论");
                $table->enum("is_public", [0, 1])->default(1)->index()->comment("是否公开");
                $table->enum("is_top", [0, 1])->default(0)->index()->comment("是否置顶");
                $table->enum("is_excellent", [0, 1])->default(0)->index()->comment("是否为精华");
                $table->timestamp('last_comment_time')->comment('最后评论时间');
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('articles');
        }
    }
    
  • 同样的方法,可以创建其他表

  • 创建完后,工程根目录下执行:

    建表前请确保该表所在数据库已建立,否则执行php artisan migrate后会报如下错误:

    [Illuminate\Database\QueryException]
    could not find driver (SQL: select * from information_schema.tables where table_schema = vitahomebackend and table_name = migrations)
    
    [PDOException]
    could not find driver
    
    php artisan migrate
    

    运行过程中如果出现:

    [PDOException]
      SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes
    

    则修改app\Providers\AppServiceProvider.php如下:

    在此执行命令结束后应该可以在数据库中看到生成了所有的表


以下为错误记录

执行php artisan migrate报错
  • 1

    • 错误信息:

      [Illuminate\Database\QueryException]
      could not find driver (SQL: select * from information_schema.tables where table_schema = vitahomebackend and table_name = migrations)
      
      [PDOException]
      could not find driver
      
    • 错误原因:

      建表之前未建库,即该表所在数据库不存在

    • 解决方法:

      建库

  • 2

    • 错误信息:

      [Illuminate\Database\QueryException]
      SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) (SQL: select * from information_schema.tables where table_schema = vitahomebackend and table_name = migrations)
      
      [PDOException]
      SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using pas
      sword: YES)
      
    • 错误原因:

      访问数据库密码不对

    • 解决方法:

      参照上面介绍重新修改密码,

      如果忘记密码,可以重装xampp,再重新配置

你可能感兴趣的:(Lavarel-MySQL建表之路)