DEV Community

Cover image for Install MariaDB in the Ubuntu Terminal:
MuthuKumar
MuthuKumar

Posted on • Updated on

Install MariaDB in the Ubuntu Terminal:

To install MariaDB on Ubuntu using the terminal, you can follow these steps:

  1. Open a Terminal: Press Ctrl + Alt + T to open a terminal window.

  2. Update Package List: It's a good practice to update the package list before installing any new software. Run the following command:

   sudo apt update
Enter fullscreen mode Exit fullscreen mode
  1. Install MariaDB: Use the following command to install MariaDB:
   sudo apt install mariadb-server
Enter fullscreen mode Exit fullscreen mode
  1. Start and Enable the Service: After the installation is complete, start the MariaDB service and enable it to start on system boot:
   sudo systemctl start mariadb
   sudo systemctl enable mariadb
Enter fullscreen mode Exit fullscreen mode
  1. Secure Installation: MariaDB comes with a script to help secure your installation. Run the following command:
   sudo mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

This script will guide you through several steps to set a root password, remove anonymous users, disallow remote root login, and remove the test database.

  1. Access MariaDB: You can access the MariaDB command-line interface by typing:
   sudo mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

You will be prompted to enter the root password you set during the secure installation.

That's it! You have successfully installed and set up MariaDB on your Ubuntu system. You can now use the mysql command to interact with the MariaDB server from the terminal.

Try it Yourself:

Update Package List:

muthukumar@DevOpsEngineer:~$ sudo apt update
Hit:1 http://packages.microsoft.com/repos/code stable InRelease
Ign:2 https://pkg.jenkins.io/debian binary/ InRelease                          
Hit:3 https://pkg.jenkins.io/debian binary/ Release                            
Get:4 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]      
Hit:6 http://in.archive.ubuntu.com/ubuntu jammy InRelease                      
Hit:7 https://dl.google.com/linux/chrome/deb stable InRelease                  
Get:8 http://in.archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]     
Hit:9 https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy InRelease   
Get:10 http://security.ubuntu.com/ubuntu jammy-security/main amd64 DEP-11 Metadata [42.8 kB]
Get:11 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 DEP-11 Metadata [39.8 kB]
Hit:12 https://ppa.launchpadcontent.net/obsproject/obs-studio/ubuntu jammy InRelease
Hit:13 https://www.mediahuman.com/packages/ubuntu jammy InRelease
Get:14 http://in.archive.ubuntu.com/ubuntu jammy-backports InRelease [109 kB]  
Get:15 http://in.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [889 kB]
Hit:16 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy InRelease      
Get:17 http://in.archive.ubuntu.com/ubuntu jammy-updates/main i386 Packages [471 kB]
Get:18 http://in.archive.ubuntu.com/ubuntu jammy-updates/main amd64 DEP-11 Metadata [101 kB]
Get:19 http://in.archive.ubuntu.com/ubuntu jammy-updates/universe i386 Packages [647 kB]
Get:20 http://in.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [965 kB]
Get:21 http://in.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 DEP-11 Metadata [278 kB]
Get:22 http://in.archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 DEP-11 Metadata [940 B]
Get:23 http://in.archive.ubuntu.com/ubuntu jammy-backports/main amd64 DEP-11 Metadata [4,932 B]
Get:24 http://in.archive.ubuntu.com/ubuntu jammy-backports/universe amd64 DEP-11 Metadata [15.6 kB]
Hit:25 https://ppa.launchpadcontent.net/umang/indicator-stickynotes/ubuntu jammy InRelease
Hit:26 https://ppa.launchpadcontent.net/wireshark-dev/stable/ubuntu jammy InRelease
Fetched 3,792 kB in 5s (737 kB/s)                          
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
52 packages can be upgraded. Run 'apt list --upgradable' to see them.
W: https://www.mediahuman.com/packages/ubuntu/dists/jammy/InRelease: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.
Enter fullscreen mode Exit fullscreen mode

Install MariaDB:

muthukumar@DevOpsEngineer:~$ sudo apt install mariadb-server
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  galera-4 libcgi-fast-perl libcgi-pm-perl libconfig-inifiles-perl libdaxctl1
  libdbd-mysql-perl libdbi-perl libfcgi-bin libfcgi-perl libfcgi0ldbl
  libhtml-template-perl libmariadb3 libndctl6 libpmem1 libterm-readkey-perl
  mariadb-client-10.6 mariadb-client-core-10.6 mariadb-common
  mariadb-server-10.6 mariadb-server-core-10.6 socat
Suggested packages:
  libmldbm-perl libnet-daemon-perl libsql-statement-perl
  libipc-sharedcache-perl mailx mariadb-test
