Upgrade MySQL 5.7 to 8 via Shell script

Here we will write a Shell script to upgrade MySQL 5.7 to 8.


# Stop MySQL 5.7 server
sudo systemctl stop mysql

# Backup MySQL 5.7 data
sudo mkdir /var/lib/mysql_backup
sudo cp -R /var/lib/mysql/* /var/lib/mysql_backup

# Remove MySQL 5.7
sudo apt-get remove mysql-server mysql-client mysql-common

# Add MySQL 8.0 repository
sudo wget -c https://dev.mysql.com/get/mysql-apt-config_0.8.13-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.13-1_all.deb

# Update repository
sudo apt-get update

# Install MySQL 8.0
sudo apt-get install mysql-server

# Start MySQL 8.0 server
sudo systemctl start mysql

# Upgrade MySQL data
sudo mysql_upgrade -u root -p

# Clean up
sudo rm mysql-apt-config_0.8.13-1_all.deb
sudo rm -rf /var/lib/mysql_backup

How it works:

This script does the following:

  1. Stops the MySQL 5.7 server.
  2. Backs up the MySQL 5.7 data to /var/lib/mysql_backup.
  3. Removes MySQL 5.7 using apt-get.
  4. Adds the MySQL 8.0 repository and updates it.
  5. Installs MySQL 8.0 using apt-get.
  6. Starts the MySQL 8.0 server.
  7. Upgrades the MySQL data using mysql_upgrade.
  8. Cleans up the backup and MySQL 8.0 repository files.

Note that this script assumes that you are running Ubuntu or a similar Linux distribution that uses apt-get for package management. If you are using a different distribution or package manager, you may need to modify the script accordingly. Additionally, make sure to back up your MySQL data before running the script, as the upgrade process may result in data loss.

Leave a Comment