MariaDB là một trong những cơ sở dữ liệu quan hệ nguồn mở được sử dụng nhiều nhất. Nó được phát triển bởi các MySQL developer và nó hoàn toàn miễn phí. Sự phát triển của MariaDB tập trung vào sự ổn định và hiệu suất. Nó là cơ sở dữ liệu mặc định trong hầu hết các bản phân phối Linux.
Bước 1: Nâng cấp hệ thống
Chạy nâng cấp hệ thống để đảm bảo rằng đang thực hiện cài đặt trên các gói mới nhất. Sau khi cập nhật, hãy khởi động lại máy chủ.
sudo apt update
sudo apt upgrade -y
sudo reboot
Bước 2: Cài đặt các gói bắt buộc
Tiếp theo, cài đặt gói software-properties-common
sudo apt install software-properties-common -y
Bước 3: Import khóa MariaDB GPG và thêm repository MariaDB APT
Với các lệnh dưới đây, sẽ import khóa MariaDB và thêm repository MariaDB APT:
curl -LsS -O https://downloads.mariadb.com/MariaDB/mariadb_repo_setup
sudo bash mariadb_repo_setup --mariadb-server-version=10.6
Bạn sẽ nhận được output có nội dung như những thứ được hiển thị bên dưới:
[info] Checking for script prerequisites.
[info] Repository file successfully written to /etc/apt/sources.list.d/mariadb.list
[info] Adding trusted package signing keys...
[info] Running apt-get update...
[info] Done adding trusted package signing keys
Bước 4: Cài đặt MariaDB trên Ubuntu 20.04 | 18.04
Khi key MariaDB và APT được thêm vào, hãy cập nhật các gói và tiến hành cài đặt MariaDB trên Ubuntu 20.04 | 18.04:
sudo apt update
sudo apt install mariadb-server mariadb-client
Bước 5: Cài đặt bảo mật MariaDB
Sau khi MariaDB được cài đặt, hãy chạy tập lệnh MySQL bên dưới để bảo mật MariaDB
$ sudo mariadb-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
haven't set the root password yet, you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.
You already have your root account protected, so you can safely answer 'n'.
Switch to unix_socket authentication [Y/n] Y
Enabled successfully!
Reloading privilege tables..
... Success!
You already have your root account protected, so you can safely answer 'n'.
Change the 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!
Bước 6: Xác nhận trạng thái Mariadb
Máy chủ MariaDB sẽ được tự động khởi động. Kiểm tra trạng thái như sau:
$ systemctl status mariadb
● mariadb.service - MariaDB 10.6.4 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Drop-In: /etc/systemd/system/mariadb.service.d
└─migrated-from-my.cnf-settings.conf
Active: active (running) since Tue 2021-10-19 11:37:25 UTC; 9s ago
Docs: man:mariadbd(8)
https://mariadb.com/kb/en/library/systemd/
Main PID: 3023 (mariadbd)
Status: "Taking your SQL requests now..."
Tasks: 14 (limit: 4703)
CGroup: /system.slice/mariadb.service
└─3023 /usr/sbin/mariadbd
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: performance_schema
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: sys
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: Phase 6/7: Checking and upgrading tables
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: Processing databases
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: information_schema
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: performance_schema
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: sys
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: sys.sys_config OK
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: Phase 7/7: Running 'FLUSH PRIVILEGES'
Oct 19 11:37:29 ubuntu /etc/mysql/debian-start[3046]: OK
root@ubuntu:~#
Bước 7: Bật MariaDB để bắt đầu khi khởi động lại máy chủ
Chạy lệnh dưới đây để cho phép MariaDB tự động khởi động khi máy chủ được khởi động lại
sudo systemctl enable mariadb
Bước 8: Kiểm tra phiên bản MariaDB
Để xác nhận phiên bản đã cài đặt, trước tiên chúng ta cần đăng nhập vào MySQL như bên dưới.
$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 32
Server version: 10.6.3-MariaDB-1:10.6.3+maria~focal mariadb.org binary distribution
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)]>
Bây giờ hãy chạy lệnh sau để kiểm tra phiên bản MariaDB
MariaDB [(none)]> SELECT VERSION();
+--------------------------------------+
| VERSION() |
+--------------------------------------+
| 10.6.4-MariaDB-1:10.6.4+maria~bionic |
+--------------------------------------+
1 row in set (0.000 sec)
MariaDB [(none)]> QUIT
Bye
Bước 9: Cách sử dụng MariaDB cơ bản
Tiếp theo, chúng ta sẽ xem một số cách sử dụng cơ bản của MariaDB Dababase như tạo cơ sở dữ liệu, người dùng, v.v.
Tạo cơ sở dữ liệu MariaDB
Như đã thấy ở trên, MariaDB sử dụng cú pháp MySQL. Để tạo cơ sở dữ liệu, trước tiên bạn cần đăng nhập vào mariadb, sau đó chạy lệnh dưới đây để tạo cơ sở dữ liệu.
#Create a new database
MariaDB [(none)]> CREATE DATABASE db1;
Query OK, 1 row affected (0.000 sec)
#If the database with the same exists
CREATE DATABASE db1;
ERROR 1007 (HY000): Can't create database 'db1'; database exists
#Create a database if already exits
MariaDB [(none)]> CREATE OR REPLACE DATABASE db1;
Query OK, 2 rows affected (0.009 sec)
#First check if a database exists
MariaDB [(none)]> CREATE DATABASE IF NOT EXISTS db1;
Query OK, 1 row affected, 1 warning (0.000 sec)
# Check Databases MariaDB
MariaDB [(none)]> SHOW DATABASES;
Thêm người dùng và cấp quyền MariaDB
Để tạo người dùng và cấp quyền
#Create user mariadb
MariaDB [(none)]> CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
#Grant all privileges to the user
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
#Grant privileges to a specific database
MariaDB [(none)]> GRANT ALL PRIVILEGES ON 'DB1'.* TO 'user1'@'localhost';
#Remember to refresh the privileges
MariaDB [(none)]> FLUSH privileges;
#To check user grants in MariaDB
MariaDB [(none)]> SHOW GRANTS FOR 'myuser'@'localhost';
Tạo bảng và thêm dữ liệu MariaDB
MariaDB [(none)]> CREATE TABLE employees (id INT, name VARCHAR(20), email VARCHAR(20));
MariaDB [(none)]> INSERT INTO employees (id,name,email) VALUES(01,"lorna","lorna@example.com"
Nếu bạn muốn xóa hoàn toàn cài đặt MariaDB, bạn có thể dùng lệnh dưới đây.
sudo apt purge mariadb-server
sudo rm -rf /var/lib/mysql/