2021年5月27日 星期四

MySQL 語法

 查詢 mysql 可使用多少記憶體(依據 my.cnf設定)

SELECT ( @@key_buffer_size
+ @@query_cache_size
+ @@innodb_buffer_pool_size
+ @@innodb_additional_mem_pool_size
+ @@innodb_log_buffer_size
+ @@max_connections * ( @@read_buffer_size
+ @@read_rnd_buffer_size
+ @@sort_buffer_size
+ @@join_buffer_size
+ @@binlog_cache_size
+ @@thread_stack
+ @@tmp_table_size )
) / (1024 * 1024 * 1024) AS MAX_MEMORY_GB;
ddd

2021年4月3日 星期六

Inversion of control 控制反轉

控制反轉:

也是降低物件間耦合度的一種原則。
當A class 中需要 B class 時,此時 B 是 A 的依賴。
一般方法是在 A 中 new B,
使用控制反轉原則時,則把獲得 B 的控制反轉給外部,
或某控制器。

即是在外部準備好 B,等需要時傳入,或再取得。

常見方法:

Dependency Injection 依賴注入
    基於介面:實現特定介面供外部容器注入依賴。    
    基於set方法:實現 set 方法供外部呼叫,傳入依賴。
    基於建構函式:在建構式傳入依賴。

Dependency Lookup   依賴搜尋
    使用框架提供的方法取的依賴。

Golang tag

 Golang struct 可使用 tag。

內建tag:

json:json.Marshal / json.Unmarsh 使用

type Item struct{

    Price int `json:"price"`    //field name will be marshal / unmarshal to price 

}


自訂tag:

可用來擴充 struct field 資料,設定預設值。

type Item struct{

    Price int `json:"price" default:"10"`

}

func (I *item) GetPrice() int {

v := reflect.ValueOf(*I)
        tag := v.Type().Field(0).Tag.Get("default")

        if tag != "" {

i,_:= strconv.Atoi(tag)
            return i
        }

        return -1

}

可用來判斷合法值。

type Item struct{

    Price int `json:"valid" :"10-100"`

}

func (I *Item)GetDefaultPrice()int{

      v:=reflect.ValueOf(I)

       tag:=v.Type().Field(0).Tag.Get("valid")

        if tag!=""{
            val,_:=strconv.Atoi(tag)
            result:=strings.Split(tag,"-")    
            min,_:=strconv.Atoi(result[0])
            max,_:=strconv.Atoi(result[1])

             if val>=min && val<=max{
                return true
            }
                return false

         }
         return false
}


2021年2月24日 星期三

golang build 其他平台


 

//windows

>CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o output main.go


//linux

>CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o output main.go

2021年1月12日 星期二

Linux 查網路通否

 這裏


netcat

yum install nc

nc -zvw3 192.168.1.10 80

Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.1.10:80.
Ncat: 0 bytes sent, 0 bytes received in 0.01 seconds.


nmap

yum install nmap

nmap 192.168.1.10 -p 80

Starting Nmap 6.40 ( http://nmap.org ) at 2019-03-27 15:42 HKT
Nmap scan report for 192.168.1.10
Host is up (0.00042s latency).
PORT STATE SERVICE
80/tcp open http


telnet

yum install telnet

telnet 192.168.1.10 80

Trying 192.168.1.10…
Connected to 192.168.1.10.
Escape character is ‘^]’.

MySQL NDB Cluster Management 管理

管理 Management

Official document

 

ndb_mgm client commands

shell>ndb_mgm -e show   //直接查看

shell>ndb_mgm //進入terminal client

ndb_mdm>help

ndb_mdm>connect connect-string

ndb_mdm>show

node_id/all start //啟動data node(data node須使用外部 ndbd --nostart 參數啟動,非真正啟動,是等 ndb_mgm的這個命令啟動)
//只對all data nodes 生效

node_id/all stop [-a,-f] //關閉指定的 data or mgm node
-f 強迫關閉,不做完整性檢查。(一般如果會造成cluster錯誤,則stop會失敗)
-a 關閉safety check

all stop //關閉全 data node (只對all data nodes生效)

node_id/all restart [-n,-i,-a,-f]
-i 重新初始,會刪除node 檔案系統。
-n 重啟data node,但是先不連線,直到下start指令
-a 放棄這個node目前的transactions。會遺失資料。
-f 強迫關閉,不做完整性檢查。

node_id/all status //顯示data node資料

node_id/all report report-type   //顯示data node 報告

all report memory usage (Data usage is 23%(93873 32K pages of total 393216)

393216 M/1024/32 K=12G

all report backup status 顯示備份狀況

all report eventlog 顯示 event from event log buffer

quit/exit     //terminates management client

shutdown   //關閉整個cluster

create nodegroup node_id[,node_id]    create a new ndb cluster node group and
          caused data node to join it
          //把某幾個node建立group,統一處理 

drop nodegroup nodegroup_id    drops the ndb cluster node group
         //刪除node group

node_id nodelog debug {on|off}    toggle debug loggin

start backup    開始online backup

abort backup    關閉online backup

安裝位置

mgm node /var/lib/mysql-cluster
mgm node backup /var/lib/mysql-cluster/BACKUP
data node /
sql node /

設定檔位置

data node, sql node 都需要 /etc/my.cnf

mgm node /var/lib/mysql-cluster/config.ini
data node  /etc/my.cnf
sql node /etc/my.cnf

log檔位置

mgm node log/data node log

安裝的位置  /var/lib/mysql-cluster/ndb_1_cluster.log
mgm log:            ndb_1_cluster.log
mgm log:            ndb_1_out.log

data node error log:ndb_3_error.log
data node trace log:ndb_3_trace.log.7

mysql node err log

看 my.cnf裡面設定
通常是 log-error=/var/log/mysqld.log

預設port

ndb_mgmd 預設1186
/var/lib/mysql-cluster/config.ini 設定
[ndb_mgmd]
PortNumber=1186

sql node 預設3306
/etc/my.cnf
[mysqld]
port=3306

data node (ndbd/ndbmtd) 預設動態分配。
可在設定檔設。
[ndbd]
ServerPort=11100

查詢使用哪個port

sudo netstat --listening --numeric --programs | grep '\bLISTEN\b' | grep -E '/ndb|/mysqld'

Adding ndb cluster nodes online

1. edit config.ini, add new ndbd sections corresponding to node.
    如果有多個mgm, 則每個config.ini都要加。
2. restart all mgm server with --reload or --initial
3. restart all data nodes.
4. if you using API node with dynamically allocated IDs, you must restart all API nodes.
5. restart any SQL or API nodes.
6. start new data nodes.
7. 執行 alter table... algorithm=inplace,reorganzie partiton ?? 

Backup and restore

backup

document

ndb_mgm command

1. 須先設定backup configuration document
2. start backup [backup_id][wait option][snapshot option]
        wait shell何時返回。nowait就即時返回。

預設備份檔位置 /var/lib/mysql-cluster/BACKUP

restore 

doucment

ndb_restore [-c connection_string] -n node_id -b backup_id [-m] -r --backup-path=/file

還原單表
ndb_restore [...] --include-databases=db1,db2 --include-tables=db3.t1,db3.t2

整庫還原

1. 進入單使用者模式
    ndb_mgm>enter single user mode
2. ndb_restore -c connetion_string -n 1 -m -r --backup_path=/file
    //第一個restore 需要 -m, 其他不需要