The following NEW packages will be installed:
  galera-4 libcgi-fast-perl libcgi-pm-perl libconfig-inifiles-perl libdaxctl1
  libdbd-mysql-perl libdbi-perl libfcgi-bin libfcgi-perl libfcgi0ldbl
  libhtml-template-perl libmariadb3 libndctl6 libpmem1 libterm-readkey-perl
  mariadb-client-10.6 mariadb-client-core-10.6 mariadb-common mariadb-server
  mariadb-server-10.6 mariadb-server-core-10.6 socat
0 upgraded, 22 newly installed, 0 to remove and 52 not upgraded.
Need to get 16.9 MB of archives.
After this operation, 156 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://in.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 mariadb-common all 1:10.6.12-0ubuntu0.22.04.1 [16.4 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu jammy/universe amd64 galera-4 amd64 26.4.9-1build1 [720 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu jammy/main amd64 libdbi-perl amd64 1.643-3build3 [741 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu jammy/main amd64 libconfig-inifiles-perl all 3.000003-1 [40.5 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 libmariadb3 amd64 1:10.6.12-0ubuntu0.22.04.1 [173 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 mariadb-client-core-10.6 amd64 1:10.6.12-0ubuntu0.22.04.1 [976 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 mariadb-client-10.6 amd64 1:10.6.12-0ubuntu0.22.04.1 [1,545 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu jammy/main amd64 libdaxctl1 amd64 72.1-1 [19.8 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu jammy/main amd64 libndctl6 amd64 72.1-1 [57.7 kB]
Get:10 http://in.archive.ubuntu.com/ubuntu jammy/main amd64 libpmem1 amd64 1.11.1-3build1 [81.4 kB]
Get:11 http://in.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 mariadb-server-core-10.6 amd64 1:10.6.12-0ubuntu0.22.04.1 [7,633 kB]
Get:12 http://in.archive.ubuntu.com/ubuntu jammy/main amd64 socat amd64 1.7.4.1-3ubuntu4 [349 kB]
Get:13 http://in.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 mariadb-server-10.6 amd64 1:10.6.12-0ubuntu0.22.04.1 [4,081 kB]
Get:14 http://in.archive.ubuntu.com/ubuntu jammy/main amd64 libcgi-pm-perl all 4.54-1 [188 kB]                                                       
Get:15 http://in.archive.ubuntu.com/ubuntu jammy/main amd64 libfcgi0ldbl amd64 2.4.2-2build2 [28.0 kB]                                               
Get:16 http://in.archive.ubuntu.com/ubuntu jammy/main amd64 libfcgi-perl amd64 0.82+ds-1build1 [22.8 kB]                                             
Get:17 http://in.archive.ubuntu.com/ubuntu jammy/main amd64 libcgi-fast-perl all 1:2.15-1 [10.5 kB]                                                  
Get:18 http://in.archive.ubuntu.com/ubuntu jammy/universe amd64 libdbd-mysql-perl amd64 4.050-5 [87.6 kB]                                            
Get:19 http://in.archive.ubuntu.com/ubuntu jammy/main amd64 libfcgi-bin amd64 2.4.2-2build2 [11.2 kB]                                                
Get:20 http://in.archive.ubuntu.com/ubuntu jammy/main amd64 libhtml-template-perl all 2.97-1.1 [59.1 kB]                                             
Get:21 http://in.archive.ubuntu.com/ubuntu jammy/main amd64 libterm-readkey-perl amd64 2.38-1build4 [25.2 kB]                                        
Get:22 http://in.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 mariadb-server all 1:10.6.12-0ubuntu0.22.04.1 [11.8 kB]                      
Fetched 16.9 MB in 8s (2,169 kB/s)                                                                                                                   
Preconfiguring packages ...
Selecting previously unselected package mariadb-common.
(Reading database ... 241552 files and directories currently installed.)
Preparing to unpack .../00-mariadb-common_1%3a10.6.12-0ubuntu0.22.04.1_all.deb ...
Unpacking mariadb-common (1:10.6.12-0ubuntu0.22.04.1) ...
Selecting previously unselected package galera-4.
Preparing to unpack .../01-galera-4_26.4.9-1build1_amd64.deb ...
Unpacking galera-4 (26.4.9-1build1) ...
Selecting previously unselected package libdbi-perl:amd64.
Preparing to unpack .../02-libdbi-perl_1.643-3build3_amd64.deb ...
Unpacking libdbi-perl:amd64 (1.643-3build3) ...
Selecting previously unselected package libconfig-inifiles-perl.
Preparing to unpack .../03-libconfig-inifiles-perl_3.000003-1_all.deb ...
Unpacking libconfig-inifiles-perl (3.000003-1) ...
Selecting previously unselected package libmariadb3:amd64.
Preparing to unpack .../04-libmariadb3_1%3a10.6.12-0ubuntu0.22.04.1_amd64.deb ...
Unpacking libmariadb3:amd64 (1:10.6.12-0ubuntu0.22.04.1) ...
Selecting previously unselected package mariadb-client-core-10.6.
Preparing to unpack .../05-mariadb-client-core-10.6_1%3a10.6.12-0ubuntu0.22.04.1_amd64.deb ...
Unpacking mariadb-client-core-10.6 (1:10.6.12-0ubuntu0.22.04.1) ...
Selecting previously unselected package mariadb-client-10.6.
Preparing to unpack .../06-mariadb-client-10.6_1%3a10.6.12-0ubuntu0.22.04.1_amd64.deb ...
Unpacking mariadb-client-10.6 (1:10.6.12-0ubuntu0.22.04.1) ...
Selecting previously unselected package libdaxctl1:amd64.
Preparing to unpack .../07-libdaxctl1_72.1-1_amd64.deb ...
Unpacking libdaxctl1:amd64 (72.1-1) ...
Selecting previously unselected package libndctl6:amd64.
Preparing to unpack .../08-libndctl6_72.1-1_amd64.deb ...
Unpacking libndctl6:amd64 (72.1-1) ...
Selecting previously unselected package libpmem1:amd64.
Preparing to unpack .../09-libpmem1_1.11.1-3build1_amd64.deb ...
Unpacking libpmem1:amd64 (1.11.1-3build1) ...
Selecting previously unselected package mariadb-server-core-10.6.
Preparing to unpack .../10-mariadb-server-core-10.6_1%3a10.6.12-0ubuntu0.22.04.1_amd64.deb ...
Unpacking mariadb-server-core-10.6 (1:10.6.12-0ubuntu0.22.04.1) ...
Selecting previously unselected package socat.
Preparing to unpack .../11-socat_1.7.4.1-3ubuntu4_amd64.deb ...
Unpacking socat (1.7.4.1-3ubuntu4) ...
Setting up mariadb-common (1:10.6.12-0ubuntu0.22.04.1) ...
update-alternatives: using /etc/mysql/mariadb.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Selecting previously unselected package mariadb-server-10.6.
(Reading database ... 241989 files and directories currently installed.)
Preparing to unpack .../0-mariadb-server-10.6_1%3a10.6.12-0ubuntu0.22.04.1_amd64.deb ...
Unpacking mariadb-server-10.6 (1:10.6.12-0ubuntu0.22.04.1) ...
Selecting previously unselected package libcgi-pm-perl.
Preparing to unpack .../1-libcgi-pm-perl_4.54-1_all.deb ...
Unpacking libcgi-pm-perl (4.54-1) ...
Selecting previously unselected package libfcgi0ldbl:amd64.
Preparing to unpack .../2-libfcgi0ldbl_2.4.2-2build2_amd64.deb ...
Unpacking libfcgi0ldbl:amd64 (2.4.2-2build2) ...
Selecting previously unselected package libfcgi-perl:amd64.
Preparing to unpack .../3-libfcgi-perl_0.82+ds-1build1_amd64.deb ...
Unpacking libfcgi-perl:amd64 (0.82+ds-1build1) ...
Selecting previously unselected package libcgi-fast-perl.
Preparing to unpack .../4-libcgi-fast-perl_1%3a2.15-1_all.deb ...
Unpacking libcgi-fast-perl (1:2.15-1) ...
Selecting previously unselected package libdbd-mysql-perl:amd64.
Preparing to unpack .../5-libdbd-mysql-perl_4.050-5_amd64.deb ...
Unpacking libdbd-mysql-perl:amd64 (4.050-5) ...
Selecting previously unselected package libfcgi-bin.
Preparing to unpack .../6-libfcgi-bin_2.4.2-2build2_amd64.deb ...
Unpacking libfcgi-bin (2.4.2-2build2) ...
Selecting previously unselected package libhtml-template-perl.
Preparing to unpack .../7-libhtml-template-perl_2.97-1.1_all.deb ...
Unpacking libhtml-template-perl (2.97-1.1) ...
Selecting previously unselected package libterm-readkey-perl.
Preparing to unpack .../8-libterm-readkey-perl_2.38-1build4_amd64.deb ...
Unpacking libterm-readkey-perl (2.38-1build4) ...
Selecting previously unselected package mariadb-server.
Preparing to unpack .../9-mariadb-server_1%3a10.6.12-0ubuntu0.22.04.1_all.deb ...
Unpacking mariadb-server (1:10.6.12-0ubuntu0.22.04.1) ...
Setting up libconfig-inifiles-perl (3.000003-1) ...
Setting up galera-4 (26.4.9-1build1) ...
Setting up libfcgi0ldbl:amd64 (2.4.2-2build2) ...
Setting up libcgi-pm-perl (4.54-1) ...
Setting up libfcgi-bin (2.4.2-2build2) ...
Setting up libhtml-template-perl (2.97-1.1) ...
Setting up socat (1.7.4.1-3ubuntu4) ...
Setting up libmariadb3:amd64 (1:10.6.12-0ubuntu0.22.04.1) ...
Setting up libdaxctl1:amd64 (72.1-1) ...
Setting up libndctl6:amd64 (72.1-1) ...
Setting up libfcgi-perl:amd64 (0.82+ds-1build1) ...
Setting up libterm-readkey-perl (2.38-1build4) ...
Setting up libpmem1:amd64 (1.11.1-3build1) ...
Setting up libdbi-perl:amd64 (1.643-3build3) ...
Setting up mariadb-server-core-10.6 (1:10.6.12-0ubuntu0.22.04.1) ...
Setting up libcgi-fast-perl (1:2.15-1) ...
Setting up mariadb-client-core-10.6 (1:10.6.12-0ubuntu0.22.04.1) ...
Setting up libdbd-mysql-perl:amd64 (4.050-5) ...
Setting up mariadb-client-10.6 (1:10.6.12-0ubuntu0.22.04.1) ...
Setting up mariadb-server-10.6 (1:10.6.12-0ubuntu0.22.04.1) ...
Failed to stop mariadb.service: Unit mariadb.service not loaded.
invoke-rc.d: initscript mariadb, action "stop" failed.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /lib/systemd/system/mariadb.service.
Setting up mariadb-server (1:10.6.12-0ubuntu0.22.04.1) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...

