2014年9月5日 星期五

RHEL設定ISO掛載及使用YUM本地ISO的步驟 (以RHEL6.5為例)

下午6:24:00 Posted by Amos , No comments

掛載系統鏡像文件

1. 上傳 rhel-server-6.5-x86_64-dvd.iso到  /usr/local/src
RHEL6.5: wget http://ftp.okhysing.is/redhat/6.5/isos/x86_64/rhel-server-6.5-x86_64-dvd.iso
RHEL7.0 : wget http://ftp.okhysing.is/redhat/7/isos/x86_64/rhel-server-7.0-x86_64-dvd.iso

2. mkdir /media/cdrom #新建鏡像文件掛載目錄
3. cd /usr/local/src  #進入系統鏡像文件存放目錄
4. mount -t iso9660 -o loop /usr/local/src/rhel-server-6.5-x86_64-dvd.iso  /media/cdrom
5. cd  /media/cdrom  #進入掛載目錄,使用ls命令可以看到ISO檔案

設置開機自動掛載系統鏡像文件

vi /etc/fstab   #添加以下代碼。實現開機自動掛載
/usr/local/src/rhel-server-6.5-x86_64-dvd.iso  /media/cdrom   iso9660    defaults,ro,loop  0 0

配置本地yum源

cd /etc/yum.repos.d/     #進入yum配置目錄
vi  rhel-media.repo     #編輯配置文件,添加以下內容

[rhel-media]
name=Red Hat Enterprise Linux 6.5
baseurl=file:///media/cdrom
enabled=1
gpgcheck=1
gpgkey=file:///media/cdrom/RPM-GPG-KEY-redhat-release

使用yum命令自動安裝軟件

yum clean all   #清除yum緩存
yum makecache   #暫存本地yum源中的軟件包信息

安裝subscription-manager-gui套件

yum install -y subscription-manager-gui

註冊RHEL

1. 使用GUI註冊: 執行 rhn_register 指令可以進行GUI註冊
2. 使用指令註冊:
2.1 Automatically Subscribing While Registering [自動啟用]
subscription-manager register --username admin-example --password secret --autosubscribe

2.2 Registering a System to the Customer Portal
subscription-manager register --username admin-example --password secret

如何讓AlertDialog 按下確認鍵時, 不會消失 (該功能可做為Dialog資料驗證使用)

上午10:52:00 Posted by Amos , No comments

不關閉訊息視窗程式碼

public static void dialogNoDismiss(DialogInterface dialog) {
 try {
  Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
  field.setAccessible(true);
  field.set(dialog, false);
 }
 catch (Exception e) {
 }
}

關閉訊息視窗程式碼

public static void dialogDismiss(DialogInterface dialog) {
 try {
  Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
  field.setAccessible(true);
  field.set(dialog, true);
 }
 catch (Exception e) {
 }
}

android EditText 使用問題及解決方案

上午10:45:00 Posted by Amos , No comments

如何 EditText 如何取得焦點

方法一: 在layout文件中針對EditText增加 <requestFocus /> 語法即可

<EditText
       android:id="@+id/EditTextName"
       android:layout_width="fill_parent",
       android:layout_height="wrap_content"
       android:hint=""
       android:inputType="text"
       android:singleLine="true" />
<requestFocus />

方法二: 透過程式方式取得EditText焦點

editTextHeight.requestFocus();

讓 EditText 顯示錯誤訊息

在程式中撰寫以下語法可顯示錯誤資訊
editTextAge.setError("XXXXXX");
但因Android4.X版本後, 無法顯示錯誤訊息的字, 因字的顏色與背景顏色相同, 故需要在/res/values/style.xml中Theme增加以下語法, 才可以正常顯示
<item name="android:textColorPrimaryInverse">@android:color/primary_text_light</item>

Centos7 / RHEL 7 修改預設開機的模式

上午10:16:00 Posted by Amos , No comments

使用指令切換文字模式或圖形模式

