{"id":1842,"date":"2021-03-12T00:03:11","date_gmt":"2021-03-11T16:03:11","guid":{"rendered":"http:\/\/blog.zengqq.com.cn\/?p=1842"},"modified":"2021-03-12T00:03:11","modified_gmt":"2021-03-11T16:03:11","slug":"installing-mariadb-in-debianraspios","status":"publish","type":"post","link":"https:\/\/blog.zengqq.com.cn\/?p=1842","title":{"rendered":"Installing MariaDB in Debian(RaspiOS)"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\" id=\"introduction\">Introduction<\/h3>\n\n\n\n<p><a href=\"https:\/\/mariadb.org\/\">MariaDB<\/a>&nbsp;is an open-source database management system, commonly used as an alternative for the MySQL portion of the popular&nbsp;<a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-install-linux-apache-mariadb-php-lamp-stack-debian10\">LAMP<\/a>&nbsp;(Linux, Apache, MySQL, PHP\/Python\/Perl) stack. It is intended to be a drop-in replacement for MySQL and Debian now only ships with MariaDB packages. If you attempt to install MySQL server related packages, you\u2019ll receive the compatible MariaDB replacement versions instead.<\/p>\n\n\n\n<p>The short version of this installation guide consists of these three steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Update your package index using&nbsp;<code>apt<\/code><\/li><li>Install the&nbsp;<code>mariadb-server<\/code>&nbsp;package using&nbsp;<code>apt<\/code>. The package also pulls in related tools to interact with MariaDB<\/li><li>Run the included&nbsp;<code>mysql_secure_installation<\/code>&nbsp;security script to restrict access to the server<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt update\nsudo apt install mariadb-server\nsudo mysql_secure_installation\n<\/pre>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<p>This tutorial will explain how to install MariaDB version 10.3 on a Debian 10 server, and verify that it is running and has a safe initial configuration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"prerequisites\">Prerequisites<\/h2>\n\n\n\n<p>To follow this tutorial, you will need:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>One Debian 10 server set up by following&nbsp;<a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/initial-server-setup-with-debian-10\">this initial server setup guide<\/a>, including a non-<strong>root<\/strong>&nbsp;user with&nbsp;<code>sudo<\/code>&nbsp;privileges and a firewall.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-1-\u2014-installing-mariadb\">Step 1 \u2014 Installing MariaDB<\/h2>\n\n\n\n<p>On Debian 10, MariaDB version 10.3 is included in the APT package repositories by default. It is marked as the default MySQL variant by the Debian MySQL\/MariaDB packaging team.<\/p>\n\n\n\n<p>To install it, update the package index on your server with&nbsp;<code>apt<\/code>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt update\n<\/pre>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<p>Then install the package:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt install mariadb-server\n<\/pre>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<p>These commands will install MariaDB, but will not prompt you to set a password or make any other configuration changes. Because the default configuration leaves your installation of MariaDB insecure, we will use a script that the&nbsp;<code>mariadb-server<\/code>&nbsp;package provides to restrict access to the server and remove unused accounts.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-2-\u2014-configuring-mariadb\">Step 2 \u2014 Configuring MariaDB<\/h2>\n\n\n\n<p>For new MariaDB installations, the next step is to run the included security script. This script changes some of the less secure default options. We will use it to block remote&nbsp;<strong>root<\/strong>&nbsp;logins and to remove unused database users.<\/p>\n\n\n\n<p>Run the security script:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo mysql_secure_installation\n<\/pre>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<p>This will take you through a series of prompts where you can make some changes to your MariaDB installation\u2019s security options. The first prompt will ask you to enter the current database&nbsp;<strong>root<\/strong>&nbsp;password. Since we have not set one up yet, press&nbsp;<code>ENTER<\/code>&nbsp;to indicate \u201cnone\u201d.<\/p>\n\n\n\n<p>The next prompt asks you whether you\u2019d like to set up a database&nbsp;<strong>root<\/strong>&nbsp;password. Type&nbsp;<code>N<\/code>&nbsp;and then press&nbsp;<code>ENTER<\/code>. In Debian, the&nbsp;<strong>root<\/strong>&nbsp;account for MariaDB is tied closely to automated system maintenance, so we should not change the configured authentication methods for that account. Doing so would make it possible for a package update to break the database system by removing access to the administrative account. Later, we will cover how to optionally set up an additional administrative account for password access if socket authentication is not appropriate for your use case.<\/p>\n\n\n\n<p>From there, you can press&nbsp;<code>Y<\/code>&nbsp;and then&nbsp;<code>ENTER<\/code>&nbsp;to accept the defaults for all the subsequent questions. This will remove some anonymous users and the test database, disable remote&nbsp;<strong>root<\/strong>&nbsp;logins, and load these new rules so that MariaDB immediately respects the changes you have made.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-3-\u2014-optional-adjusting-user-authentication-and-privileges\">Step 3 \u2014 (Optional) Adjusting User Authentication and Privileges<\/h2>\n\n\n\n<p>In Debian systems running MariaDB 10.3, the&nbsp;<strong>root<\/strong>&nbsp;MariaDB user is set to authenticate using the&nbsp;<code>unix_socket<\/code>&nbsp;plugin by default rather than with a password. This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external program (e.g., phpMyAdmin) administrative rights.<\/p>\n\n\n\n<p>Because the server uses the&nbsp;<strong>root<\/strong>&nbsp;account for tasks like log rotation and starting and stopping the server, it is best not to change the&nbsp;<strong>root<\/strong>&nbsp;account\u2019s authentication details. Changing credentials in the&nbsp;<code>\/etc\/mysql\/debian.cnf<\/code>&nbsp;configuration file may work initially, but package updates could potentially overwrite those changes. Instead of modifying the&nbsp;<strong>root<\/strong>&nbsp;account, the package maintainers recommend creating a separate administrative account for password-based access.<\/p>\n\n\n\n<p>To do so, we will create a new account called&nbsp;<code>admin<\/code>&nbsp;with the same capabilities as the&nbsp;<strong>root<\/strong>&nbsp;account, but configured for password authentication. To do this, open up the MariaDB prompt from your terminal:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo mysql\n<\/pre>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<p>Now, we will create a new user with&nbsp;<strong>root<\/strong>&nbsp;privileges and password-based access. Change the username and password to match your preferences:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">MariaDB [(none)]> GRANT ALL ON *.* TO 'admin'@'localhost' IDENTIFIED BY '123123' WITH GRANT OPTION;\n<\/pre>\n\n\n\n<p>Flush the privileges to ensure that they are saved and available in the current session:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">MariaDB [(none)]> FLUSH PRIVILEGES;\n<\/pre>\n\n\n\n<p>Following this, exit the MariaDB shell:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">MariaDB [(none)]> exit\n<\/pre>\n\n\n\n<p>Finally, let\u2019s test the MariaDB installation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-4-\u2014-testing-mariadb\">Step 4 \u2014 Testing MariaDB<\/h2>\n\n\n\n<p>When installed from the default repositories, MariaDB should start running automatically. To test this, check its status.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl status mariadb\n<\/pre>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<p>You\u2019ll receive output that is similar to the following:Output<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u25cf mariadb.service - MariaDB 10.3.15 database server\n   Loaded: loaded (\/lib\/systemd\/system\/mariadb.service; enabled; vendor preset: enabled)\n   Active: active (running) since Fri 2019-07-12 20:35:29 UTC; 47min ago\n     Docs: man:mysqld(8)\n           https:\/\/mariadb.com\/kb\/en\/library\/systemd\/\n Main PID: 2036 (mysqld)\n   Status: \"Taking your SQL requests now...\"\n    Tasks: 30 (limit: 2378)\n   Memory: 76.1M\n   CGroup: \/system.slice\/mariadb.service\n           \u2514\u25002036 \/usr\/sbin\/mysqld\n\nJul 12 20:35:29 deb-mariadb1 \/etc\/mysql\/debian-start[2074]: Phase 6\/7: Checking and upgrading tables\nJul 12 20:35:29 deb-mariadb1 \/etc\/mysql\/debian-start[2074]: Running 'mysqlcheck' with connection arguments: --socket='\/var\/run\/mysqld\/mysqld.sock' --host='localhost' --socket='\/var\/run\/mysqld\/mysqld.sock' --host='localhost' --socket='\/var\/run\/mysqld\/mysqld.sock'\nJul 12 20:35:29 deb-mariadb1 \/etc\/mysql\/debian-start[2074]: # Connecting to localhost...\nJul 12 20:35:29 deb-mariadb1 \/etc\/mysql\/debian-start[2074]: # Disconnecting from localhost...\nJul 12 20:35:29 deb-mariadb1 \/etc\/mysql\/debian-start[2074]: Processing databases\nJul 12 20:35:29 deb-mariadb1 \/etc\/mysql\/debian-start[2074]: information_schema\nJul 12 20:35:29 deb-mariadb1 \/etc\/mysql\/debian-start[2074]: performance_schema\nJul 12 20:35:29 deb-mariadb1 \/etc\/mysql\/debian-start[2074]: Phase 7\/7: Running 'FLUSH PRIVILEGES'\nJul 12 20:35:29 deb-mariadb1 \/etc\/mysql\/debian-start[2074]: OK\nJul 12 20:35:30 deb-mariadb1 \/etc\/mysql\/debian-start[2132]: Triggering myisam-recover for all MyISAM tables and aria-recover for all Aria tables<\/pre>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<p>If MariaDB isn\u2019t running, you can start it with the command&nbsp;<code>sudo systemctl start mariadb<\/code>.<\/p>\n\n\n\n<p>For an additional check, you can try connecting to the database using the&nbsp;<code>mysqladmin<\/code>&nbsp;tool, which is a client that lets you run administrative commands. For example, this command says to connect to MariaDB as&nbsp;<strong>root<\/strong>&nbsp;and return the version using the Unix socket:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo mysqladmin version\n<\/pre>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<p>You should receive output similar to this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Outputmysqladmin  Ver 9.1 Distrib 10.3.15-MariaDB, for debian-linux-gnu on x86_64\nCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.\n\nServer version      10.3.15-MariaDB-1\nProtocol version    10\nConnection      Localhost via UNIX socket\nUNIX socket     \/var\/run\/mysqld\/mysqld.sock\nUptime:         48 min 14 sec\n\nThreads: 7  Questions: 474  Slow queries: 0  Opens: 177  Flush tables: 1  Open tables: 31  Queries per second avg: 0.163\n<\/pre>\n\n\n\n<p>If you configured a separate administrative user with password authentication, you could perform the same operation by typing:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mysqladmin -u admin -p version\n<\/pre>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<p>This means that MariaDB is up and running and that your user is able to authenticate successfully.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>In this guide you installed MariaDB to act as an SQL server. During the installation process you also secured the server. Optionally, you also created a separate user to ensure administrative access to MariaDB across package updates.<\/p>\n\n\n\n<p>Now that you have a running and secure MariaDB server, here some examples of next steps that you can take to work with the server:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-import-and-export-databases-in-mysql-or-mariadb\">Import and export databases<\/a><\/li><\/ul>\n\n\n\n<p>You can also incorporate MariaDB into a larger application stack:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-install-linux-nginx-mariadb-php-lemp-stack-on-debian-10\">How To Install Linux, Nginx, MariaDB, PHP (LEMP stack) on Debian 10<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Introduction MariaDB&#038;nbsp [&#8230;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[253,249],"tags":[350,53],"class_list":["post-1842","post","type-post","status-publish","format-standard","hentry","category-fun","category-technology","tag-mairadb","tag-mysql"],"_links":{"self":[{"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/1842","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1842"}],"version-history":[{"count":1,"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/1842\/revisions"}],"predecessor-version":[{"id":1843,"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/1842\/revisions\/1843"}],"wp:attachment":[{"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}