Enter fullscreen mode Exit fullscreen mode

Start and Enable the Service:

muthukumar@DevOpsEngineer:~$ sudo systemctl start mariadb
muthukumar@DevOpsEngineer:~$ sudo systemctl enable mariadb
Synchronizing state of mariadb.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable mariadb

Enter fullscreen mode Exit fullscreen mode
muthukumar@DevOpsEngineer:~$ sudo systemctl status mariadb
● mariadb.service - MariaDB 10.6.12 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2023-08-16 14:41:53 IST; 1min 19s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 20173 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 10 (limit: 9110)
     Memory: 61.2M
        CPU: 450ms
     CGroup: /system.slice/mariadb.service
             └─20173 /usr/sbin/mariadbd

Aug 16 14:41:53 DevOpsEngineer mariadbd[20173]: 2023-08-16 14:41:53 0 [Note] /usr/sbin/mariadbd: ready for connections.
Aug 16 14:41:53 DevOpsEngineer mariadbd[20173]: Version: '10.6.12-MariaDB-0ubuntu0.22.04.1'  socket: '/run/mysqld/mysqld.sock'  port: 3306  Ubuntu 22>
Aug 16 14:41:53 DevOpsEngineer systemd[1]: Started MariaDB 10.6.12 database server.
Aug 16 14:41:53 DevOpsEngineer /etc/mysql/debian-start[20191]: Upgrading MySQL tables if necessary.
Aug 16 14:41:53 DevOpsEngineer /etc/mysql/debian-start[20194]: Looking for 'mariadb' as: /usr/bin/mariadb
Aug 16 14:41:53 DevOpsEngineer /etc/mysql/debian-start[20194]: Looking for 'mariadb-check' as: /usr/bin/mariadb-check
Aug 16 14:41:53 DevOpsEngineer /etc/mysql/debian-start[20194]: This installation of MariaDB is already upgraded to 10.6.12-MariaDB.
Aug 16 14:41:53 DevOpsEngineer /etc/mysql/debian-start[20194]: There is no need to run mysql_upgrade again for 10.6.12-MariaDB.
Aug 16 14:41:53 DevOpsEngineer /etc/mysql/debian-start[20194]: You can use --force if you still want to run mysql_upgrade
Aug 16 14:41:53 DevOpsEngineer /etc/mysql/debian-start[20202]: Checking for insecure root accounts.

muthukumar@DevOpsEngineer:~$ 

Enter fullscreen mode Exit fullscreen mode

Secure Installation:

step 1:

muthukumar@DevOpsEngineer:~$ sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 

Enter fullscreen mode Exit fullscreen mode

step 2:

Enter the PASSWORD (example)123456

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] 

Enter fullscreen mode Exit fullscreen mode
Switch to unix_socket authentication [Y/n] Y
Enabled successfully!
Reloading privilege tables..
 ... Success!
Enter fullscreen mode Exit fullscreen mode
You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] Y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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.

Enter fullscreen mode Exit fullscreen mode
Remove anonymous users? [Y/n] Y
 ... Success!

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? [Y/n] Y
 ... Success!

By default, MariaDB 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? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Enter fullscreen mode Exit fullscreen mode

Access MariaDB:

muthukumar@DevOpsEngineer:~$ sudo mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 42
Server version: 10.6.12-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 


Enter fullscreen mode Exit fullscreen mode

Top comments (0)