systemctl isolate multi-user.target //切換到模式3,該命令對下次啟動無影響,等價於telinit 3 
systemctl isolate runlevel3.target //切換到模式3,該命令對下次啟動無影響,等價於telinit 3 
systemctl isolate graphical.target //切換到模式5,該命令對下次啟動無影響,等價於telinit 5 
systemctl isolate runlevel5.target //切換到模式5,該命令對下次啟動無影響,等價於telinit 5 

執行systemctl命令,設置啟動時預設進入文字模式或圖形模式。

# systemctl -f enable multi-user.target //文字模式 
# systemctl -f enable graphical.target //圖形模式 

執行以下命令,設置啟動時預設進入文字模式或圖形模式

開機啟動之預設檔案為default.target,預設連結到graphical.target(大致相當於原來的運行級別5)。
ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target //文字模式 
ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target //圖形模式 

2014年7月30日 星期三

Centos6.5 中安裝 MariaDB 5.5 Galera Server (叢集版本)

下午3:35:00 Posted by Amos , , No comments

簡介

本文概述簡單的步驟,教導如何建置 Master-Master Replication 叢集(Galera Cluster)架構的MariaDB(請參考下圖),並安裝phpmyadmin管理MariaDB資 料庫。

Galera Cluster 介紹

為什麼要選擇 Galera Cluster Server,它有什麼優點及功能呢?MySQL/Galera 是一套可以同步多台 MySQL/InnoDB 機器的叢集系統,底下可以列出功能。
  • 同步複製資料
  • 可讀取和寫入叢集系統內任一節點
  • 自動偵測節點錯誤,如果有節點當機,則叢集系統自動移除該節點
  • 可任意擴充節點
  • 採用 row level 方式來平行複製資料
從上面功能看來,我們可以平行任意擴充節點,動態增加伺服器到叢集系統,要做到上面功能,就是利用 Galera library 來做到同步資料處理,同步的詳細細節,可以參考 Galera library 連結。這邊就不再多描述了。

環境介紹及前置作業

作業系統版本 : Centos 6.5 (64位元)

MariaDB資料庫版本 : 5.5.38 (Galera-Server 64位元)

PHP版本 : 5.4.X (REMI版本)

安裝編輯及wget套件

若於Centos中已有安裝vim (編輯工具)和wget(抓檔案工具)時,可以忽略該步驟
yum -y install wget vim

關閉SELinux

編輯 /etc/sysconfig/selinux檔案,將SELINUX=enforcing 改成 SELINUX=disabled,重新開機。
# This file controls the state of SELinux on the system
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,>
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

設定Open File個數

編輯 /etc/security/limits.conf檔案,設定Open File個數
* hard nofile 65535
* soft nofile 65535

開啟防火牆

編輯 /etc/sysconfig/iptables檔案,開啟8080防火牆(用於node.js的服務),設定完成後請重新啟動防火牆。
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 4567 -j ACCEPT  使用於Galera
-A INPUT -m state --state NEW -m tcp -p tcp --dport 4444 -j ACCEPT  使用於同步資料
-A INPUT -m state --state NEW -m udp -p udp --dport 4444 -j ACCEPT  使用於同步資料
/etc/init.d/iptables restart

安裝及設定Apache

輸入 yum install httpd* ,安裝Apacher Server相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum -y install httpd*
chkconfig --level 345 httpd on
chkconfig --list httpd

