MariaDB是一个开源的关系数据库管理系统。它最后被设计为MySQL的向后兼容二进制二进制替换。
MariaDB由MySQL的原始开发人员和凋谢源代码社区开发和保护。
本指南阐明了如何在Ubuntu 20.04(WSL)上装置和爱护MariaDB。

1、根底条件

本文装置环境为Ubuntu20.04 on WSL(Windows Subsystem for Linux),咱们假如您具备root用户或具备sudo权限的治理拜访权限。

2、装置MariaDB

sudo apt updatesudo apt -y install mariadb-server

装置实现后,MariaDB服务不会主动启动,请应用上面命令启动MariaDB服务:

sudo service mysql start

3、配置MariaDB

MariaDB服务有一个脚本叫做mysql_secure_installation,它容许你能够很容易进步数据库服务器平安。
执行不带参数的脚本:

sudo mysql_secure_installation

该脚本将提醒您输出root明码,因为您尚未设置root明码,只需在此处按“ Enter”。

Enter current password for root (enter for none):

在下一个提示符下,将要求您设置MySQL root用户的明码,输出n

Set root password? [Y/n] n

在Ubuntu上,auth_socket默认状况下插件会对MariaDB根用户进行身份验证。该插件通过查看调用客户端程序的本地零碎用户是否与指定的MariaDB用户名匹配来工作。

接下来,将要求您删除匿名用户,限度root用户对本地计算机的拜访,删除测试数据库并从新加载特权表。能够依照本人的爱好来批改:

# sudo mysql_secure_installationNOTE: 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 currentpassword for the root user.  If you've just installed MariaDB, andyou 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 MariaDBroot user without the proper authorisation.# 批改root用户明码Set root password? [Y/n] n ... skipping.By default, a MariaDB installation has an anonymous user, allowing anyoneto log into MariaDB without having to have a user account created forthem.  This is intended only for testing, and to make the installationgo a bit smoother.  You should remove them before moving into aproduction environment.# 删除匿名用户Remove anonymous users? [Y/n] y ... Success!Normally, root should only be allowed to connect from 'localhost'.  Thisensures that someone cannot guess at the root password from the network.# 禁止root近程登录Disallow root login remotely? [Y/n] n ... skipping.By default, MariaDB comes with a database named 'test' that anyone canaccess.  This is also intended only for testing, and should be removedbefore moving into a production environment.# 删除测试数据库和拜访权限Remove test database and access to it? [Y/n] n ... skipping.Reloading the privilege tables will ensure that all changes made so farwill 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 MariaDBinstallation should now be secure.Thanks for using MariaDB!

4、以root身份登录

要从命令行与MariaDB服务器进行交互,请应用mysql客户端实用程序或其别名mariadb。此工具是作为MariaDB服务器软件包的依赖项装置的。

该auth_socket插件对localhost通过Unix套接字文件从进行连贯的用户进行身份验证。这意味着您不能通过提供明码来以root用户身份进行身份验证。

要以root用户身份登录MariaDB服务器(留神:以后形式下不再须要输出-u root -p参数了),请输出:

sudo mysql

将为您提供MariaDB shell,如下所示:

# sudo mysqlWelcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 54Server version: 10.3.31-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04Copyright (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)]>MariaDB [(none)]> quitBye

5、创立一个超级用户

在运行MariaDB的Ubuntu零碎上,默认状况下,将root MariaDB用户设置为应用unix_socket插件而不是应用明码进行身份验证。 因为服务器应用root帐户执行日志轮换以及启动和进行服务器等工作,因而最好不要更改root帐户的身份验证详细信息。 相同,软件包维护者倡议为基于明码的拜访创立一个独自的治理帐户。

如果要应用内部程序(例如phpMyAdmin)以root用户身份登录MariaDB服务器,则有两个抉择。

第一个是将身份验证办法从更改auth_socket为mysql_native_password。您能够通过运行以下命令来做到这一点:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_strong_password';FLUSH PRIVILEGES;

举荐的第二个选项是创立一个新的专用治理用户,该用户能够拜访所有数据库:

GRANT ALL PRIVILEGES on *.* TO 'admin'@'%' IDENTIFIED BY 'very_strong_password' WITH GRANT OPTION;SET PASSWORD FOR admin=PASSWORD('very_strong_password');FLUSH PRIVILEGES;QUIT;

您能够应用任何想要的名称来命名治理用户,然而请确保应用强明码。残缺的示例如下:

# sudo mysqlWelcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 43Server version: 10.3.31-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04Copyright (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)]> GRANT ALL PRIVILEGES on *.* TO 'admin'@'%' IDENTIFIED BY 'very_strong_password' WITH GRANT OPTION;RD FOQuery OK, 0 rows affected (0.000 sec)R admiMariaDB [(none)]> SET PASSWORD FOR admin=PASSWORD('very_strong_password');Query OK, 0 rows affected (0.000 sec)ES;QMariaDB [(none)]> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.000 sec)MariaDB [(none)]> QUIT;Bye

您能够应用mysqladmin工具(能够运行治理命令的客户端)测试该新用户。 以下mysqladmin命令以admin用户身份连贯到MariaDB,并在提醒用户输出明码后返回版本号:

sudo mysqladmin -u admin -p version

将收到相似于以下的输入:

# sudo mysqladmin -u admin -p versionEnter password:mysqladmin  Ver 9.1 Distrib 10.3.31-MariaDB, for debian-linux-gnu on x86_64Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Server version          10.3.31-MariaDB-0ubuntu0.20.04.1Protocol version        10Connection              Localhost via UNIX socketUNIX socket             /var/run/mysqld/mysqld.sockUptime:                 10 min 31 secThreads: 6  Questions: 140  Slow queries: 0  Opens: 38  Flush tables: 1  Open tables: 31  Queries per second avg: 0.221

6、MariaDB应用

  • MariaDB治理和根底配置
  • MariaDB用户和数据库治理
  • MariaDB导入测试数据库employees

7、其余链接

  • 在Ubuntu原生零碎上装置MariaDB:
    https://blog.csdn.net/fenglai...
  • How To Install MariaDB on Ubuntu 20.04:
    https://www.digitalocean.com/...
  • How To Import and Export Databases in MySQL or MariaDB:
    https://www.digitalocean.com/...
  • An Introduction to Queries in MySQL:
    https://www.digitalocean.com/...
  • How to Manage MySQL Databases and Users from the Command Line
    https://linuxize.com/post/how...