{"id":767,"date":"2022-09-01T12:08:02","date_gmt":"2022-09-01T12:08:02","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=767"},"modified":"2022-09-01T12:08:02","modified_gmt":"2022-09-01T12:08:02","slug":"install-mariadb-on-ubuntu","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=767","title":{"rendered":"How to Install MariaDB on Ubuntu [Setup &#038; Configurations]"},"content":{"rendered":"<p>In this blog, you will learn to <strong>install <\/strong><a href=\"https:\/\/mariadb.org\/?ref=devopscube.com\" rel=\"noreferrer noopener\"><strong>MariaDB<\/strong><\/a><strong> on Ubuntu 20.04<\/strong> server using step-by-step guides including important <strong>MariaDB<\/strong> configurations.<\/p>\n<h2 id=\"install-mariadb-on-ubuntu-2004\">Install MariaDB on Ubuntu 20.04<\/h2>\n<p>Follow the steps given below for installing and configuring MariaDB.<\/p>\n<h3 id=\"step-1-install-the-mariadb-server-client\">Step 1:  Install the MariaDB server &amp; Client<\/h3>\n<p>Update the server<\/p>\n<pre><code>sudo apt update -y<\/code><\/pre>\n<p>As a recommended practice, we will add the <a href=\"https:\/\/mariadb.com\/kb\/en\/mariadb-package-repository-setup-and-usage\/?ref=devopscube.com\" rel=\"noreferrer noopener\">official MariaDB apt repository<\/a> using the following script.<\/p>\n<pre><code>curl -LsS https:\/\/r.mariadb.com\/downloads\/mariadb_repo_setup | sudo bash<\/code><\/pre>\n<p>Now, install MariaDB server and client.<\/p>\n<pre><code>sudo apt-get install mariadb-server mariadb-client -y<\/code><\/pre>\n<h3 id=\"step-2-validate-mariadb-installtion\">Step 2: Validate MariaDB Installtion<\/h3>\n<p>First, let&#8217;s check the installed version of MariaDB using the following command.<\/p>\n<pre><code>mysql -V<\/code><\/pre>\n<p>Now, let&#8217;s check MariaDB Service Status.<\/p>\n<pre><code>sudo systemctl status mariadb<\/code><\/pre>\n<p>You should get an active status output as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-28-22.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"626\" height=\"376\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-28-22.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-28-22.png 626w\"><\/figure>\n<h3 id=\"step-3-secure-mariadb\">Step 3: Secure MariaDB<\/h3>\n<p>The next step is to secure the MariaDB server by removing all the insecure configurations. Execute the following command to start the configuration.<\/p>\n<pre><code>sudo mysql_secure_installation<\/code><\/pre>\n<p>You will be prompted with six questions. Choose options as shown below.<\/p>\n<ol>\n<li><strong>Enter current password for root (enter for none):<\/strong> Press enter as there is no password by default.<\/li>\n<li><strong>Set root password? [Y\/n]: <\/strong>Select Y and enter a new password.<\/li>\n<li><strong>Remove anonymous users? [Y\/n]:<\/strong> Select Y<\/li>\n<li><strong>Disallow root login remotely? [Y\/n]<\/strong>: Enter Y<\/li>\n<li><strong>Remove the test database and access to it? [Y\/n]:<\/strong> Enter Y<\/li>\n<li><strong>Reload privilege tables now? [Y\/n]:<\/strong> Enter Y<\/li>\n<\/ol>\n<h3 id=\"step-4-connect-to-mariadb\">Step 4: Connect to MariaDB<\/h3>\n<p>Now that we have secured MariaDB and set a root password, let&#8217;s validate the configurations by connecting to MariaDB.<\/p>\n<p>Execute the following <strong>command to connect to MariaDB<\/strong>. When prompted, enter the root password you set up in the previous step.<\/p>\n<pre><code>mysql -u root -p<\/code><\/pre>\n<p>You should be able to connect as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-29-23.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"618\" height=\"358\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-29-23.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-29-23.png 618w\"><\/figure>\n<h2 id=\"mariadb-remote-connection-configuration\">MariaDB Remote Connection Configuration<\/h2>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/mariadb-drawio.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"645\" height=\"551\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/mariadb-drawio.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/mariadb-drawio.png 645w\"><\/figure>\n<p>By default, MariaDB accepts connections only on the Unix socket and localhost. You can verify it by listing the TCP connection<\/p>\n<pre><code>ss -nlt<\/code><\/pre>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-319.png\" class=\"kg-image\" alt=\"MariaDB default localhost connection.\" loading=\"lazy\" width=\"405\" height=\"141\"><\/figure>\n<p>If you try to connect MariaDB from a remote host, you will get the following error<\/p>\n<pre><code>ERROR 2002 (HY000): Can't connect to server on '192.168.6.6' (115)<\/code><\/pre>\n<p>To enable remote connection, open the <code>50-server.cnf<\/code> file. You can find this file in the <code>\/etc\/mysql\/mariadb.conf.d\/ <\/code>location.<\/p>\n<pre><code>sudo vi \/etc\/mysql\/mariadb.conf.d\/50-server.cnf<\/code><\/pre>\n<p>Find the <code>bind-address<\/code> parameter and replace the bind-address value from <code>127.0.0.1<\/code> to <code>0.0.0.0<\/code> as shown below.<\/p>\n<pre><code>bind-address            = 0.0.0.0<\/code><\/pre>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-1-52.png\" class=\"kg-image\" alt=\"MariaDB remote connection bind address\" loading=\"lazy\" width=\"557\" height=\"519\"><\/figure>\n<p>Restart the MariaDB server for the changes to take effect.<\/p>\n<pre><code>sudo systemctl restart mariadb<\/code><\/pre>\n<p>Now, if you check the TCP connections, you will see that the MariaDB server is listening on all the interfaces.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-2-54.png\" class=\"kg-image\" alt=\"MariaDB server listening on all interfaces.\" loading=\"lazy\" width=\"408\" height=\"152\"><\/figure>\n<h2 id=\"mariadb-database-access-from-remote-clients\">MariaDB Database Access From Remote Clients<\/h2>\n<p>Even though the MariaDB server accepts connections from remote hosts, you need to allow host-specific remote database access. If not, you will get the following error.<\/p>\n<pre><code>ERROR 1130 (HY000): Host '' is not allowed to connect to this MariaDB server<\/code><\/pre>\n<p>Now we need to create a database and a user to allow remote connections. First login using the root user and password.<\/p>\n<pre><code>mysql -u root -p<\/code><\/pre>\n<p>Create a database using the following command. Replace <code>appdb<\/code> with a database name of your choice.<\/p>\n<pre><code>CREATE DATABASE appdb;<\/code><\/pre>\n<p>Create a database user named <code>dbadmin<\/code> and grant all the privileges to the <code>appdb<\/code> database from the remote host <code>192.168.6.6<\/code>. Replace <code>192.168.6.6<\/code> with your client host IP address from where you need to connect to the Database.<\/p>\n<pre><code>GRANT ALL ON appdb.* to 'dbadmin'@'192.168.6.6' IDENTIFIED BY 'myPassword';<\/code><\/pre>\n<p>If you want to allow DB connection from anyhost for a user you need to use <code>%<\/code> wildcard instead of host address as shown below.<\/p>\n<pre><code>GRANT ALL ON appdb.* to 'user'@'%' IDENTIFIED BY 'myPassword';<\/code><\/pre>\n<p>In MariaDB, a user is identified along with the host address. You can list all the users using the following SQL query.<\/p>\n<pre><code>SELECT User, Host FROM mysql.user;<\/code><\/pre>\n<p>Now, try connecting to the <code>dbadmin<\/code> MariaDB database from the remote host.<\/p>\n<pre><code>mysql -u dbadmin -p -h 192.168.10.5<\/code><\/pre>\n<h2 id=\"mariadb-important-configurations\">MariaDB Important Configurations<\/h2>\n<p>The following table contains all the important MariaDB configurations.<\/p>\n<p><!--kg-card-begin: html--><\/p>\n<table class=\"has-fixed-layout\">\n<thead>\n<tr>\n<th>MariaDB Configuration<\/th>\n<th>Details<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>MariaDB default port<\/td>\n<td><code>3306<\/code><\/td>\n<\/tr>\n<tr>\n<td>TCP Socket file<\/td>\n<td><code>\/run\/mysqld\/mysqld.sock<\/code><\/td>\n<\/tr>\n<tr>\n<td>MariaDB Config Files location<\/td>\n<td><code>\/etc\/mysql\/mariadb.conf.d<\/code><\/td>\n<\/tr>\n<tr>\n<td>MariaDB Server Configuration file <\/td>\n<td><code>\/etc\/mysql\/mariadb.conf.d\/50-server.cnf<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><!--kg-card-end: html--><\/p>\n<h2 id=\"mariadb-server-installation-faqs\">MariaDB Server Installation FAQs<\/h2>\n<h3 id=\"how-to-enable-remote-access-on-mariadb\">How to enable remote access on MariaDB?<\/h3>\n<p>To enable remote access, you need to replace the bind address with 0.0.0.0. Also, you need to add a user with the remote host details or a wildcard entry to access the database remotely.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>This guide looked at the steps to install MariaDB on a Ubuntu server.<\/p>\n<p>As a DevOps engineer, it is essential to know the basic administration and configurations involved in a Database.<\/p>\n<p>If you are starting your DevOps engineer journey, look at my comprehensive <a href=\"https:\/\/devopscube.com\/become-devops-engineer\/\">guide to becoming a DevOps engineer<\/a>.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/install-mariadb-on-ubuntu\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Install MariaDB on Ubuntu [Setup &amp; Configurations] \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/install-mariadb-on-ubuntu\/<\/p>\n","protected":false},"author":1,"featured_media":768,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-767","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=767"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/767\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/768"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}