如何从默认MySQL 5.7版本升级至MySQL 8.0(非重装防数据损坏)
Hey there! Totally get why you're cautious here—upgrading MySQL without messing up your data is no joke, especially since 8.0 was still relatively new when you asked. Let’s walk through a safe, in-place upgrade process that skips a full reinstall, so you can unlock all those great 8.0 features without losing a single row of data.
Safe In-Place Upgrade from MySQL 5.7 to 8.0 (No Reinstall Needed)
Pre-Upgrade Prep (Critical—Don’t Skip!)
- Backup everything first: This is non-negotiable. Use
mysqldumpto create a full backup of all your databases. Run this command (replacerootwith your admin user if needed):
Store this file somewhere safe (not on the same server!). Also, copy your MySQL config files (mysqldump -u root -p --all-databases --routines --triggers --events > full_mysql_backup.sql/etc/my.cnfor/etc/mysql/my.cnfdepending on your OS) to a backup location. - Check for deprecated features: MySQL 8.0 removes or modifies some 5.7 features. Run the built-in upgrade checker to catch issues early:
Fix any flags it throws—like old authentication plugins, deprecated SQL syntax, or MyISAM tables (consider converting these to InnoDB if possible, since MyISAM has limited support in 8.0).mysqlcheck -u root -p --all-databases --check-upgrade - Update system packages: Make sure your OS is fully updated to avoid dependency conflicts during the upgrade.
Step-by-Step Upgrade Process
- Stop the MySQL 5.7 service:
# For systemd systems (Ubuntu 16.04+, CentOS 7+) sudo systemctl stop mysql # For SysVinit systems sudo service mysql stop - Add the official MySQL 8.0 repository:
The exact steps depend on your OS:- Ubuntu/Debian:
wget https://dev.mysql.com/get/mysql-apt-config_0.8.22-1_all.deb sudo dpkg -i mysql-apt-config_0.8.22-1_all.deb # Select MySQL 8.0 from the interactive prompt, then update apt sudo apt update - CentOS/RHEL:
sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm sudo yum-config-manager --enable mysql80-community
- Ubuntu/Debian:
- Upgrade MySQL packages (don’t install fresh!):
Use your package manager to upgrade existing 5.7 packages to 8.0—this preserves your data directory:
Follow any on-screen prompts; this replaces 5.7 binaries with 8.0 ones without wiping your data.# Ubuntu/Debian sudo apt upgrade mysql-server # CentOS/RHEL sudo yum upgrade mysql-community-server - Run the system table upgrade script:
MySQL 8.0 needs to update its system tables to the new schema. Start the service (it might fail initially—this is normal):
This script fixes schema mismatches and ensures system tables work with 8.0.sudo systemctl start mysql # Run the upgrade script mysql_upgrade -u root -p - Verify the upgrade:
Log into MySQL to confirm you’re running 8.0:
Inside the MySQL shell, run:mysql -u root -p
You should see output likeSELECT VERSION();8.0.x. Double-check that all your databases and tables are present and accessible. - Tweak config settings:
MySQL 8.0 has new default settings. Open your backupmy.cnffile and update any deprecated options. For example,default_authentication_plugindefaults tocaching_sha2_password—if your apps don’t support this yet, you can temporarily set it back tomysql_native_passworduntil you update your applications.
Post-Upgrade Checks
- Test all your applications to ensure they connect and function correctly with MySQL 8.0.
- Monitor the MySQL error log (
/var/log/mysql/error.logor similar) for warnings or unexpected errors. - If something goes wrong, roll back using your backup: stop the 8.0 service, restore 5.7 binaries, then import your backup SQL file with
mysql -u root -p < full_mysql_backup.sql.
Pro tip: If you’re still nervous, test this entire process on a staging server first! That way you can work out any kinks without risking production data.
内容的提问来源于stack exchange,提问作者Imnotapotato




