Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Ubuntu 16.04 安裝與設定 MariaDB 完整流程

MariaDB 是 MySQL 的社群分支,在 Ubuntu 16.04 的官方套件庫中已是預設的資料庫方案。本文從安裝、登入、編碼設定一路寫到遠端連線,把常見的坑一次填平。

一、安裝 MariaDB

# apt-get install mariadb-server python-pymysql

安裝過程中會要求你設定 root 帳號的密碼。若遇到套件找不到,先更新一下本機索引:

sudo apt-get update

二、從命令列登入

mysql -uroot -p

輸入密碼後看到如下歡迎訊息,就代表資料庫已經跑起來了:

root@zhai:~# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 42
Server version: 10.0.25-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
Ctrl-C -- exit!
Aborted

三、驗證安裝結果

除了看版本號,也可以直接查變數,確認伺服器狀態正常:

MariaDB [(none)]> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | utf8mb4                    |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

MariaDB [(none)]> show variables like 'collation%';
+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8mb4_general_ci |
| collation_database   | utf8mb4_general_ci |
| collation_server     | utf8mb4_general_ci |
+----------------------+--------------------+
3 rows in set (0.00 sec)

能正常列出上述結果,說明資料庫安裝成功。

四、服務的啟動與停止

$ sudo /etc/init.d/mysql start
$ sudo /etc/init.d/mysql stop

重啟服務:

# service mysql restart

五、設定預設字元集

編輯 /etc/mysql/my.cnf,在 [mysqld] 段落加入:

[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8

存檔後重啟 MySQL 讓設定生效。

六、用 mysql_secure_installation 加固

執行:

mysql_secure_installation

這個互動腳本會依序問你幾個問題,正式環境建議全部照建議值處理:

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Change the root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!

Remove anonymous users? [Y/n] y ... Success!

Disallow root login remotely? [Y/n] y ... Success!

Remove test database and access to it? [Y/n] y
- Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
... Failed! Not critical, keep moving...
- Removing privileges on test database... ... Success!

Reload privilege tables now? [Y/n] y ... Success!

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

其中「test 資料庫不存在」的錯誤並不影響結果,可以忽略。

七、開放遠端存取

7.1 防火牆

如果 Ubuntu 上有啟用防火牆或 iptables 規則,請自行放行 3306 埠。

7.2 檢查 3306 埠的監聽範圍

~# netstat -an | grep 3306
tcp  0  0  127.0.0.1:3306  0.0.0.0:*  LISTEN

結果顯示只綁在 127.0.0.1,所以其他主機連不進來。解法是修改 /etc/mysql/my.cnf,找到這一段:

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1

把 bind-address 整行註解掉(或改成實際的 IP),建議直接註解。重啟後再確認:

~# netstat -an | grep 3306
tcp  0  0  0.0.0.0:3306  0.0.0.0:*  LISTEN

7.3 為使用者授權

此時從別台機器測試,可能仍會看到:

~# mysql -h 192.168.0.101 -u root -p
Enter password:
ERROR 1130 (00000): Host 'Ubuntu-Fvlo.Server' is not allowed to connect to this MySQL server

原因是權限還沒開放給遠端來源。登入伺服器後用 GRANT 授權:

mysql> grant all on *.* to 'root'@'%' identified by '你的密碼';
mysql> flush privileges;

完成後再用 mysql -h 連一次,能進入提示符就代表成功;保險一點,可以從真正的遠端主機再測一遍。

Copyright © Linux教程網 All Rights Reserved