為了安全性考量,設定用戶無法瀏覽Apache預設的首頁,請參考以下步驟進行設定(每行前面都加入#符號,共計4行)。
vi /etc/httpd/conf.d/welcome.conf 

#<LocationMatch "^/+$">
    #Options -Indexes
    #ErrorDocument 403 /error/noindex.html
#</LocationMatch>

啟用Apache時若有出現Could not reliably determine the server's fully qualified domain name訊息,請修改如下之檔案內容
vi /etc/httpd/conf/httpd.conf
ServerName  localhost:80    #新增加這一項參數

重新啟動Apache Server。
/etc/init.d/httpd restart

設定php.ini檔案

編輯 /etc/php.ini檔案,設定以下參數
vi /etc/php.ini 
修改為: max_execution_time = 300 修改為: max_input_time = 600 修改為: post_max_size = 800M 修改為: upload_max_filesize = 800M 修改為: default_charset="utf8" 修改為: default_socket_timeout = 300 修改為: short_open_tag = On 修改為: date.timezone = "Asia/Taipei"

安裝及設定MariaDB資料庫

安裝 EPEL 套件

因安裝MariaDB-Galera需要socat套件,故需要先安裝EPEL套件來源檔,請參考以下步驟進行設定。
rpm -ivh http://mirror01.idc.hinet.net/EPEL/6/x86_64/epel-release-6-8.noarch.rpm

設定MariaDB的來源碼

編輯 /etc/yum.repos.d/mariadb.repo檔案,若是32位元系統請根據以下方式設定
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-x86
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

若為64位元系統請根據以下方式設定
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

安裝MariaDB

安裝MariaDB相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum install MariaDB-Galera-server MariaDB-client galera
service mysql start

駐: 安裝後啟動MariaDB,MariaDB的服務稱為mysql,與MySQL服務名稱(mysqld)不同

設定MariaDB自動啟動

chkconfig --level 345 mysql on
chkconfig --list mysql

MariaDB 設定檔的位置位於/etc/my.cnf.d

my.cnf.d目錄中有兩個檔案,mysql-clients.cnf和server.cnf檔案,可將設定檔之範例檔案拷貝至my.cnf.d/目錄中,範例檔位於 /usr/share/mysql/ 目錄中,可根據主機的記憶體規格選擇合適的設定檔
  • my-innodb-heavy-4G.cnf: 適合大於 4GB RAM的伺服器使用。
  • my-huge.cnf: 適合 1GB – 2GB RAM的伺服器使用。
  • my-large.cnf: 適合 512MB RAM的伺服器使用。
  • my-medium.cnf: 適合64MB~512 RAM 的伺服器使用。
  • my-small.cnf: 適合記憶體小於 64MB的伺服器。

拷貝範例設定檔至/etc/my.cnf.d/目錄中,命名為mysql-clients.cnf
cp /usr/share/mysql/my-huge.cnf /etc/my.cnf.d/mysql-clients.cnf
service mysql restart

設定MariaDB root帳號之密碼

/usr/bin/mysqladmin -u root password '新密碼'
重新啟動 MariaDB Server
/etc/init.d/mysql restart

啟動MariaDB並配置Galera同步配置

建立 cluster 使用者,密碼為p@ssw0rd,針對 % 跟 localhost 同時建立,帳號密碼配置(每一台資料庫中都建立同步用之帳號)
GRANT USAGE ON *.* to cluster@'%' IDENTIFIED BY 'p@ssw0rd';
GRANT ALL PRIVILEGES on *.* to cluster@'%';
GRANT USAGE ON *.* to cluster@'localhost' IDENTIFIED BY 'p@ssw0rd';
GRANT ALL PRIVILEGES on *.* to cluster@'localhost';
FLUSH PRIVILEGES;

建立Galera設定檔 wsrep.cnf

cp /usr/share/mysql/wsrep.cnf /etc/my.cnf.d/
於第一台主機(192.168.1.100)中編輯 /etc/my.cnf.d/wsrep.cnf 檔案,輸入以下設定
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_sst_auth=cluster:p@ssw0rd

# Galera Cluster Configuration
wsrep_cluster_name="test_cluster"
wsrep_cluster_address="gcomm://第二台主機IP"

# Galera Node Configuration
wsrep_node_address="192.168.1.100"
wsrep_node_name="this_node_name"

重新啟動 mysql 後並執行 netstat -na 看到多一個 listen 4567 port
service mysql start --wsrep-new-cluster
netstat -na
LISTEN      
tcp        0      0 0.0.0.0:4567                0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:3306                0.0.0.0:*   
於第二台主機(192.168.1.101)中編輯 /etc/my.cnf.d/wsrep.cnf 檔案,將 cluster address 設定為第一台主機的IP,輸入以下設定
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_sst_auth=cluster:p@ssw0rd

# Galera Cluster Configuration
wsrep_cluster_name="test_cluster"
wsrep_cluster_address="gcomm://第一台主機IP"

# Galera Node Configuration
wsrep_node_address="192.168.1.101"
wsrep_node_name="this_node_name"
重新啟動 mysql 後並執行 netstat -na 看到多一個 listen 4567 port
/etc/init.d/mysql restart

於第一台和第二台主機中執行以下指令,確認Cluster設定完成並啟用

$ mysql -e "SHOW STATUS LIKE 'wsrep%';"

可看到如下結果
+------------------------------+--------------------------------------+
| Variable_name                | Value                                |
+------------------------------+--------------------------------------+
| wsrep_local_state_uuid       | b5fc5a4a-1782-11e4-8689-b3cbc5faf010 |
| wsrep_protocol_version       | 5                                    |
| wsrep_last_committed         | 0                                    |
| wsrep_replicated             | 0                                    |
| wsrep_replicated_bytes       | 0                                    |
| wsrep_repl_keys              | 0                                    |
| wsrep_repl_keys_bytes        | 0                                    |
| wsrep_repl_data_bytes        | 0                                    |
| wsrep_repl_other_bytes       | 0                                    |
| wsrep_received               | 2                                    |
| wsrep_received_bytes         | 216                                  |
| wsrep_local_commits          | 0                                    |
| wsrep_local_cert_failures    | 0                                    |
| wsrep_local_replays          | 0                                    |
| wsrep_local_send_queue       | 0                                    |
| wsrep_local_send_queue_avg   | 0.000000                             |
| wsrep_local_recv_queue       | 0                                    |
| wsrep_local_recv_queue_avg   | 0.000000                             |
| wsrep_local_cached_downto    | 18446744073709551615                 |
| wsrep_flow_control_paused_ns | 0                                    |
| wsrep_flow_control_paused    | 0.000000                             |
| wsrep_flow_control_sent      | 0                                    |
| wsrep_flow_control_recv      | 0                                    |
| wsrep_cert_deps_distance     | 0.000000                             |
| wsrep_apply_oooe             | 0.000000                             |
| wsrep_apply_oool             | 0.000000                             |
| wsrep_apply_window           | 0.000000                             |
| wsrep_commit_oooe            | 0.000000                             |
| wsrep_commit_oool            | 0.000000                             |
| wsrep_commit_window          | 0.000000                             |
| wsrep_local_state            | 4                                    |
| wsrep_local_state_comment    | Synced                               |
| wsrep_cert_index_size        | 0                                    |
| wsrep_causal_reads           | 0                                    |
| wsrep_cert_interval          | 0.000000                             |
| wsrep_incoming_addresses     | 210.71.253.55:3306,210.61.8.40:3306  |
| wsrep_cluster_conf_id        | 4                                    |
| wsrep_cluster_size           | 2                                    |
| wsrep_cluster_state_uuid     | b5fc5a4a-1782-11e4-8689-b3cbc5faf010 |
| wsrep_cluster_status         | Primary                              |
| wsrep_connected              | ON                                   |
| wsrep_local_bf_aborts        | 0                                    |
| wsrep_local_index            | 0                                    |
| wsrep_provider_name          | Galera                               |
| wsrep_provider_vendor        | Codership Oy     |
| wsrep_provider_version       | 25.3.5(rXXXX)                        |
| wsrep_ready                  | ON                                   |
| wsrep_thread_count           | 2                                    |
+------------------------------+--------------------------------------+

測試 Master-Master Replication 架構

登入其中一台主機,建立新的資料庫(playground)與表格(equipment)
mysql -u root -p
CREATE DATABASE playground;
use playground;
CREATE TABLE equipment ( id INT NOT NULL AUTO_INCREMENT, type VARCHAR(50), quant INT, color VARCHAR(25), PRIMARY KEY(id));
輸入資料至equipment表格中
INSERT INTO equipment (type, quant, color) VALUES ("slide", 2, "blue");
再登入第二台主機查看是否有新增playground資料庫及equipment資料表,並新增一筆資料至equipment資料表
use playground;
SELECT * FROM playground.equipment;
INSERT INTO equipment (type, quant, color) VALUES ("swing", 10, "yellow");
再登入至第一台主機查看是否equipment資料表有新增一筆資料
use playground;
SELECT * FROM playground.equipment;

增加新的Node至MariaDB Cluster

可任意新增多台 Node 到 Cluster 叢集系統裡,設置過程如下
  • 安裝 MariaDB Server
  • 安裝 Galera Library
  • 設定wsrep.cnf檔案內容

動態設定 gcomm://參數

若新增一台新的 Node,就必須修改其它 Node 的 gcomm:// 設定,並且重新啟動 MariaDB 服務,這樣會有DownTime時間影響服務,故可透過線上修改 GLOBAL wsrep_cluster_address 的參數值達到不須重新啟動服務,請參考如下設定
mysql -e "SHOW VARIABLES LIKE 'wsrep_cluster_address';"
mysql -u root -p
SET GLOBAL wsrep_cluster_address='gcomm://設定IP';

參考資料

安裝及設定phpMyAdmin

下載phpMyAdmin-4.0.10.1-all-languages.tar.gz並安裝

解壓縮phpMyAdmin-4.0.10.1-all-languages
tar -zxvf phpMyAdmin-4.0.10.1-all-languages.tar.gz

搬移 phpMyAdmin-4.0.10.1-all-languages目錄到/usr/share,並修改名稱為phpMyAdmin-4.0.10.1
mv phpMyAdmin-4.0.10.1-all-languages /usr/share/phpMyAdmin-4.0.10.1

複製範本設定檔 config.sample.inc.php 並重新命名為 config.inc.php
cd /usr/share/phpMyAdmin-4.0.10.1
cp config.sample.inc.php config.inc.php

修改 phpMyAdmin 的認證方式,請編輯config.inc.php檔案,重新啟動 Apache
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookies';
/etc/init.d/httpd restart

修改config.inc.php檔案的權限
chmod 644 config.inc.php

移除setup安裝資料夾
mv setup setup.org

設定phpMyAdmin網站別名(可視情況設定)

編輯 /etc/httpd/conf/httpd.conf 檔案

#Set Virtual Host
Alias /mysqladm "/usr/share/phpMyAdmin-4.0.10.1"
<Directory "/usr/share/phpMyAdmin-4.0.10.1">
  Options -Indexes FollowSymLinks
  AllowOverride None
  Order allow,deny
  Allow from all
</Directory>

開啟瀏覽器輸入http://您的IP/mysqladm將會看到phpMyAdmin登入網頁,輸入使用者名稱及密碼後即可開始管理MySQL資料庫

Troubleshooting

假設您有遇到 Native table 'performance_schema'.'???' has the wrong structure 等的錯誤訊息LOG (位於/var/lib/mysql/) 時, 表示資料表因為資料庫升版造成異常,可使用以下的指令進行修復
mysql_upgrade -u root -p

2014年7月28日 星期一

Centos6.5 中安裝MariaDB 10.0.12 資料庫與phpMyAdmin軟體

下午1:50:00 Posted by Amos , , No comments

簡介

本文概述簡單的步驟,教導如何建置LAMP環境(Apache、PHP、MariaDB),並安裝phpmyadmin管理MariaDB資料庫。

環境介紹及前置作業

作業系統版本 : Centos 6.5 (64位元)

MariaDB資料庫版本 : 10.0.12 (64位元)

PHP版本 : 5.4.X (REMI版本)

安裝編輯及wget套件

若於Centos中已有安裝vim (編輯工具)和wget(抓檔案工具)時,可以忽略該步驟
yum -y install wget vim

關閉SELinux

編輯 /etc/sysconfig/selinux檔案,將SELINUX=enforcing 改成 SELINUX=disabled,重新開機。
# This file controls the state of SELinux on the system
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,>
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

設定Open File個數

編輯 /etc/security/limits.conf檔案,設定Open File個數
* hard nofile 65535
* soft nofile 65535

開啟防火牆

編輯 /etc/sysconfig/iptables檔案,開啟8080防火牆(用於node.js的服務),設定完成後請重新啟動防火牆。
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
/etc/init.d/iptables restart 

安裝及設定Apache

輸入 yum install httpd* ,安裝Apacher Server相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum -y install httpd*
chkconfig --level 345 httpd on
chkconfig --list httpd

為了安全性考量,設定用戶無法瀏覽Apache預設的首頁,請參考以下步驟進行設定(每行前面都加入#符號,共計4行)。
vi /etc/httpd/conf.d/welcome.conf 

#<LocationMatch "^/+$">
    #Options -Indexes
    #ErrorDocument 403 /error/noindex.html
#</LocationMatch>

啟用Apache時若有出現Could not reliably determine the server's fully qualified domain name訊息,請修改如下之檔案內容
vi /etc/httpd/conf/httpd.conf
ServerName  localhost:80    #新增加這一項參數

安裝及設定php

安裝php套件

安裝PHP相關套件(套件來源使用remi與EPEL),並設定於開機時自動啟用,請參考以下步驟進行設定。
rpm -ivh http://mirror01.idc.hinet.net/EPEL/6/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

yum install --enablerepo=remi php
yum install --enablerepo=remi php-mysql
yum install --enablerepo=remi php-mbstring
yum install --enablerepo=remi php-mcrypt

重新啟動Apache Server。
/etc/init.d/httpd restart

設定php.ini檔案

編輯 /etc/php.ini檔案,設定以下參數
vi /etc/php.ini 
修改為: max_execution_time = 300 修改為: max_input_time = 600 修改為: post_max_size = 800M 修改為: upload_max_filesize = 800M 修改為: default_charset="utf8" 修改為: default_socket_timeout = 300 修改為: short_open_tag = On 修改為: date.timezone = "Asia/Taipei"

安裝及設定MariaDB資料庫

設定MariaDB的來源碼

編輯 /etc/yum.repos.d/mariadb.repo檔案,若是32位元系統請根據以下方式設定
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.0/centos6-x86
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

若為64位元系統請根據以下方式設定
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.0/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

安裝MariaDB

安裝MariaDB相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum install MariaDB-devel MariaDB-client MariaDB-server  MariaDB-common MariaDB-shared MariaDB-devel MariaDB-compat

service mysql start

駐: 安裝後啟動MariaDB,MariaDB的服務稱為mysql,與MySQL服務名稱(mysqld)不同

設定MariaDB自動啟動

chkconfig --level 345 mysql on
chkconfig --list mysql

MariaDB 設定檔的位置位於/etc/my.cnf.d

my.cnf.d目錄中有兩個檔案,mysql-clients.cnf和server.cnf檔案,可將設定檔之範例檔案拷貝至my.cnf.d/目錄中,範例檔位於 /usr/share/mysql/ 目錄中,可根據主機的記憶體規格選擇合適的設定檔
  • my-innodb-heavy-4G.cnf: 適合大於 4GB RAM的伺服器使用。
  • my-huge.cnf: 適合 1GB – 2GB RAM的伺服器使用。
  • my-large.cnf: 適合 512MB RAM的伺服器使用。
  • my-medium.cnf: 適合64MB~512 RAM 的伺服器使用。
  • my-small.cnf: 適合記憶體小於 64MB的伺服器。

拷貝範例設定檔至/etc/my.cnf.d/目錄中,命名為mysql-clients.cnf
cp /usr/share/mysql/my-huge.cnf /etc/my.cnf.d/mysql-clients.cnf
service mysql restart

設定MariaDB root帳號之密碼

/usr/bin/mysqladmin -u root password '新密碼'

修改/etc/my.cnf.d/mysql-clients.cnf檔案,設定資料庫預設的字元碼為UTF8

init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

重新啟動 MariaDB Server
/etc/init.d/mysql restart

安裝及設定phpMyAdmin

下載phpMyAdmin-4.0.10.1-all-languages.tar.gz並安裝

解壓縮phpMyAdmin-4.0.10.1-all-languages
tar -zxvf phpMyAdmin-4.0.10.1-all-languages.tar.gz

搬移 phpMyAdmin-4.0.10.1-all-languages目錄到/usr/share,並修改名稱為phpMyAdmin-4.0.10.1
mv phpMyAdmin-4.0.10.1-all-languages /usr/share/phpMyAdmin-4.0.10.1

複製範本設定檔 config.sample.inc.php 並重新命名為 config.inc.php
cd /usr/share/phpMyAdmin-4.0.10.1
cp config.sample.inc.php config.inc.php

修改 phpMyAdmin 的認證方式,請編輯config.inc.php檔案,重新啟動 Apache
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookies';
/etc/init.d/httpd restart

修改config.inc.php檔案的權限
chmod 644 config.inc.php

移除setup安裝資料夾
mv setup setup.org

設定phpMyAdmin網站別名(可視情況設定)

編輯 /etc/httpd/conf/httpd.conf 檔案

#Set Virtual Host
Alias /mysqladm "/usr/share/phpMyAdmin-4.0.10.1"
<Directory "/usr/share/phpMyAdmin-4.0.10.1">
  Options -Indexes FollowSymLinks
  AllowOverride None
  Order allow,deny
  Allow from all
</Directory>

開啟瀏覽器輸入http://您的IP/mysqladm將會看到phpMyAdmin登入網頁,輸入使用者名稱及密碼後即可開始管理MySQL資料庫

Troubleshooting

假設您有遇到 Native table 'performance_schema'.'???' has the wrong structure 等的錯誤訊息LOG (位於/var/lib/mysql/) 時, 表示資料表因為資料庫升版造成異常,可使用以下的指令進行修復
mysql_upgrade -u root -p

MariaDB 參考資料

2014年7月27日 星期日

Centos6.5 中安裝MariaDB 5.5 資料庫與phpMyAdmin軟體

上午11:54:00 Posted by Amos , , No comments

簡介

本文概述簡單的步驟,教導如何建置LAMP環境(Apache、PHP、MariaDB),並安裝phpmyadmin管理MariaDB資料庫。

環境介紹及前置作業

作業系統版本 : Centos 6.5 (64位元)

MariaDB資料庫版本 : 5.5.38 (64位元)

PHP版本 : 5.4.X (REMI版本)

安裝編輯及wget套件

若於Centos中已有安裝vim (編輯工具)和wget(抓檔案工具)時,可以忽略該步驟
yum -y install wget vim

關閉SELinux

編輯 /etc/sysconfig/selinux檔案,將SELINUX=enforcing 改成 SELINUX=disabled,重新開機。
# This file controls the state of SELinux on the system
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,>
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

設定Open File個數

編輯 /etc/security/limits.conf檔案,設定Open File個數
* hard nofile 65535
* soft nofile 65535

開啟防火牆

編輯 /etc/sysconfig/iptables檔案,開啟8080防火牆(用於node.js的服務),設定完成後請重新啟動防火牆。
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
/etc/init.d/iptables restart 

安裝及設定Apache

輸入 yum install httpd* ,安裝Apacher Server相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum -y install httpd*
chkconfig --level 345 httpd on
chkconfig --list httpd

為了安全性考量,設定用戶無法瀏覽Apache預設的首頁,請參考以下步驟進行設定(每行前面都加入#符號,共計4行)。
vi /etc/httpd/conf.d/welcome.conf 

#<LocationMatch "^/+$">
    #Options -Indexes
    #ErrorDocument 403 /error/noindex.html
#</LocationMatch>

啟用Apache時若有出現Could not reliably determine the server's fully qualified domain name訊息,請修改如下之檔案內容
vi /etc/httpd/conf/httpd.conf
ServerName  localhost:80    #新增加這一項參數

安裝及設定php

安裝php套件

安裝PHP相關套件(套件來源使用remi與EPEL),並設定於開機時自動啟用,請參考以下步驟進行設定。
rpm -ivh http://mirror01.idc.hinet.net/EPEL/6/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

yum install --enablerepo=remi php
yum install --enablerepo=remi php-mysql
yum install --enablerepo=remi php-mbstring
yum install --enablerepo=remi php-mcrypt

重新啟動Apache Server。
/etc/init.d/httpd restart

設定php.ini檔案

編輯 /etc/php.ini檔案,設定以下參數
vi /etc/php.ini 
修改為: max_execution_time = 300 修改為: max_input_time = 600 修改為: post_max_size = 800M 修改為: upload_max_filesize = 800M 修改為: default_charset="utf8" 修改為: default_socket_timeout = 300 修改為: short_open_tag = On 修改為: date.timezone = "Asia/Taipei"

安裝及設定MariaDB資料庫

設定MariaDB的來源碼

編輯 /etc/yum.repos.d/mariadb.repo檔案,若是32位元系統請根據以下方式設定
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-x86
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

若為64位元系統請根據以下方式設定
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

安裝MariaDB

安裝MariaDB相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum install MariaDB-devel MariaDB-client MariaDB-server  MariaDB-common MariaDB-shared MariaDB-devel

service mysql start

駐: 安裝後啟動MariaDB,MariaDB的服務稱為mysql,與MySQL服務名稱(mysqld)不同

設定MariaDB自動啟動

chkconfig --level 345 mysql on
chkconfig --list mysql

MariaDB 設定檔的位置位於/etc/my.cnf.d

my.cnf.d目錄中有兩個檔案,mysql-clients.cnf和server.cnf檔案,可將設定檔之範例檔案拷貝至my.cnf.d/目錄中,範例檔位於 /usr/share/mysql/ 目錄中,可根據主機的記憶體規格選擇合適的設定檔
  • my-innodb-heavy-4G.cnf: 適合大於 4GB RAM的伺服器使用。
  • my-huge.cnf: 適合 1GB – 2GB RAM的伺服器使用。
  • my-large.cnf: 適合 512MB RAM的伺服器使用。
  • my-medium.cnf: 適合64MB~512 RAM 的伺服器使用。
  • my-small.cnf: 適合記憶體小於 64MB的伺服器。

拷貝範例設定檔至/etc/my.cnf.d/目錄中,命名為mysql-clients.cnf
cp /usr/share/mysql/my-huge.cnf /etc/my.cnf.d/mysql-clients.cnf
service mysql restart

修改/etc/my.cnf.d/mysql-clients.cnf檔案,設定資料庫預設的字元碼為UTF8

init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

重新啟動 MariaDB Server
/etc/init.d/mysql restart

設定MariaDB root帳號之密碼

/usr/bin/mysqladmin -u root password '新密碼'

安裝及設定phpMyAdmin

下載phpMyAdmin-4.0.10.1-all-languages.tar.gz並安裝

解壓縮phpMyAdmin-4.0.10.1-all-languages
tar -zxvf phpMyAdmin-4.0.10.1-all-languages.tar.gz

搬移 phpMyAdmin-4.0.10.1-all-languages目錄到/usr/share,並修改名稱為phpMyAdmin-4.0.10.1
mv phpMyAdmin-4.0.10.1-all-languages /usr/share/phpMyAdmin-4.0.10.1

複製範本設定檔 config.sample.inc.php 並重新命名為 config.inc.php
cd /usr/share/phpMyAdmin-4.0.10.1
cp config.sample.inc.php config.inc.php

修改 phpMyAdmin 的認證方式,請編輯config.inc.php檔案,重新啟動 Apache
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookies';
/etc/init.d/httpd restart

修改config.inc.php檔案的權限
chmod 644 config.inc.php

移除setup安裝資料夾
mv setup setup.org

設定phpMyAdmin網站別名(可視情況設定)

編輯 /etc/httpd/conf/httpd.conf 檔案

#Set Virtual Host
Alias /mysqladm "/usr/share/phpMyAdmin-4.0.10.1"
<Directory "/usr/share/phpMyAdmin-4.0.10.1">
  Options -Indexes FollowSymLinks
  AllowOverride None
  Order allow,deny
  Allow from all
</Directory>

開啟瀏覽器輸入http://您的IP/mysqladm將會看到phpMyAdmin登入網頁,輸入使用者名稱及密碼後即可開始管理MySQL資料庫