在docker error下迁移mysql5/laravel 5指定的密钥太长

ljsrvy3e  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(306)

我尝试在docker下安装我的laravel 5.7.19应用程序,运行迁移时出现错误:

Migrating: 2018_01_01_145312_create_settings_table
Specified key was too long
   Illuminate\Database\QueryException  : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `settings` add unique `settings_name_unique`(`name`))

  at /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668| 

  Exception trace:

其中设置定义为:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateSettingsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('settings', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->string('name', 255)->unique();
            $table->string('value', 255);
            $table->timestamp('created_at')->useCurrent();
            $table->timestamp('updated_at')->nullable();

            $table->index(['created_at'], 'settings_created_at_index');
        });
        Artisan::call('db:seed', array('--class' => 'SettingsWithInitData'));
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('settings');
    }
}

触发的种子程序文件是voces/database/seeds/settingswithinitdata.php,其中包含以下行:

public function run()
{
    DB::table('settings')->insert([
        'name' => 'site_name',
        'value' =>  'Select & Vote',
    ]);

    DB::table('settings')->insert([
        'name' => 'copyright_text',
        'value' =>  '© 2018 - 2018 All rights reserved',
    ]);

    DB::table('settings')->insert([
        'name' => 'elastic_automation',
        'value' =>  'N',
    ]);

设置->名称字段没有超过40个字符,我的灯系统没有任何问题。
在phpmyadmin中,我用utf8\u general\u ci创建了新的数据库。
正在检查phpmyadmin中创建的表我在名称字段上看不到唯一索引:https://imgur.com/a/2rfeygn show变量有很大的输出,在这种情况下哪一个变量是有意义的?
怎么了?
my docker-compose.yml有行:

version: '3.1'

services:

    web:

        build:
            context: ./web
            dockerfile: Dockerfile.yml

        environment:
            - APACHE_RUN_USER=www-data
        volumes:
            - ${APP_PATH_HOST}:${APP_PTH_CONTAINER}
        ports:
            - 8081:80
        working_dir: ${APP_PTH_CONTAINER}

    db:
        image: mysql:5.6.41
        restart: always
        environment: 
            MYSQL_ROOT_PASSWORD: 1
        volumes:
            - ${DB_PATH_HOST}:/var/lib/mysql

    phpmyadmin:
        depends_on:
          - db
        image: phpmyadmin/phpmyadmin
        restart: always
        ports:
          - 8082:80
        environment:
          PMA_HOST: db
          MYSQL_ROOT_PASSWORD: 1

    composer:
        image: composer:1.8
        volumes:
            - ${APP_PATH_HOST}:${APP_PTH_CONTAINER}
        working_dir: ${APP_PTH_CONTAINER}
        command: composer install --ignore-platform-reqs

谢谢!

mwg9r5ms

mwg9r5ms1#

这是因为你是在mysql版本下运行的 v5.7.7 . 要解决您的问题,请更改 docker-compose.yml 或者你可以编辑 AppServiceProvider.php ```
use Illuminate\Support\Facades\Schema;

public function boot()
{
// add this line
Schema::defaultStringLength(191);
}

资料来源:https://laravel-news.com/laravel-5-4-key-too-long-error

相关问题