如何在 CentOS 7 上安裝 MariaDB 10.2


本文將介紹如何在 CentOS 7/RHEL 上安裝 MariaDB 10.2。這篇文章可以作為初學者的指南或參考。請注意,這也可以作為 MySQL 的替代方案。

MariaDB 的特性

  • MariaDB 是一款開源的替代型關係資料庫管理軟體。
  • MariaDB 具有強大的效能,快速且可擴充套件,並擁有豐富的儲存引擎。
  • MariaDB 具有 GIS 和 JSON 等新特性。

伺服器資訊和軟體包

  • 包含的軟體包:MariaDB、MariaDB-server、MariaDB-libs。
  • 使用的守護程序名稱為:mariadb。
  • 埠號:3306。
  • 配置檔案路徑:/etc/my.cnf。

在 CentOS 7 / RHEL 7 上安裝 MariaDB 10.2

通常,MariaDB 軟體包隨安裝介質一起在本地儲存庫中提供,我們可以直接安裝。但是,如果需要安裝最新軟體包,可以透過將儲存庫新增到 yum 本地儲存庫列表來實現。我們使用以下命令和程式碼將儲存庫新增到 yum。

將 MariaDB 安裝的 Repo 新增到 Yum

為 CentOS 7 64 位新增 Repo

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

為 RHEL 7 64 位新增 Repo

[mariadb10]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/rhel7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

使用 Yum Repo 安裝 MariaDB

我們可以使用 Yum Repo 安裝 MariaDB,因為我們剛剛將儲存庫新增到本地 yum 中,以下是使用 Yum 安裝 MariaDB 的命令

# yum groupinstall mariadb*
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: ftp.iitm.ac.in
* extras: ftp.iitm.ac.in
* updates: ftp.iitm.ac.in
....
....
.....
....
....
...
....
Downloading packages:
--------------------------------------------------------------------------------------------------------------------------------------------
Total 68 MB/s | 22 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Installing : perl-Data-Dumper-2.145-3.el7.x86_64 1/13
Installing : unixODBC-2.3.1-10.el7.x86_64 2/13
Installing : perl-Net-Daemon-0.48-5.el7.noarch 3/13
Installing : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64 4/13
Installing : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64 5/13
Installing : perl-IO-Compress-2.061-2.el7.noarch 6/13
Installing : perl-PlRPC-0.2020-14.el7.noarch 7/13
Installing : perl-DBI-1.627-4.el7.x86_64 8/13
Installing : perl-DBD-MySQL-4.023-5.el7.x86_64 9/13
Installing : 1:mariadb-5.5.41-2.el7_0.x86_64 10/13
Installing : 1:mariadb-server-5.5.41-2.el7_0.x86_64 11/13
Installing : mysql-connector-odbc-5.2.5-6.el7.x86_64 12/13
Installing : MySQL-python-1.2.3-11.el7.x86_64 13/13
Verifying : 1:mariadb-5.5.41-2.el7_0.x86_64 1/13
Verifying : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64 2/13
Verifying : mysql-connector-odbc-5.2.5-6.el7.x86_64 3/13
Verifying : perl-Data-Dumper-2.145-3.el7.x86_64 4/13
Verifying : MySQL-python-1.2.3-11.el7.x86_64 5/13
Verifying : 1:mariadb-server-5.5.41-2.el7_0.x86_64 6/13
Verifying : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64 7/13
Verifying : perl-PlRPC-0.2020-14.el7.noarch 8/13
Verifying : perl-Net-Daemon-0.48-5.el7.noarch 9/13
Verifying : perl-DBI-1.627-4.el7.x86_64 10/13
Verifying : unixODBC-2.3.1-10.el7.x86_64 11/13
Verifying : perl-DBD-MySQL-4.023-5.el7.x86_64 12/13
Verifying : perl-IO-Compress-2.061-2.el7.noarch 13/13
Installed:
MySQL-python.x86_64 0:1.2.3-11.el7 mariadb.x86_64 1:5.5.41-2.el7_0 mariadb-server.x86_64 1:5.5.41-2.el7_0
mysql-connector-odbc.x86_64 0:5.2.5-6.el7
Dependency Installed:
perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 perl-DBD-MySQL.x86_64 0:4.023-5.el7
perl-DBI.x86_64 0:1.627-4.el7 perl-Data-Dumper.x86_64 0:2.145-3.el7 perl-IO-Compress.noarch 0:2.061-2.el7
perl-Net-Daemon.noarch 0:0.48-5.el7 perl-PlRPC.noarch 0:0.2020-14.el7 unixODBC.x86_64 0:2.3.1-10.el7
Complete!

啟動 MariaDB 服務

以下是啟動 MariaDB 服務的命令

# systemctl start mariadb.services

我們需要在啟動時新增服務以啟動服務,可以使用以下命令在啟動時啟動 MariaDB 服務。

# systemctl enable mariadb.services

新增 MariaDB 埠以允許防火牆訪問

# firewall-cmd –permanent –add-services=mysql
# firewall-cmd –permanent –add-port=3306/tcp
# firewall-cmd –reload

保護 mariadb 並設定 root 使用者密碼

預設情況下,mariadb 的 root 密碼未設定,如果沒有 root 密碼,我們將無法登入資料庫伺服器。以下是設定 MariaDB 的 root 密碼、刪除匿名使用者、透過限制 root 的遠端登入來保護 MariaDB 以及刪除測試資料庫的命令。

# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
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
you 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 MariaDB
root user without the proper authorization.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading the 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. Install MariaDB 10.2 RHEL 7
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. Install MariaDB 10.2 RHEL 7
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!

測試 MariaDB 連線

完成安裝併為 root 使用者分配密碼並保護連線後,我們需要透過登入資料庫來測試 mariadb。以下是測試 MariaDB 的命令:

# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with; or \g.
Your MariaDB connection id is 10
Server version: 5.5.41-MariaDB MariaDB Server Install MariaDB 10.2 RHEL 7
Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>

透過以上步驟,我們可以安裝 CentOS 7/RHEL 7 上的 MariaDB 10.2,並透過刪除匿名使用者和測試資料庫來保護 mariadb,這是一個很好的 MySQL 伺服器替代方案。在以後的文章中,您將學習更多關於 MariaDB 的知識。

更新於:2019年10月18日

1K+ 瀏覽量

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告