How to Install MariaDB Database Server in Linux Ubuntu 18.04

Posted on

This is an article for describing how to install MariaDB Server in Linux Ubuntu 18.0.4. There are several steps for installing MariaDB Server in that operating system platform. The following are those steps :

1. Search the package with the name of ‘mariadb-server’ using the software management tool ‘apt’ since it is in Ubuntu distribution. Just execute the following command :

root@hostname:~# apt search mariadb-server
Sorting... Done
Full Text Search... Done
mariadb-server/bionic-updates,bionic-security 1:10.1.40-0ubuntu0.18.04.1 all
  MariaDB database server (metapackage depending on the latest version)

mariadb-server-10.1/bionic-updates,bionic-security 1:10.1.40-0ubuntu0.18.04.1 amd64
  MariaDB database server binaries

mariadb-server-core-10.1/bionic-updates,bionic-security 1:10.1.40-0ubuntu0.18.04.1 amd64
  MariaDB database core server files

root@hostname:~#

2. Fortunately, there is a package with the name of mariadb-server in the above output. Check if it has already exist using the command ‘apt list –installed | grep mariadb-server’. Just execute the following command :

root@hostname:~## apt list --installed | grep mariadb-server

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

mariadb-server-10.1/bionic-updates,bionic-security,now 1:10.1.40-0ubuntu0.18.04.1 amd64 [installed]
mariadb-server-core-10.1/bionic-updates,bionic-security,now 1:10.1.40-0ubuntu0.18.04.1 amd64 [installed,automatic]
root@hostname:~## 

3. Apparently, if the output is similar with the above output, it means ‘mariadb-server’ is already installed before. If it is not, just install it using the command ‘apt-get -y install mariadb-server-10.1’. There is a reason why in the installation the ‘mariadb-server-10.1’ is chosen instead of ‘mariadb-server’. Read the description for the reason in the article with the title of ‘How to Solve Error on Installing MariaDB Database Server in Linux Ubuntu 18.04’ in this link. So, execute the command as follows :

root@hostname:~# apt-get -y install mariadb-server-10.1
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libevent-core-2.1-6 libpython-all-dev python-all python-all-dev python-asn1crypto python-cffi-backend python-cryptography python-dbus python-enum34 python-gi python-idna python-ipaddress
  python-keyring python-keyrings.alt python-secretstorage python-wheel python-xdg python3-wheel
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  galera-3 libconfig-inifiles-perl libdbd-mysql-perl libjemalloc1 libterm-readkey-perl mariadb-client-10.1 mariadb-client-core-10.1 mariadb-common mariadb-server-core-10.1
Suggested packages:
  mailx mariadb-test tinyca
The following packages will be REMOVED:
  mysql-client-5.7 mysql-client-core-5.7 mysql-server mysql-server-5.7 mysql-server-core-5.7
The following NEW packages will be installed:
  galera-3 libconfig-inifiles-perl libdbd-mysql-perl libjemalloc1 libterm-readkey-perl mariadb-client-10.1 mariadb-client-core-10.1 mariadb-common mariadb-server-10.1 mariadb-server-core-10.1
