How to fix MySql: index column size too large (Laravel migrate)

前端 未结 8 1346
北荒
北荒 2020-12-01 04:34

I duplicated a project with a vagrant box which installs Debian, Nginx, PhpMyAdmin, .. With the new project the Laravel\'s php artisan migrate is not working an

相关标签:
8条回答
  • 2020-12-01 05:06

    For Laravel 5.5 or higher change on your config/database.php file the engine to InnoDB ROW_FORMAT=DYNAMIC:

    'connections' => [
        ...
        'mysql' => [
            ...
            'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
     ]]
    

    Thanks to laracasts: https://laracasts.com/discuss/channels/eloquent/migrations-and-table-options-row-format

    0 讨论(0)
  • 2020-12-01 05:08

    It works with 5.5.52-MariaDB.

    Set all encoding to utf8_general_ci (server, database, connection).

    0 讨论(0)
  • 2020-12-01 05:12

    Three solutions, each with a drawback:

    • MySQL 5.7 avoids the problem. Consider upgrading.

    • VARCHAR(255) is usually var bigger than necessary. If you can safely shrink to 191 or less, the error will go away.

    • Switch to utf8 (from utf8mb4), if you don't need Chinese or Emoji.

    0 讨论(0)
  • 2020-12-01 05:21

    Also had this issue what I just did, was revert from utf8mb4_unicode_ci to utf8_unicode_ci in the db connection script

    0 讨论(0)
  • 2020-12-01 05:24

    As you can see in the error message - "The maximum column size is 767 bytes", if you want to create an index on it. A VARCHAR(255) column can take up to 765 (255*3) bytes using utf8 and 1020 (255*4) bytes using utf8mb4. This is because in MySQL utf8 takes up to 3 bytes and utf8mb4 up to 4 bytes (the real UTF8). Thus creating a VARCHAR(255) (unique) index with utf8mb4 will fail.

    This are your options to fix the problem:

    Set default collation in my.ini:

    collation_server=utf8_unicode_ci
    character_set_server=utf8
    

    Set default collation for the database when creating:

    CREATE DATABASE IF NOT EXISTS `your_db` COLLATE 'utf8_unicode_ci'
    

    Set default collation for the table/column. (I don't recommend that)

    Change the column size to 190 (varchar(190)) or less.

    Laravel 5.4 fix

    The Mysql server configuration is overwriten by Laravel's migration command. It will set the collation and charset to the configuration's version.

    Change the fields charset and collation of the db engine in the database config file located in config/database.php.

    ..
    'mysql' => [
                'driver' => 'mysql',
                'host' => env('DB_HOST', '127.0.0.1'),
                'port' => env('DB_PORT', '3306'),
                'database' => env('DB_DATABASE', 'forge'),
                'username' => env('DB_USERNAME', 'forge'),
                'password' => env('DB_PASSWORD', ''),
                //'charset' => 'utf8mb4',
                //'collation' => 'utf8mb4_unicode_ci',
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix' => '',
                'strict' => true,
                'engine' => null,
            ],
    ..
    
    0 讨论(0)
  • 2020-12-01 05:24

    For mariadb, update your *my.cnf file with following configuration,

    innodb_default_row_format=dynamic
    innodb_file_format=barracuda
    innodb_file_per_table=true
    innodb_large_prefix=true
    

    Then, you have to restart mariadb service for updated configuration to take effect.

    0 讨论(0)
提交回复
热议问题