You are on page 1of 24

13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

Index

1. First steps .............................................................................................................. 2

2. Installation of MariaDB using the package manager .............................................. 2

2.1. Verification of Running Processes.................................................................... 3

2.2. Ensure Installation Security ............................................................................. 3

1.3. Uninstall MariaDB using package manager...................................................... 4

3. Installation of MariaDB using Mirror Server ........................................................... 5

4. Checks ................................................................................................................... 5

4.1. Check the installation ...................................................................................... 6

5. Connecting to MariaDB ......................................................................................... 8

5.1. Connecting from a remote client ..................................................................... 9

6. MariaDB Logs ...................................................................................................... 10

6.1. Error log ........................................................................................................ 11

6.2. General query log ......................................................................................... 13

6.3. Slow query log .............................................................................................. 14

7. Installation of MariaDB in a LAMP environment .................................................. 16

7.1. Installation of Apache ................................................................................... 16

7.2. Installation of PHP ......................................................................................... 20

7.3. Installation of PhpMyAdmin .......................................................................... 22


13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

1. First steps
First, we need to select the user and root for Debian; in my case, I have chosen:

User: asir2 Password: asir2

User: root Password: root

We can add a shortcut to open the terminal for convenience:

Look for "Settings" in the applications menu, then "Keyboard Shortcuts" and "Add
new": Name: Command Console Command: gnome-terminal Shortcut: Ctrl + Alt + t

The next step would be to update the repositories using the command:

apt-get update && apt-get install -y wget

