How to install MySQL on Ubuntu

In this article, we will see how to install MySQL on Ubuntu.

On Ubuntu 20.04(or later version), we can install MySQL using the APT package repository. Present version of MySQL available in the default Ubuntu repository is version 8.0.27.

Step 1: How to install MySQL on Ubuntu

1. Update repository by running below command on Ubuntu(Any Debian OS).

sudo apt update

2. Install MySQL Server on Ubuntu by using below command.

sudo apt install mysql-server

If it asks to “Do you want to continue?“, then type Y and press enter.

[sudo] password for r2schools: Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libcgi-fast-perl libcgi-pm-perl libevent-core-2.1-7 libevent-pthreads-2.1-7 libfcgi-perl libhtml-template-perl libmecab2 mecab-ipadic mecab-ipadic-utf8 mecab-utils mysql-client-8.0 mysql-client-core-8.0 mysql-server-8.0 mysql-server-core-8.0 Suggested packages: libipc-sharedcache-perl mailx tinyca The following NEW packages will be installed: libcgi-fast-perl libcgi-pm-perl libevent-core-2.1-7 libevent-pthreads-2.1-7 libfcgi-perl libhtml-template-perl libmecab2 mecab-ipadic mecab-ipadic-utf8 mecab-utils mysql-client-8.0 mysql-client-core-8.0 mysql-server mysql-server-8.0 mysql-server-core-8.0 0 upgraded, 15 newly installed, 0 to remove and 147 not upgraded. Need to get 31.5 MB of archives. After this operation, 262 MB of additional disk space will be used. Do you want to continue? [Y/n] Y Get:14 http://us.archive.ubuntu.com/ubuntu focal/main amd64 mecab-ipadic-utf8 all 2.7.0-20070801+main-2.1 [4,380 B] Get:15 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 mysql-server all 8.0.27-0ubuntu0.20.04.1 [9,548 B] Fetched 31.5 MB in 36s (864 kB/s) Preconfiguring packages ... Selecting previously unselected package mysql-client-core-8.0. (Reading database ... 196041 files and directories currently installed.) Preparing to unpack .../00-mysql-client-core-8.0_8.0.27-0ubuntu0.20.04.1_amd64.deb ... Unpacking mysql-client-core-8.0 (8.0.27-0ubuntu0.20.04.1) ... Selecting previously unselected package mysql-client-8.0. Preparing to unpack .../01-mysql-client-8.0_8.0.27-0ubuntu0.20.04.1_amd64.deb ... Unpacking mysql-client-8.0 (8.0.27-0ubuntu0.20.04.1) ... Selecting previously unselected package mysql-server-core-8.0. Preparing to unpack .../05-mysql-server-core-8.0_8.0.27-0ubuntu0.20.04.1_amd64.deb ... Unpacking mysql-server-core-8.0 (8.0.27-0ubuntu0.20.04.1) ... Selecting previously unselected package mysql-server-8.0. Preparing to unpack .../06-mysql-server-8.0_8.0.27-0ubuntu0.20.04.1_amd64.deb ... Unpacking mysql-server-8.0 (8.0.27-0ubuntu0.20.04.1) ... Selecting previously unselected package libcgi-pm-perl. Unpacking mysql-server (8.0.27-0ubuntu0.20.04.1) ... Setting up libmecab2:amd64 (0.996-10build1) ... Setting up mysql-client-core-8.0 (8.0.27-0ubuntu0.20.04.1) ... Setting up libcgi-pm-perl (4.46-1) ... Setting up libhtml-template-perl (2.97-1) ... Setting up mecab-utils (0.996-10build1) ... Setting up libevent-core-2.1-7:amd64 (2.1.11-stable-1) ... Setting up mysql-client-8.0 (8.0.27-0ubuntu0.20.04.1) ... Setting up libfcgi-perl (0.79-1) ... Setting up libevent-pthreads-2.1-7:amd64 (2.1.11-stable-1) ... Setting up mecab-ipadic (2.7.0-20070801+main-2.1) ... Compiling IPA dictionary for Mecab. This takes long time... reading /usr/share/mecab/dic/ipadic/unk.def ... 40 emitting double-array: 100% |###########################################| Setting up mysql-server-core-8.0 (8.0.27-0ubuntu0.20.04.1) ... Setting up mecab-ipadic-utf8 (2.7.0-20070801+main-2.1) ... Compiling IPA dictionary for Mecab. This takes long time... Setting up mysql-server-8.0 (8.0.27-0ubuntu0.20.04.1) ... update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode Renaming removed key_buffer and myisam-recover options (if present) mysqld will log errors to /var/log/mysql/error.log mysqld is running as pid 34629 Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service. Setting up mysql-server (8.0.27-0ubuntu0.20.04.1) ... Processing triggers for systemd (245.4-4ubuntu3.11) ... Processing triggers for man-db (2.9.1-1) ... Processing triggers for libc-bin (2.31-0ubuntu9.2) ...

Step 2: Configure MySQL on Ubuntu after installation.

For fresh installations of MySQL, we have to run the DBMS’s included security script. This script changes some of the less secure default options for things like remote root logins and sample users.

1. Run the script mysql_secure_installation with sudo:

sudo mysql_secure_installation

How to install MySQL on Ubuntu

Type Y and press enter.

2. Select the password validation level from 0 , 1 or 2. Type number and press enter.

3. Enter the password for user root and press enter.

4. If we want to remove anonymous users, press Y otherwise N. I dont want to add anonymous users. So, type Y and press enter.

5. If we dont want to allow root user remotely, then type Y otherwise N. I want to allow root login remotely. So, gave ‘N’.

6. If we want to remove test database, then type Y otherwise. I dont want to remove. So, typed N and press enter.

7. Reload privileges Y and press enter.

Step 3: Connect to MySQL on Ubuntu after installation.

1. Run the below command to connect to MySQL server on Ubuntu.

sudo mysql -u root -p

or

1. Connect to MySQL as sudo mysql, otherwise we will receive following error.

ERROR 1045 (28000): Access denied for user 'root@localhost'@'localhost' (using password: YES)

So, run the command sudo mysql:

2. To check the status of MySQL server on Ubuntu, run the below command.

sudo systemctl status mysql

So in this article, we have seen how to install MySQL on Ubuntu and connect to MySQL on Ubuntu.