0 upgraded, 10 newly installed, 5 to remove and 2 not upgraded.
Need to get 21.6 MB of archives.
After this operation, 14.7 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 mariadb-common all 1:10.1.40-0ubuntu0.18.04.1 [16.2 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic/universe amd64 galera-3 amd64 25.3.20-1 [947 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic/main amd64 libconfig-inifiles-perl all 2.94-1 [40.4 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 mariadb-client-core-10.1 amd64 1:10.1.40-0ubuntu0.18.04.1 [4,745 kB]
Get:5 http://archive.ubuntu.com/ubuntu bionic/universe amd64 libjemalloc1 amd64 3.6.0-11 [82.4 kB]                                                                                                         
Get:6 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 mariadb-client-10.1 amd64 1:10.1.40-0ubuntu0.18.04.1 [5,646 kB]                                                                       
Get:7 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 mariadb-server-core-10.1 amd64 1:10.1.40-0ubuntu0.18.04.1 [4,945 kB]                                                                  
Get:8 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 mariadb-server-10.1 amd64 1:10.1.40-0ubuntu0.18.04.1 [5,094 kB]                                                                       
Get:9 http://archive.ubuntu.com/ubuntu bionic/universe amd64 libdbd-mysql-perl amd64 4.046-1 [82.0 kB]                                                                                                     
Get:10 http://archive.ubuntu.com/ubuntu bionic/universe amd64 libterm-readkey-perl amd64 2.37-1build1 [24.4 kB]                                                                                            
Fetched 21.6 MB in 24s (905 kB/s)                                                                                                                                                                          
Preconfiguring packages ...
(Reading database ... 340539 files and directories currently installed.)
Removing mysql-server (5.7.27-0ubuntu0.18.04.1) ...
Removing mysql-server-5.7 (5.7.27-0ubuntu0.18.04.1) ...
update-alternatives: using /etc/mysql/my.cnf.fallback to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Removing mysql-client-5.7 (5.7.27-0ubuntu0.18.04.1) ...
Removing mysql-client-core-5.7 (5.7.27-0ubuntu0.18.04.1) ...
Removing mysql-server-core-5.7 (5.7.27-0ubuntu0.18.04.1) ...
Selecting previously unselected package mariadb-common.
(Reading database ... 340322 files and directories currently installed.)
Preparing to unpack .../0-mariadb-common_1%3a10.1.40-0ubuntu0.18.04.1_all.deb ...
Unpacking mariadb-common (1:10.1.40-0ubuntu0.18.04.1) ...
Selecting previously unselected package galera-3.
Preparing to unpack .../1-galera-3_25.3.20-1_amd64.deb ...
Unpacking galera-3 (25.3.20-1) ...
Selecting previously unselected package libconfig-inifiles-perl.
Preparing to unpack .../2-libconfig-inifiles-perl_2.94-1_all.deb ...
Unpacking libconfig-inifiles-perl (2.94-1) ...
Selecting previously unselected package mariadb-client-core-10.1.
Preparing to unpack .../3-mariadb-client-core-10.1_1%3a10.1.40-0ubuntu0.18.04.1_amd64.deb ...
Unpacking mariadb-client-core-10.1 (1:10.1.40-0ubuntu0.18.04.1) ...
Selecting previously unselected package libjemalloc1.
Preparing to unpack .../4-libjemalloc1_3.6.0-11_amd64.deb ...
Unpacking libjemalloc1 (3.6.0-11) ...
Selecting previously unselected package mariadb-client-10.1.
Preparing to unpack .../5-mariadb-client-10.1_1%3a10.1.40-0ubuntu0.18.04.1_amd64.deb ...
Unpacking mariadb-client-10.1 (1:10.1.40-0ubuntu0.18.04.1) ...
Selecting previously unselected package mariadb-server-core-10.1.
Preparing to unpack .../6-mariadb-server-core-10.1_1%3a10.1.40-0ubuntu0.18.04.1_amd64.deb ...
Unpacking mariadb-server-core-10.1 (1:10.1.40-0ubuntu0.18.04.1) ...
Setting up mariadb-common (1:10.1.40-0ubuntu0.18.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.1.
(Reading database ... 340488 files and directories currently installed.)
Preparing to unpack .../mariadb-server-10.1_1%3a10.1.40-0ubuntu0.18.04.1_amd64.deb ...
/var/lib/mysql: found previous version 5.7
The file /var/lib/mysql/debian-5.7.flag indicates a
version that cannot automatically be upgraded. Therefore the
previous data directory will be renamed to /var/lib/mysql-5.7 and
a new data directory will be initialized at /var/lib/mysql.
Please manually export/import your data (e.g. with mysqldump) if needed.
Unpacking mariadb-server-10.1 (1:10.1.40-0ubuntu0.18.04.1) ...
Selecting previously unselected package libdbd-mysql-perl.
Preparing to unpack .../libdbd-mysql-perl_4.046-1_amd64.deb ...
Unpacking libdbd-mysql-perl (4.046-1) ...
Selecting previously unselected package libterm-readkey-perl.
Preparing to unpack .../libterm-readkey-perl_2.37-1build1_amd64.deb ...
Unpacking libterm-readkey-perl (2.37-1build1) ...
Setting up libconfig-inifiles-perl (2.94-1) ...
Processing triggers for ureadahead (0.100.0-21) ...
Setting up libjemalloc1 (3.6.0-11) ...
Setting up mariadb-client-core-10.1 (1:10.1.40-0ubuntu0.18.04.1) ...
Setting up libterm-readkey-perl (2.37-1build1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Setting up galera-3 (25.3.20-1) ...
Processing triggers for systemd (237-3ubuntu10.24) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up libdbd-mysql-perl (4.046-1) ...
Setting up mariadb-client-10.1 (1:10.1.40-0ubuntu0.18.04.1) ...
Setting up mariadb-server-core-10.1 (1:10.1.40-0ubuntu0.18.04.1) ...
Setting up mariadb-server-10.1 (1:10.1.40-0ubuntu0.18.04.1) ...
Installing new version of config file /etc/apparmor.d/usr.sbin.mysqld ...
Installing new version of config file /etc/init.d/mysql ...
Installing new version of config file /etc/logrotate.d/mysql-server ...
Installing new version of config file /etc/mysql/debian-start ...
Created symlink /etc/systemd/system/mysql.service → /lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /lib/systemd/system/mariadb.service.
Job for mariadb.service failed because a timeout was exceeded.
See "systemctl status mariadb.service" and "journalctl -xe" for details.
Processing triggers for systemd (237-3ubuntu10.24) ...
Processing triggers for ureadahead (0.100.0-21) ...
root@hostname:~# 

4. After successfully install MariaDB Database Server as in the above output, start the service by executing the following command :

root@hostname:~# systemctl start mariadb
root@hostname:~#

5. Next step, continue it by executing the following command to check the MariaDB Database Server’s service status :

root@hostname:~# systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2019-07-25 08:44:08; 5s ago
  Process: 784 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 701 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 783 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─783 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─946 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --sock...

Jul 25 08:44:04 hostname systemd[1]: Starting MariaDB database server...
Jul 25 08:44:04 hostname mariadb-prepare-db-dir[701]: Initializing MariaDB database
Jul 25 08:44:05 hostname mariadb-prepare-db-dir[701]: 190725  8:44:05 [Note] /usr/libexec/mysqld (mysqld 5.5.60-MariaDB) starting as process 768 ...
Jul 25 08:44:06 hostname mariadb-prepare-db-dir[701]: 190725  8:44:06 [Note] /usr/libexec/mysqld (mysqld 5.5.60-MariaDB) starting as process 777 ...
Jul 25 08:44:06 hostname mariadb-prepare-db-dir[701]: PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
Jul 25 08:44:06 hostname mariadb-prepare-db-dir[701]: To do so, start the server, then issue the following commands:
Jul 25 08:44:06 hostname mysqld_safe[783]: 190725 08:44:06 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
Jul 25 08:44:06 hostname mysqld_safe[783]: 190725 08:44:06 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Jul 25 08:44:08 hostname systemd[1]: Started MariaDB database server.
root@hostname:~#

6. Apparently, the service is running well. In order to persist the service whenever the server is restarted and it will automatically start during the boot process, just execute the following command :

root@hostname:~# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

7. Continue on the following step to secure the already installed MariaDB Database Server :

root@hostname:~# 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
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

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

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

Set 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.

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!
root@hostname:~#

8. Finally, after successfully executing the above command with the specific output above, just execute the following command to connect to MariaDB Database Server using the ‘root’ account with the password initialized above.

root@hostname:~# mysql -uroot -p 
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.60-MariaDB MariaDB Server

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)]> 

 

Leave a Reply