To check the Debian version we are using: cat /etc/*release

To update the operating system: apt upgrade -y

To grant sudo access to a user, you can add them to the sudoers group. This is defined
in /etc/sudoers. Although there is also a command to do it directly without editing the
file.

To install some necessary utilities, we use:

apt-get install software-properties-common dirmngr apt-get install curl

To display the available repositories for MariaDB, use: apt search mariadb

2. Installation of MariaDB using the package manager


The required package is mariadb-server, and you can install it by running:

sudo apt install -y mariadb-server

Now, you can check the status of the service by executing:

systemctl status mariadb


13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

You can also enable it using: systemctl enable mariadb

And restart it with: systemctl restart mariadb

Or stop it with: systemctl stop mariadb

2.1. Verification of Running Processes

1. Mariadb: This program launches the MySQL server. It is the DBMS (Database
Management System) software. It receives client queries and executes them,
returning the appropriate results.
2. Mysql: This process is the client application. Queries to be sent to the DBMS are
written in the client.
3. Mysql_safe: This script starts the database manager (mysqld). If the MySQL
server fails, mysql_safe restarts it.

2.2. Ensure Installation Security


After installation, any user can connect to the database with full administrator
privileges. The database's administrator user is the root user, which should not be
confused with the root user of the operating system.

It is advisable, therefore, to ensure that all potential issues are addressed. For this
purpose, MySQL, and alternatively MariaDB, includes a script written in Perl that takes
care of this: mysql_secure_installation.

The security installation script will prompt us with some questions:

1. Enter the password for the MariaDB root. By default, it is empty, so we press
enter.
13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

2. It asks if we want to use unix_socket authentication. This is a way to access


without a password but with a different type of security. We say NO.
[Reference: https://mariadb.com/kb/en/authentication-plugin-unix-socket/]

3. It then asks us to change the MariaDB root password. We say yes and set the
new password: rootmariadb.

4. We say yes to the remaining options (although in practice, we comment on and


explain them).

1.3. Uninstall MariaDB using package manager


To perform a proper removal of the installation and ensure no remnants are left, we
need to delete all installed packages and the created folders and users. For this
purpose, we use dpkg. Dpkg is the low-level package management tool for Debian
systems. It is used to install, remove, and build Debian packages (.deb). The most well-
known front-ends, aptitude and apt-get, are user-friendly interfaces for the dpkg tool.

To remove everything related, you can use the following commands:

root@debian:~# /etc/init.d/mysql stop # Stop the service if it is active.

root@debian:~# apt-get remove --purge mariadb* # Remove mariadb.

root@debian:~# apt-get autoremove # Remove automatically installed packages


(dependencies).

root@debian:~# apt-get autoclean # Clean the local repository, cache, and


unnecessary versioned packages.

To delete folders:

root@debian:~# rm -rf /etc/mysql root@debian:~# rm -rf /var/lib/mysql*


13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

At this point, the machine is completely clean.

3. Installation of MariaDB using Mirror Server


The only downside is that they may experience a delay of a few days to a week in any
major release updates.

1. Go to the MariaDB downloads.

2. Choose the MariaDB version.

3. Select the Mirror location from where we will download the files.

4. In the text box below, it displays the commands to execute for the download.

5. You can use copy-paste directly into the console you are working with on
Debian.

4. Checks
We can establish a connection to MariaDB using: mariadb -u root -p

Check the default databases with: SHOW DATABASES;

Connect to one of them with: USE database_name;

Retrieve its tables with: SHOW TABLES;


13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

4.1. Check the installation


Base installation directory: /usr/bin

Service directory: /lib/systemd/system


13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

Data directory: /var/lib/mysql

 When MariaDB installation is complete, a new service is created:


mysqld.service, which is running and enabled for automatic startup.
 mysqld is the one that launches the MySQL server daemon process,
meaning the server application responsible for organizing and storing
data on persistent devices. It receives queries from the client, executes
them, and performs other related tasks.
 mysql is the client application's service. In the client, queries are written
and sent to the manager.

Configuration directory: /etc/mysql


13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

5. Connecting to MariaDB
To connect locally, use the command: mariadb -u username -p (-u indicates the
username, and -p is for the password).

To specify the host, use -h hostname (for example, -h localhost).

If the installation is done correctly, it shouldn't allow entry without the MariaDB
password.

To create users, use:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

User information is stored in the 'user' table of the 'mysql' database. You can check
them with:

SELECT user, host FROM mysql.user;

You can verify that when you connect with a new user, not all databases appear as they
do when logging in with the DBMS root.

Regarding the host, there are several options:


13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

5.1. Connecting from a remote client


Firstly, we need to install the MariaDB client on another Debian machine, and let's
name it 'cliente' for distinction by editing the /etc/hostname and /etc/hosts files.
13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

And restart the machine. Now we can install the client using:

apt-get install mariadb-client

On the server, create a user with which to connect, setting the host to '%'. After
creating it, execute FLUSH PRIVILEGES to reload the privileges.

Next, configure the mariadb.conf.d file to allow remote access:

And restart MariaDB.

From the client, we can use: mariadb -u username -h host_ip -p

to connect. For example:

6. MariaDB Logs
The most important log files in MySQL are as follows:

 Error log: records abnormal events that occur between the server and the
database, issues during server startup, execution, or shutdown.
13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

 General query log: logs connections with clients and the queries requested by
each one. It's a log that grows rapidly.

 Binary logs: records database modifications such as table creation, schema


modification, insertion of new values, or table queries. These logs are used for
backups and database recovery.

 Slow query log: records queries that exceed the estimated execution time.

6.1. Error log

1. The first thing we need to do is specify the path where the log file will be
stored. Initially, if we access MariaDB, we can check that the 'log_error' variable does
not yet have a defined value.

2. Edit the file /etc/mysql/mariadb.conf.d/50-server.cnf and remove the #


symbol from the corresponding line. We will leave the default path.

3. You can write these errors to a directory for MariaDB, for example:
/var/log/mariadb/error.log. Remember to create the directory.
13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

After restarting the server, we can observe the content:

Now let's provoke an error that will be reflected in the log file we just created. Using
the ss command, display UDP and TCP connections along with the process ID. Check
which port MariaDB is listening on (3306).

Change the listening port of MySQL in the file /etc/mysql/mariadb.conf.d/50-


server.cnf.

Restart mariaDB.
13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

In the log file:

To continue, change the port back (delete the line from the 50-server.cnf file) and
restart MariaDB..

6.2. General query log


We access MariaDB as the root user and check the status of the variables configuring
this log file:

 general_log: Indicates whether logging of this type of errors is enabled


or not.
 general_log_file: Indicates the path where the log file is stored.

Edit the 50-server.cnf file to configure both variables. Remove the pound sign. Leave
the default path and restart MariaDB. Every Data Control Language (DCL) operation will
be stored in the general_log_file
13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

In the server log file:

6.3. Slow query log


First, we enable the slow query log. In MariaDB, we can locate the variables that
configure this log.

 "long_query_time": Indicates the time threshold that query execution should


not exceed to be considered slow.
13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

 "slow_query_log": Determines whether the slow query log is active or not.

 "slow_query_log_file": Specifies the path where the log file will be stored.

We configure these variables in 50-server.cnf and restart MariaDB.

In the log, information about queries taking more than 10 seconds to execute (as per
the configured time) will be recorded; the others will not.
13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

7. Installation of MariaDB in a LAMP environment


First, we need to perform a secure installation of the MariaDB server on a machine (the
previous machine will suffice).

7.1. Installation of Apache


13/11/2023 Installation and Configuration of MariaDB Enrique Martínez
13/11/2023 Installation and Configuration of MariaDB Enrique Martínez
13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

Check in Firefox using 127.0.0.1 or localhost:


13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

7.2. Installation of PHP


Install PHP and its dependencies using:

apt -y install php php-cgi libapache2-mod-php php-common php-pear php-mbstring

Now, we configure Apache:


13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

Restart Apache to load the new configuration with systemctl restart apache2, and
finally, execute:

echo ‘<?php phpinfo(); ?>’ > /var/www/html/info.php

Now, you can check the PHP installation in Firefox by accessing:


13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

7.3. Installation of PhpMyAdmin


We start with some prerequisite installations:

Now, go to the phpMyAdmin website and download it:

Copy the link to the file.


13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

The default Apache web directory is located at /var/www/html/.

Unzip the recently downloaded package:

Since it has a long name, let's change it:

Change the ownership of this directory to the web server user to enable writing to it:

You can try accessing it through the browser:


13/11/2023 Installation and Configuration of MariaDB Enrique Martínez

Before proceeding with this step, it's necessary to restart the computer. The browser
may not display any messages. In any case, continue with the procedure.

Now you can check if it works by accessing localhost or 127.0.0.1. If you encounter any
errors, run the mysql_secure_installation script and set a password for the root user of
the database.

You might also like