在樹莓派上配置MariaDB
前言
MariaDB是由原本開發MySQL的一些原始開發者領導,他們擔心Oracle收購MySQL后會有一些隱患。MariaDB與MySQL保持這高度兼容性,並使用了一個新的存儲引擎Aria。
安裝MriaDB
sudo apt-get install mariadb-server
靜靜的等待安裝完成即可,中間會詢問是否繼續,輸入Y繼續即可。安裝完成后就可以通過一下命令連接到MariaDB
sudo mysql
出現如下訊息表示已成功連接到MariaDB了
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 10.1.38-MariaDB-0+deb9u1 Raspbian 9.0
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)]>
配置密碼訪問
默認情況下MariaDB安裝好后都沒有配置訪問用戶的密碼,因此如果需要遠程連接時會無法連接。因此需要先對root用戶設置密碼。首先透過上一步中的命令連接至MariaDB,輸入如下語句進行密碼的修改
use mysql;
UPDATE user SET password=password('password') WHERE user='root';
UPDATE user SET plugin='mysql_native_password' WHERE user = 'root';
flush privileges;
exit
以上執行完成后,重啟服務
sudo systemctl restart mariadb
重啟完成后,試用密碼進行mariadb登錄,驗證是否修改成功
mysql -u root -p
輸入上面設置的密碼就可以看到第一步安裝完成登錄時一樣的畫面了。
配置MariaDB可遠程連接
MariaDB默認只監聽了127.0.0.1這個IP地址,這個時候是無法從外部連接到樹莓派上MariaDB。
先使用一下命令打開配置文件
nano /etc/mysql/mariadb.conf.d/50-server.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表示只監聽了127.0.0.1這個IP,將這一行的前面加上# 將這一行注釋起來,這樣MariaDB就監聽了所有的IP。
此時從外部的電腦連接MariaDB會提示"xxx.xxx.xxx is not allowed to connect to this MariaDB Server"。同樣使用上一步中的mysql命令連接到MariaDB,輸入如下命令:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
--格式如下
GRANT ALL PRIVILEGES ON *.* TO 'user'@'remoteip' IDENTIFIED BY 'password' WITH GRANT OPTION;
--更新權限
FLUSH PRIVILEGES;
至此可從外部連接到樹莓派上的MariaDB了