在Laravel 5.4上使用php artisan make:auth迁移错误

[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter tabl e users add unique users_email_unique(email)) [PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes


当前回答

对于不想更改AppServiceProvider.php的人。 (在我看来,仅仅为了迁移而更改AppServiceProvider.php是一个坏主意)

您可以将数据长度添加回database/migrations/下的迁移文件,如下所示:

create_users_table.php

$table->string('name',64);
$table->string('email',128)->unique();

create_password_resets_table.php

$table->string('email',128)->index();

其他回答

腔:

添加到。env文件

DB_ENGINE=InnoDB

如果您运行“php artisan migrate”时出现此错误。你可以这样修改你想要更新的表:

    DB::statement('ALTER TABLE table_name ROW_FORMAT = DYNAMIC;');        

在迁移脚本中。例子:

class MyMigration extends Migration {

/**
 * Run the migrations.
 */
public function up()
{
    DB::statement('ALTER TABLE table_name ROW_FORMAT = DYNAMIC;');        
    Schema::table('table_name', function ($table) {
        //....
    });
}

/**
 * Undo the migrations.
 */
public function down()
{
    //....
}
}

然后再次运行php artisan migrate

只要在/etc/mysql/mariadb.conf.d/50-server.cnf或其他配置文件中写几行:

innodb-file-format=barracuda
innodb-file-per-table=ON
innodb-large-prefix=ON
innodb_default_row_format = 'DYNAMIC'

和sudo服务mysql重启

https://stackoverflow.com/a/57465235/778234

查看文档:https://dev.mysql.com/doc/refman/5.6/en/innodb-row-format.html

模式:defaultStringLength (191);将默认定义所有字符串191的长度,这可能会破坏您的数据库。你不能走这条路。

只需在数据库迁移类中定义任何特定列的长度。例如,我在CreateUsersTable类中定义了“name”,“username”和“email”,如下所示:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 191);
            $table->string('username', 30)->unique();
            $table->string('email', 191)->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

没有人告诉的解决方案是,在Mysql v5.5和以后的InnoDB是默认的存储引擎,它没有这个问题,但在许多情况下,像我的,有一些旧的Mysql ini配置文件使用旧的MYISAM存储引擎,如下所示。

default-storage-engine=MYISAM

这造成了所有这些问题,解决方案是将Mysql的ini配置文件中的默认存储引擎一劳永逸地更改为InnoDB,而不是做临时的hack。

default-storage-engine=InnoDB

如果你在MySql v5.5或更高版本,那么InnoDB是默认引擎,所以你不需要像上面那样显式地设置它,只要从你的ini文件中删除default-storage-engine=MYISAM(如果它存在),你就可以开始了。