Skip to main content

安裝 MariaDB 10.3 版

1 安裝 MariaDB 10.3 套件

在 Ubuntu Server 20.04 安裝 MariaDB 10.3 相對簡單,而且 10.3 是比較穩定的版本,故目前仍建議使用預設的版本,安裝指令如下所示,這個版本的 MariaDB 相依了非常多的套件。

$ sudo apt install mariadb-server -y
2 初始化 MariaDB 資料庫環境

因為 Ubuntu Server 20.04 預設會啟用服務,所以可以直接初始化 MariaDB 的環境,指令如下:

先直接切換成 root 身份,使用 sudo 指令會有奇怪的狀況發生
$ sudo su -
# mysql_secure_installation

接下來會一直問各種問題,如下所示,詢問目前 root 用戶的密碼,因為一開始沒有設定,請直接按 Enter 鍵繼續。


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):直接按 Enter 鍵

要不要設定 root 密碼?按下 Y 並輸入二次密碼。

OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

是否要移除 anonymous users 的密碼?按下 Y 繼續。

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!

是否要關閉 root 從遠端登入的權限?按下 Y 繼續。

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!

是否要移除測試資料庫?按下 Y 繼續。

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!

是否要 Reload 權限?按下 Y 繼續。

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!

完成 MariaDB 初始化設定工作,然後執行 exit 指令跳出 root 身份。

# exit
3 重新啟動 MariaDB 服務
$ sudo systemctl restart mariadb
4 確認 MariaDB 服務狀態

利用 netstat 指令確認服務是否有啟動(有發現 3306 port 運作中)。

$ sudo netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0*               LISTEN      22965/mysqld
tcp        0      0 *               LISTEN      603/systemd-resolve
tcp        0      0    *               LISTEN      690/sshd: /usr/sbin
tcp        0      0*               LISTEN      22203/sshd: ossii@p
tcp6       0      0 :::80                   :::*                    LISTEN      21359/apache2
tcp6       0      0 :::22                   :::*                    LISTEN      690/sshd: /usr/sbin
tcp6       0      0 ::1:6010                :::*                    LISTEN      22203/sshd: ossii@p
tcp6       0      0 :::443                  :::*                    LISTEN      21359/apache2

利用 systemctl 指令查看一下相關服務的啟用情況。

$ sudo systemctl status mariadb
● mariadb.service - MariaDB 10.3.34 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-07-27 19:53:43 CST; 41s ago
       Docs: man:mysqld(8)
    Process: 24511 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
    Process: 24521 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 24528 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $? -eq 0 ]   && systemctl set-environment _WSREP_START_POSITION=$VA>
    Process: 24609 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 24611 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
   Main PID: 24578 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 31 (limit: 2273)
     Memory: 63.6M
     CGroup: /system.slice/mariadb.service
             └─24578 /usr/sbin/mysqld

Jul 27 19:53:43 ubuntu-test-server2 mysqld[24578]: 2022-07-27 19:53:43 0 [Note] /usr/sbin/mysqld (mysqld 10.3.34-MariaDB-0ubuntu0.20.04.1) starting as process 24578 ...
Jul 27 19:53:43 ubuntu-test-server2 systemd[1]: Started MariaDB 10.3.34 database server.
Jul 27 19:53:43 ubuntu-test-server2 /etc/mysql/debian-start[24613]: Upgrading MySQL tables if necessary.
Jul 27 19:53:43 ubuntu-test-server2 /etc/mysql/debian-start[24616]: Looking for 'mysql' as: /usr/bin/mysql
Jul 27 19:53:43 ubuntu-test-server2 /etc/mysql/debian-start[24616]: Looking for 'mysqlcheck' as: /usr/bin/mysqlcheck
Jul 27 19:53:43 ubuntu-test-server2 /etc/mysql/debian-start[24616]: This installation of MariaDB is already upgraded to 10.3.34-MariaDB.
Jul 27 19:53:43 ubuntu-test-server2 /etc/mysql/debian-start[24616]: There is no need to run mysql_upgrade again for 10.3.34-MariaDB.
Jul 27 19:53:43 ubuntu-test-server2 /etc/mysql/debian-start[24616]: You can use --force if you still want to run mysql_upgrade
Jul 27 19:53:43 ubuntu-test-server2 /etc/mysql/debian-start[24625]: Checking for insecure root accounts.
Jul 27 19:53:43 ubuntu-test-server2 /etc/mysql/debian-start[24629]: Triggering myisam-recover for all MyISAM tables and aria-recover for all Aria tables

如此就完成 MariaDB 的安裝工作了。