一、前提條件
你需要擁有 Ubuntu 服務器的管理權限,或者以 root 身份 或者以擁有 sudo 權限的用戶身份登錄系統(tǒng)。
二、在 Ubuntu 上安裝 MariaDB
Ubuntu 軟件源倉庫中的 MariaDB 最新版是 10.3,可以運行下面的命令進行安裝:
sudo apt update sudo apt install mariadb-server
安裝完成后 ,MariaDB 服務將會自動啟動。輸入以下命令驗證數(shù)據(jù)庫服務器是否正在運行:
sudo systemctl status mariadb
輸出結果將會顯示服務已經(jīng)啟用,并且正在運行:
...
三、維護 MariaDB
MariaDB 服務器有一個腳本叫做mysql_secure_installation,通過它你可以很容易提高數(shù)據(jù)庫服務器的安全性。
不帶參數(shù)運行腳本:
sudo mysql_secure_installation
根據(jù)腳本提示輸入 root 密碼:
Enter current password for root (enter for none):
由于沒有設置 root 密碼,所以這里僅僅輸入回車"Enter"即可。
接下來,會提示是否為 MySQL root 用戶設置密碼:
Set root password? [Y/n] n
輸入n。在 Ubuntu 上, MariaDB 用戶默認使用auth_socket進行鑒權。這個插件會檢查啟動客戶端的本地系統(tǒng)用戶是否和指定的 MariaDB 用戶名相匹配。
下一步,系統(tǒng)會要求移除匿名用戶,限制 root 用戶訪問本地機器,移除測試數(shù)據(jù)庫,并且重新加載權限表。如下所示,你只需要輸入Y:
Remove anonymous users? [Y/n] Y Disallow root login remotely? [Y/n] Y Remove test database and access to it? [Y/n] Y Reload privilege tables now? [Y/n] Y
四、以 root 身份登錄
如果想要在終端命令行和 MariaDB 服務器進行交互,可以使用mysql客戶端工具或者mariadb。這個工具被作為 MariaDB 服務器軟件包的依賴軟件被安裝。
這個auth_socket插件將會通過 Unix socket 文件驗證用戶來連接localhost。這就意味著你不能通過提供密碼來驗證 root。
想要以 root 用戶名登錄 MariaDB 服務器,需要輸入以下命令:
sudo mysql
執(zhí)行成功后會展示 MariaDB shell,如下所示:
Welcome to the MariaDB monitor. Commands end with ; or \\\\g. Your MariaDB connection id is 61 Server version: 10.3.22-MariaDB-1ubuntu1 Ubuntu 20.04 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)]> Bye
如果想使用第三方程序(例如 phpMyAdmin),以 root 身份登錄你的 MariaDB 服務器,有以下兩種方式可以選擇。
第一個是將鑒權方法從auth_socket修改為mysql_native_password。你可以通過運行下面的命令實現(xiàn):
ALTER USER \\\'root\\\'@\\\'localhost\\\' IDENTIFIED WITH mysql_native_password BY \\\'very_strong_password\\\'; FLUSH PRIVILEGES;
第二個推薦的方式就是創(chuàng)建一個管理員用戶,可以訪問所有的數(shù)據(jù)庫:
GRANT ALL PRIVILEGES ON *.* TO \\\'administrator\\\'@\\\'localhost\\\' IDENTIFIED BY \\\'very_strong_password\\\';
五、總結
至此,我們已經(jīng)向你展示了如何在 Ubuntu 20.04 上安裝 MariaDB。現(xiàn)在你的數(shù)據(jù)庫服務器應該已經(jīng)上線,并且正在運行了。
更多關于云服務器,域名注冊,虛擬主機的問題,請訪問三五互聯(lián)官網(wǎng):www.shinetop.cn