MySQL的最佳替代方案:MariaDB基本安裝說明

MariaDB_index

10幾年以來,MySQL一直都是網頁伺服器的資料庫解決方案,數以萬計的開源專案都以MySQL為預設的資料庫結構,當時的昇陽電腦也提供了許多的資源來經營,但是在2010年昇陽電腦被Oracle併購後,社群一般認為MySQL終將會逐漸走向封閉式軟體,所以MySQL的作者又主導了MySQL的分支計劃,也就是MariaDB的版本,目的在於提供完全開放源始碼授權的資料庫,除此之外也與MySQL完全相容,近幾年MariaDB的發展也提供了許多MySQL商業版本才有的功能,本篇的主要在說在如何在CentOS 7上建立基本的MariaDB運作環境,以及安裝一個基本的管理工具。(參考連結:MariaDB versus MySQL – Features)

 在「【伺服器】大改版的 CentOS 7 及基本安裝說明」一文中有提到,CentOS 7預設已經沒有提供MySQL的套件,並已採用MariaDB為預設的資料庫方案,在一開始的安裝中並沒有選到套件,請利用管理員的身份登入,安裝以下的套件:

#yum install mariadb mariadb-server
#systemctl start mariadb.service   #啟動服務
#systemctl enable mariadb.service  #設定開機啟動服務

接下來初始化MariaDB的環境,執行以下指令:

#mysql_secure_installation

會出現以下的訊息:

/usr/bin/mysql_secure_installation: line 379: find_mysql_client:命令找不到
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): #輸入目前MariaDB 的root密碼(第一次設定應該是空的,所以直接按Enter即可)
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 #是否要設定新的MariaDBroot密碼?在此是按Y
New password:              #設定第一次新的MariaDB root密碼
Re-enter new password:     #設定第二次
 Password updated successfully!
 Reloading 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 #是否要移除anonymous user的資料?預設是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 #設定是否讓root只能從localhost登入,不能從其他的網路登入!(預設是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.
Remove test database and access to it? [Y/n] Y #是否要移除test的資料庫?預設是移除
- 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.
Reload privilege tables now? [Y/n]  Y #是否要重新載入權限的table資訊?預設是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,指令和過去MySQL仍為一樣:

#mysql

若不打帳號密碼,會出現以下錯誤訊息:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

正確的指令:

#mysql -u root -p
Enter password:你剛剛設定的密碼

Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 18
Server version: 5.5.37-MariaDB MariaDB Server

Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

查詢一下資料庫:

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]>

指令和原有的MySQL都相同。

讓PHP支援MariaDB的功能,請安裝以下套件:

#yum install php-mysql

安裝MariaDB管理工具,一樣可以使用phpMyAdmin。

#yum install phpMyAdmin

在/etc/httpd/conf.d/phpMyAdmin.conf中註解以下的內容,允許從其他的網段登入phpMyAdmin:

[...]
Alias /phpMyAdmin /usr/share/phpMyAdmin
Alias /phpmyadmin /usr/share/phpMyAdmin

#<Directory /usr/share/phpMyAdmin/>
#   <IfModule mod_authz_core.c>
#     # Apache 2.4
#     <RequireAny>
#       Require ip 127.0.0.1
#       Require ip ::1
#     </RequireAny>
#   </IfModule>
#   <IfModule !mod_authz_core.c>
#     # Apache 2.2
#     Order Deny,Allow
#     Deny from All
#     Allow from 127.0.0.1
#     Allow from ::1
#   </IfModule>
#</Directory>

#加入以下內容:
<Directory /usr/share/phpMyAdmin/>
        Options none
        AllowOverride Limit
        Require all granted
</Directory>

[...]

接下來編輯/etc/phpMyAdmin/config.inc.php,把下行的內容由「cookie」改為「http」。

$cfg['Servers'][$i]['auth_type']     = 'http';

存檔後再重新啟動apache~

#systemctl restart httpd.service

連結http://yourip/phpMyadmin,網頁會跳出帳號密碼的設定畫面,輸入後就可登入!

MariaDB_phpmyadmin

成功!

參考資料:Install Apache, PHP And MySQL On CentOS 7 (LAMP)

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *