MySQL and MariaDB are popular open-source relational database management systems. As such, they are made available on most platforms, including Ubuntu Linux, and can be installed easily.
The installation process for MySQL or MariaDB on Ubuntu is straightforward, thanks to the availability of both systems in the default Ubuntu package manager repository. All you need to do is use the apt command in the terminal to download and install the desired database management system along with its dependencies.
Once the installation is complete, you can use a hardening script to secure the installation further. While this step is not mandatory in a development environment, it is highly recommended for production systems. The hardening script will help you lock down the installation, making it more secure and less vulnerable to attacks. By following this guide, you'll have a fully functional and secure installation of either MySQL or MariaDB on your Ubuntu Linux system.
$ sudo apt update [sudo] password for user: Hit:1 http://ports.ubuntu.com/ubuntu-ports kinetic-security InRelease Hit:2 http://us.ports.ubuntu.com/ubuntu-ports kinetic InRelease Hit:3 http://us.ports.ubuntu.com/ubuntu-ports kinetic-updates InRelease Hit:4 http://us.ports.ubuntu.com/ubuntu-ports kinetic-backports InRelease Reading package lists... Done Building dependency tree... Done Reading state information... Done 7 packages can be upgraded. Run 'apt list --upgradable' to see them.
$ sudo apt install --assume-yes mysql-server Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: libaio1 libcgi-fast-perl libcgi-pm-perl libevent-core-2.1-7a libevent-pthreads-2.1-7 libfcgi-bin libfcgi-perl libfcgi0ldbl libhtml-template-perl libmecab2 libprotobuf-lite23 mecab-ipadic mecab-ipadic-utf8 mecab-utils mysql-client-8.0 mysql-client-core-8.0 mysql-common mysql-server-8.0 mysql-server-core-8.0 Suggested packages: libipc-sharedcache-perl mailx tinyca The following NEW packages will be installed: libaio1 libcgi-fast-perl libcgi-pm-perl libevent-core-2.1-7a libevent-pthreads-2.1-7 libfcgi-bin libfcgi-perl libfcgi0ldbl libhtml-template-perl libmecab2 libprotobuf-lite23 mecab-ipadic mecab-ipadic-utf8 mecab-utils mysql-client-8.0 mysql-client-core-8.0 mysql-common mysql-server mysql-server-8.0 mysql-server-core-8.0 0 upgraded, 20 newly installed, 0 to remove and 10 not upgraded. Need to get 28.0 MB of archives. After this operation, 239 MB of additional disk space will be used. ##### snipped
Replace package name with mariadb-server to install MariaDB.
Installed service is automatically started, and set to start during system boot.
Related: How to manage MySQL or MariaDB service in Linux
Skipping this step will cause error in the mysql_secure_installation step below.
$ sudo mysql_secure_installation Securing the MySQL server deployment. Enter password for user root:
VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component? Press y|Y for Yes, any other key for No: y
There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
Using existing password for root. Estimated strength of the password: 50 Change the password for root ? ((Press y|Y for Yes, any other key for No) : no
Use the same password as set prior to running mysql_secure_installation.
By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 27 Server version: 8.0.32-0ubuntu0.22.10.2 (Ubuntu) Copyright (c) 2000, 2023, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
Comment anonymously. Login not required.