While considering to install MySQL on Ubuntu 18.04 one should look into a couple of caveats – first, the Ubuntu 18.04 repositories contain the old MySQL version i.e. 5.7; second, people often prefer to move away from MySQL which is now controlled by Oracle Corporation and install MariaDB, which is the fork of MySQL and maintained by the original developers of MySQL.
The latest version of MariaDB as of writing this article is 10.7 – so, let’s install MariaDB 10.7 on Ubuntu 18.04. First, uninstall MySQL’s old version, if it’s already installed (from distribution repositories), by executing following commands;
sudo systemctl stop mysql
sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo apt-get autoremove
sudo apt-get autoclean
sudo rm -rf /var/lib/mysql/
sudo rm -rf /etc/mysql/
Now, install repository for MariaDB 10.7 by entering following commands;
sudo apt-get install software-properties-common
sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] https://mariadb.mirror.liquidtelecom.com/repo/10.7/ubuntu bionic main'
sudo apt update
Having updated the repositories, let’s install MariaDB 10.7 by entering following command;
sudo apt install mariadb-server mariadb-client
You can start MariaDB by entering following command;
sudo systemctl start mysql
For logging into MariaDB for the first time, you can use the following command;
sudo mysql
It is recommended that right after installation of MariaDB as given above, one should run the following command in order to set root password (on fresh installation, root password is blank) and other essential settings. Good news is that most of questions here should be answered as ‘Y’
sudo mysql_secure_installation
That’s it. Now you can log into MariaDB by entering the following command and then providing password for root;
mysql -u root -p
There is another shortest possible method of replacing MySQL with MariaDB without uninstalling MySQL. Just two steps are needed here; first, stop MySQL server (if it’s running) by entering following command;
sudo systemctl stop mysql
And then directly install MariaDB server (having MariaDB 10.7 repositories installed as instructed above) by entering following command, it’ll automagically replace MySQL with MariaDB 10.7;
sudo apt install mariadb-server
By following the second method, you’d be scratching your head as to what just happened. Remember, we mentioned above that MariaDB is a fork of MySQL and it is also touted as a ‘drop-in’ replacement for MySQL.