2016年10月27日 星期四

在CentOS 7 架設互為備援的MariaDB設定

Azure上新增的VM選CentOS 7安裝完畢以後~

安裝MariaDB...就參閱之前的文章步驟不變。

設定DB
vim /etc/my.cnf.d/server.cnf

[mysqld]
# Replication Setting
server-id=1  <- 為數字,每台不重複即可
log-bin=mysql-bin

新增同步帳號
CREATE USER 'backup'@'Remote_IP_Addr' IDENTIFIED BY 'key in password';
GRANT REPLICATION SLAVE ON *.* TO 'backup'@'Remote_IP_Addr';
FLUSH PRIVILEGES;

接著做一次 MariaDB 異地備援不同步時的處理,只需要DB SYNC一次即可,任選一邊當MASTER。

兩邊均設定主從
STOP SLAVE;
CHANGE MASTER TO MASTER_HOST='Remote_IP_Addr', MASTER_PORT=3306, MASTER_USER='backup', MASTER_PASSWORD='key in password';
START SLAVE;

檢查雙邊機制是否正常運作,若沒有ERROR就是了。
show slave status \G;



MariaDB : Failed Registration of InnoDB as a Storage Engine

昨晚DB顯示GG了,早上追了一下貌似自建Function沒設Read Only造成Log塞爆導致Slave DB LOG同步異常,然後自我修復重啟失敗所致。

做個MEMO囉...不經一事不長一智。
ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL,
or READS SQL DATA in its declaration and binary logging is enabled
(you *might* want to use the less safe log_bin_trust_function_creators
variable)
參考官方文件中的描述,在建立FUNCTION時,要增加READS SQL DATA否則在Recovery或Replicatioin時會出現unsafe的error message,這message多了就會塞爆LOG...
CREATE FUNCTION f1(i INT)
RETURNS INT
DETERMINISTIC
READS SQL DATA
BEGIN
  RETURN i;
END;

無法啟動的LOG類似以下這個...只需要把/var/lib/mysql/下的ib_logfileXX跟ibdataX搬移開來(要移除也行~不過個人習慣是下MV指另,RM下好離手就回不來了。

101003 18:37:27 [Note] Plugin 'FEDERATED' is disabled.
InnoDB: Error: log file .\ib_logfile0 is of different size 0 40894464 bytes
InnoDB: than specified in the .cnf file 0 536870912 bytes!
101003 18:37:27 [ERROR] Plugin 'InnoDB' init function returned error.
101003 18:37:27 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
101003 18:37:27 [ERROR] Unknown/unsupported table type: InnoDB
101003 18:37:27 [ERROR] Aborting

接者下service mysql start 或 systemctl start mariadb.service 啟動即可。

  • Slave DB 出現 Could not initialize master info structure 時,下reset slave; 清一下LOG
  • 出現 Error : Tablespace for table '`database`.`table-temp`' exists. Please DISCARD the tablespace before IMPORT.
    這個卡麻煩~畢竟DB不是都自己在處理的,其他人刪除Table沒下Drop指令而是使用...時,就會造成此錯誤,請將該table-temp.idb搬移出目錄即可。
  •  







2016年10月19日 星期三

在 Windows 7 上安裝 Apache + PHP +MySQL

務必注意作業系統是32還是64位元的,因為選錯了安裝會跳異常但不會跟你說是XX位元問題。

Apache 2.4 Install
下載網址:http://www.apachelounge.com/download/
安裝在C更目錄下,命名為Apache24

增修 ./Apache24\conf\httpd.conf 
LoadModule php5_module "c:/php/php5apache2_4.dll"
AddHandler application/x-httpd-php .php
# configure the path to php.ini
PHPIniDir "C:/php"

#Find Directory index and add index.php
DirectoryIndex index.html index.php

Microsoft Visual C++ 2012 Runtime
下載網址 https://support.microsoft.com/en-us/kb/2977003 ...不裝的話會跳錯誤訊息...

PHP 5.6.12  Install
下載網址  https://support.microsoft.com/en-us/kb/2977003 ... 下載套件VC14表示是利用哪個版本的Visual Studio編譯,所以下載的套件必須搭配相對的Runtime才能使用。

安裝在C更目錄下,命名為php
增修php.ini
extension_dir = "ext"

extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_bz2.dll
extension=php_fileinfo.dll
extension=php_gd2.dll
extension=php_gettext.dll
extension=php_gmp.dll
extension=php_mbstring.dll

使用php -m 檢視載入模組是否正確,也可以使用phpinfo();來看。

在系統環境變數下
新增;c:\php;c:\Apache24;c:\Apache24\bin

命令提示字元操作
安裝Apache  c:\apache24\bin\httpd -k install
開機啟動        c:\sc config Apache2.4 start=auto

其他
網站預設路徑 C:\Apache24\htdocs

2016年10月5日 星期三

codeigniter csrf機制設定

串接金流後...想找些避免被有心人士誤用的頁面稽核機制,除了原有的那些措施之外,有文章推薦可啟用在 CI 2.0的 csrf(Cross Site Request Forgery) protection 功能。

設定 application\config\config.php將csrf打開

$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;

接著在View中如果有需要提交的Form則使用form_open(action, variable array)來寫就會直接產生相關的Code,真方便。

不過...如果有接收金流的Return Url時,會出現不給接收的Message(謎之聲~當然因為金流公司不會送相關的稽核Code過來。
解法:

設定 application\config\config.php中改寫為

if (stripos($_SERVER["REQUEST_URI"],'controler name/function name') === FALSE) {
  $config['csrf_protection'] = TRUE;
else 
  $config['csrf_protection'] = FALSE; 

這樣就能有例外情況了。

另外如果不想使用form_open()要手動來的話~找到的範例如下。
$(function(){
    $('#btn').click(function(){
        $.ajax({
            type:'POST'
            ,url:'/ajax' //ajax接收的server端
            ,data:$('#form').serialize()+'&csrf_test_name='+ getCookie('csrf_test_name')
            ,success:function(data){
            alert(data.msg);
            }
            ,dataType:'json'
        });
    });
});

function getCookie(name){
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr != null) return unescape(arr[2]); return null;

}

RHEL / CentOS 安裝 APC 加速 PHP

APC (Alternate PHP Cache) 是一套 PHP 快取系統, 它可將沒改變 PHP Code 情況下, 將已編譯結果緩存下來, 下次需要使用時就可節省重新編譯的時間。

因為CentOS之前手動升級成PHP 5.6所以指令要小修一下才能裝。

所需套件
yum install php56w-pear php56w-devel httpd-devel pcre-devel
安裝套件
yum install php56w-pecl-apcu

在Ubuntu使用
apt-get install php-apc

重啟服務

service httpd restart

最後檢查 APC 是否正確安裝, 使用 phpinfo()去觀看是否有APCU的套件,若有則表示完成了。