添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

// Tutorial //

How To Prepare For Your MySQL 5.7 Upgrade

Published on April 20, 2015
Default avatar

By Morgan Tocker

How To Prepare For Your MySQL 5.7 Upgrade

An Article from the MySQL Team at Oracle

Introduction

over 1.1 million requests per second .

Before you rush to run mysql_upgrade , though, you should make sure you’re prepared. This tutorial can help you do just that.

Note: As of July 1, 2022, DigitalOcean no longer supports the creation of new FreeBSD Droplets through the Control Panel or API. However, you can still spin up FreeBSD Droplets using a custom image. Learn how to import a custom image to DigitalOcean by following our product documentation .

Data Integrity Changes, with Examples

1) Inserting a negative value into an unsigned column

2) Division by zero

3) Inserting a 20 character string into a 10 character column

4) Inserting the non standard zero date into a datetime column

5) Using GROUP BY and selecting an ambiguous column

Understanding Behaviors Set by sql_mode

  • ONLY_FULL_GROUP_BY
  • STRICT_TRANS_TABLES
  • NO_ENGINE_SUBSTITUTION
  • NO_AUTO_CREATE_USER
  • The mode STRICT_TRANS_TABLES has also become more strict, and enables the behaviour previously specified under the modes ERROR_FOR_DIVISION_BY_ZERO, NO_ZERO_DATE and NO_ZERO_IN_DATE.

    Click on any of these mode names to visit the MySQL manual, to find out more information.

    Suggestions on How to Transition

  • Whitelist: Have new parts of your application enable the new default <tt>sql_mode</tt> options. For example, if you are building a set of cron jobs to rebuild caches of data, these can set the <tt>sql_mode</tt> as soon as they connect to MySQL. Existing application code can initially stay with the existing non-strict behaviour.
  • Blacklist: When you have made some headway in converting applications, it is time to make the new <tt>sql_mode</tt> the default for your server. It is possible to still have legacy applications the previous behaviour by having them change the sql_mode when they connect to MySQL. On an individual statement basis, MySQL also supports the IGNORE modifier to downgrade errors. For example: INSERT IGNORE INTO my_table . . .
  • Staged Rollout: If you are in control of your application, you may be able to implement a feature to change the sql_mode on a per user-basis. A good use case for this would be to allow internal users to beta test everything to allow for a more gradual transition.
  • Step 1 — Finding Incompatible Statements that Produce Warnings or Errors

    performance_schema is a diagnostic feature which is enabled by default on MySQL 5.6 and above. Using the performance_schema, it’s possible to write a query to return all the statements the server has encountered that have produced errors or warnings.

    MySQL 5.6+ query to report statements that produce errors or warnings:

    SELECT 
    `DIGEST_TEXT` AS `query`,
    `SCHEMA_NAME` AS `db`,
    `COUNT_STAR` AS `exec_count`,
    `SUM_ERRORS` AS `errors`,
    (ifnull((`SUM_ERRORS` / nullif(`COUNT_STAR`,0)),0) * 100) AS `error_pct`,
    `SUM_WARNINGS` AS `warnings`,
    (ifnull((`SUM_WARNINGS` / nullif(`COUNT_STAR`,0)),0) * 100) AS `warning_pct`,
    `FIRST_SEEN` AS `first_seen`,
    `LAST_SEEN` AS `last_seen`,
    `DIGEST` AS `digest`
     performance_schema.events_statements_summary_by_digest
    WHERE
    ((`SUM_ERRORS` &gt; 0) OR (`SUM_WARNINGS` &gt; 0))
    ORDER BY
     `SUM_ERRORS` DESC,
     `SUM_WARNINGS` DESC;
    

    MySQL 5.6+ query to report statements that produce errors:

    SELECT 
    `DIGEST_TEXT` AS `query`,
    `SCHEMA_NAME` AS `db`,
    `COUNT_STAR` AS `exec_count`,
    `SUM_ERRORS` AS `errors`,
    (ifnull((`SUM_ERRORS` / nullif(`COUNT_STAR`,0)),0) * 100) AS `error_pct`,
    `SUM_WARNINGS` AS `warnings`,
    (ifnull((`SUM_WARNINGS` / nullif(`COUNT_STAR`,0)),0) * 100) AS `warning_pct`,
    `FIRST_SEEN` AS `first_seen`,
    `LAST_SEEN` AS `last_seen`,
    `DIGEST` AS `digest`
     performance_schema.events_statements_summary_by_digest
    WHERE
     `SUM_ERRORS` &gt; 0
    ORDER BY
     `SUM_ERRORS` DESC,
     `SUM_WARNINGS` DESC;
    

    Step 2 — Making MySQL 5.6 Behave Like MySQL 5.7

    GitHub project available with a sample configuration file that will allow you to do this. By using the upcoming defaults in MySQL 5.6 you will be able to eliminate the chance that your application will depend on the less-strict behaviour.

    The file is rather short, so we’re also including it here:

    # This makes a MySQL 5.6 server behave similar to the new defaults
    # in MySQL 5.7
    [mysqld]
    # MySQL 5.7 enables more SQL modes by default, but also
    # merges ERROR_FOR_DIVISION_BY_ZERO, NO_ZERO_DATE, NO_ZERO_IN_DATE
    # into the definition of STRICT_TRANS_TABLES.
    # Context: http://dev.mysql.com/worklog/task/?id=7467
    sql-mode="ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,ERROR_FOR_DIVISION_BY_ZERO,NO_ZERO_DATE,NO_ZERO_IN_DATE"
    # The optimizer changes the default from 10 dives to 200 dives by default
    # Context: http://mysqlserverteam.com/you-asked-for-it-new-default-for-eq_range_index_dive_limit/
    eq_range_index_dive_limit=200
    # MySQL 5.7 contains a new internal server logging API.
    # The setting log_warnings is deprecated in 5.7.2 in favour of log_error_verbosity.
    # *But* the default fo log_warnings also changes to 2 as well:
    log_warnings=2
    # MySQL 5.7.7 changes a number of replication defaults
    # Binary logging is still disabled, but will default to ROW when enabled.
    binlog_format=ROW
    sync_binlog=1
    slave_net_timeout=60
    # InnoDB defaults to the new Dynamic Row format with Barracuda file format.
    # large_prefix is also enabled, which allows for longer index values.
    innodb_strict_mode=1
    innodb_file_format=Barracuda
    innodb_large_prefix=1
    innodb_purge_threads=4 # coming in 5.7.8
    innodb_checksum_algorithm=crc32
    # In MySQL 5.7 only 20% of the pool will be dumped, 
    # But 5.6 does not support this option
    innodb_buffer_pool_dump_at_shutdown=1
    innodb_buffer_pool_load_at_startup=1
    # These two options had different names in previous versions
    # (binlogging_impossible_mode,simplified_binlog_gtid_recovery)
    # This config file targets 5.6.23+, but includes the 'loose' modifier to not fail
    # prior versions.
    loose-binlog_error_action=ABORT_SERVER
    loose-binlog_gtid_recovery_simplified=1
    # 5.7 enable additional P_S consumers by default
    # This one is supported in 5.6 as well.
    performance-schema-consumer-events_statements_history=ON
    

    (Optional) Step 3 — Changing sql_mode on a Per Session Basis

    Ready to Upgrade

    MySQL’s official upgrade guide to flip the switch.

    Conclusion

  • What’s New in MySQL 5.7? (So Far)
  • What’s New in MySQL 5.7? (First Release Candidate)
  • Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

    Learn more about us


    About the authors
    Default avatar
    Morgan Tocker

    author



    Still looking for an answer?

    Ask a question Search for more help

    Was this helpful?
    4 Comments
    

    This textbox defaults to using Markdown to format your answer.

    You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

    romanmi December 15, 2021

    There are issues in diagnostic SQL queries in article: Please correct to “>” instead of

    Or just add to config:

    [mysqld]
    sql_mode = ""
    

    and forget all this “Degradation”

    Re: “5) Using GROUP BY and selecting an ambiguous column”

    Please update the example to include the correct Group By syntax in 5.7

    Another one - missing default values on int

    CREATE TABLE test (  
     id int,
     weight int NOT NULL  
    

    Previous behavior:

    INSERT INTO test (id) VALUES (1);
    Query OK, 1 row affected, 1 warning (0.00 sec)
    

    MySQL 5.7:

    INSERT INTO test (id) VALUES (1);