Latest Post

Tăng thứ hạng và truy cập tự nhiên với 10 phương pháp SEO hay nhất Kiếm Tiền Online (mmo): Khái Niệm và Các Hình Thức Phổ Biến

MariaDB là một hệ quản trị cơ sở dữ liệu mã nguồn mở phổ biến, nổi tiếng với độ ổn định và khả năng mở rộng với các engine lưu trữ mới. Được phát triển dựa trên MySQL, MariaDB tập trung vào việc cải thiện tính ổn định và hiệu suất, và đặc biệt, nó là miễn phí cho người sử dụng. MariaDB thậm chí là cơ sở dữ liệu mặc định trong nhiều bản phân phối Linux. Với sự đa dạng của nhiều công cụ và plugin, MariaDB có rất nhiều ứng dụng khác nhau. Trong hướng dẫn này, chúng ta sẽ khám phá quá trình cài đặt MariaDB 10.6 trên CentOS 7.

Hướng dẫn install MariaDB CentOS 7

Bước 1: Cập nhật gói hệ thống

Hãy đảm bảo rằng bạn đã cập nhật các gói hệ thống lên phiên bản mới nhất trước khi thực hiện quá trình cài đặt. Điều này giúp tránh gặp các vấn đề có thể xuất hiện do sự không tương thích giữa các thành phần phụ thuộc (dependencies).

sudo dnf upgrade

Bước 2: Thêm Kho lưu trữ MariaDB

Chúng ta cần tạo một tệp repo MariaDB và thêm nội dung để cài đặt MariaDB.

curl -LsS -O https://downloads.mariadb.com/MariaDB/mariadb_repo_setup
sudo bash mariadb_repo_setup --mariadb-server-version=10.6

Đầu ra của lệnh như sau:

[info] Checking for script prerequisites.
[info] Repository file successfully written to /etc/yum.repos.d/mariadb.repo
[info] Adding trusted package signing keys...
/etc/pki/rpm-gpg /home/rocky
/home/rocky
[info] Successfully added trusted package signing keys
[info] Cleaning package cache...
62 files removed

Bước 3: Install MariaDB 10.6 CentOS 7

Sau khi đã lưu tệp repo, tiến hành cài đặt MariaDB 10.6:

sudo dnf install MariaDB-server MariaDB-client

Bước 4: Khởi động và kích hoạt MariaDB

Sau khi đã cài đặt, hãy khởi động mariadb và cũng kích hoạt tự động khởi động khi hệ thống khởi động lại.

sudo systemctl start mariadb
sudo systemctl enable mariadb

Bảo mật quá trình cài đặt máy chủ cơ sở dữ liệu:

$ 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 5: Kiểm tra phiên bản MariaDB

Chúng ta cần đăng nhập vào MariaDB trước khi kiểm tra phiên bản đã được cài đặt.

$ mysql -u root -p
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.6.4-MariaDB MariaDB Server
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ạn đã có thể thấy phiên bản MariaDB đã được cài đặt từ kết quả ở trên. Tuy nhiên, bạn cũng có thể chạy lệnh sau để kiểm tra phiên bản MariaDB:

MariaDB [(none)]> SELECT VERSION();
+-------------------------------------+
| VERSION() |
+-------------------------------------+
| 10.6.4-MariaDB |
+-------------------------------------+
1 row in set (0.000 sec)

Bước 6: Tạo cơ sở dữ liệu trong MariaDB

Sau khi đăng nhập vào MariaDB, bạn có thể tạo một cơ sở dữ liệu (database) như sau:

#Create a new database
MariaDB [(none)]> CREATE DATABASE db1;
Query OK, 1 row affected (0.000 sec)
#If the database with the same name exists, you should get an error
ERROR 1007 (HY000): Can't create database 'db1'; database exists

#Create a database if already exits, replace
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;

Bước 7: Tạo người dùng và cấp đặc quyền trong MariaDB

Để tạo một người dùng và cấp đặc quyền cho các cơ sở dữ liệu, hãy thực hiện các lệnh sau:

#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';

Bước 8: Tạo bảng và thêm dữ liệu trong MariaDB

Vì chúng ta đã có một cơ sở dữ liệu, chúng ta có thể tiếp tục tạo một bảng và thêm một số giá trị vào đó.

MariaDB [(none)]> USE db1;
MariaDB [(none)]> CREATE TABLE employees (id INT, name VARCHAR(20), email VARCHAR(20));
MariaDB [(none)]> INSERT INTO employees (id,name,email) VALUES(01,"lorna","[email protected]")

Xoá cài đặt MariaDB

Để xóa hoàn toàn MariaDB, hãy chạy các lệnh sau:

sudo dnf remove MariaDB-server MariaDB-client
sudo rm -rf /var/lib/mysql/
sudo rm /etc/my.cnf

Bạn đã hoàn thành việc cài đặt MariaDB phiên bản 10.6 thành công. Chúng ta cũng đã thực hiện việc gỡ bỏ MariaDB. Hi vọng hướng dẫn này mang lại giá trị cho bạn.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *