Skip to content

Instantly share code, notes, and snippets.

@CosminEugenDinu
Last active August 26, 2020 22:14
Show Gist options
  • Save CosminEugenDinu/7ef73816f950b1ba8a9fd1eabb23c42e to your computer and use it in GitHub Desktop.
Save CosminEugenDinu/7ef73816f950b1ba8a9fd1eabb23c42e to your computer and use it in GitHub Desktop.
Shell script for installing MySql 8.x on WSL2 Ubuntu18.04

Install MySql 8.x on WSL2 Ubuntu18.04

(
git clone https://gist.github.com/CosminEugenDinu/7ef73816f950b1ba8a9fd1eabb23c42e install_mysql8
chmod 740 install_mysql8/wsl2-ubuntu18.04-mysql8.0.sh
./install_mysql8/wsl2-ubuntu18.04-mysql8.0.sh
)

Enter MySql console in order to setup database, users, etc.:

sudo mysql
# or $sudo mysql -u root -p (if you set up root password)
mysql> create database db_name;
mysql> create user db_user@localhost identified by 'db_user_pass';
mysql> grant all on db_name.* to db_user@localhost;
mysql> exit

Import backup database data

sudo mysql db_name < /full/path/to/backup.sql
#!/usr/bin/env bash
# uninstall existing mysql 8
sudo service mysql stop
sudo apt remove --purge mysql-common -y # Remove data directories? <Yes>
sudo apt remove --purge mysql-community* -y
sudo apt remove --purge mysql-apt-config -y
sudo rm -rf /var/lib/mysql
sudo rm -rf /var/log/mysql
sudo rm -rf /etc/mysql
sudo apt autoremove -y && sudo apt autoclean
# install from debian package
mysql_apt_deb=mysql-apt-config_0.8.14-1_all.deb
wget –c "https://dev.mysql.com/get/${mysql_apt_deb}"
# When prompted, select 8.0 (if default, arrow-down to OK and press Enter)
sudo dpkg -i $mysql_apt_deb
rm $mysql_apt_deb
sudo apt update
# install mysql-server
# should be version 8.x
sudo apt install mysql-server -y
# download script mysql.server.sh
wget -c "https://raw.githubusercontent.com/mysql/mysql-server/8.0/support-files/mysql.server.sh"
# change lines basedir=/usr and datadir=/var/lib/mysql
# move the script renamed as mysql ( /etc/init.d/mysql )
sudo mv mysql.server.sh /etc/init.d/mysql
# make sure it is executable
sudo chmod +x /etc/init.d/mysql
# Add the path to the mysql-installation-directory to the basedir variable
# If you change base dir, you must also change datadir, like:
#[mysqld]
# basedir=/usr
# datadir=/var/lib/mysql
# in configuration file /etc/mysql/my.cnf
# for now let's change only basedir=/usr
printf "
[mysqld]
basedir=/usr
" | sudo tee -a /etc/mysql/my.cnf
# got error after reinstalling: mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists
# so check if exists and if not make it
if [ ! -d "/var/run/mysqld" ]; then
sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld
fi
# start mysql server
sudo service mysql start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment