laravel5.5 Syntax Error or Access violation:1071 Specified key was too long

Source: Internet
Author: User
Tags mysql version php mysql

When you perform a data migration in laravel5.5

PHP Artisan Migrate

First, the system environment:

Ubutun Ubuntu 16.04.3 LTS

Mysql:5.6.35-log

The character encoding is used by default since Laravel 5.4 utf8mb4 , not the previous utf8 encoding. As a result, the php artisan migrate  above error will occur.

Source of the problem

MySQL supports encoding with a utf8 maximum character length of 3 bytes, and an insert exception occurs if a wide character of 4 bytes is encountered. Three bytes UTF-8 the maximum encoding Unicode character is 0xFFFF, which is the basic Multilingual Plane (BMP) in Unicode. Thus, the Unicode characters of non-basic multilingual planes, including emoji emoticons (emoji is a special Unicode encoding), cannot be stored using the MySQL utf8 character set.

This should also be one of the reasons why Laravel 5.4 used 4-byte-length utf8mb4 character encoding instead. It is important to note, however, that only MySQL 5.5.3 version will begin to support utf8mb4 character encoding (view version: selection version(); ). If the MySQL version is too low, a version update is required.

Note: If you are upgrading from Laravel 5.3 to Laravel 5.4, you do not need to switch the character encoding.

Solve the problem
    1. Upgrade MySQL version to 5.5.3 or more.

    2. Laravel default 1071, and the error indicates that the database set the maximum is 767, so the error

      Laravel 5.4 uses utf8mb4 the character encoding by default instead of the previous utf8 encoding. The maximum character length for Mb4 is 4 bytes, and the workaround is:

      Manually configure the migration command migrate the resulting default string length, calling Schema in Appserviceprovider::d the Efaultstringlength method to implement the configuration:

Perform:p hp artisan migrate again

It seems that the mistake is different. After looking for MySQL to perform a security mode check, Laravel's config/database.php mysql option configured strict mode by default to True, modified to:

After executing again:

Ok.. Complete..

laravel5.5 Syntax Error or Access violation:1071 Specified key was too long

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.