2019年12月16日 星期一

redis

command

  • redis-server:啟動服務
  • redis-client -h 127.0.0.1 -p 6379:client登入

Config File

  • Mac /usr/local/etc/redis.conf
  • Centos /etc/redis.conf

Set password

在redis.conf中設置,requirepass pass 這行。

執行session設置(僅限本session有效)

redis 127.0.0.1:6379> AUTH PASSWORD
(error) ERR Client sent AUTH, but no password is set
redis 127.0.0.1:6379> CONFIG SET requirepass "mypass"
OK
redis 127.0.0.1:6379> AUTH mypass

Mac Command

launchctl

Basic

launchctl依據plist的設定起動程式

Commands

load read a plist file and schedule a launch
unload stop and unschedule a config file
start start the service, ignore the schedule
stop stop the service, ignore the schedule

restart example

launchctl unload -w ~/Library/LaunchAgents/com.apple.myservice.plist

launchctl load -w ~/Library/LaunchAgents/com.apple.myservice.plist


Unload default apache
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

User defined schedule config
範例

2019年12月4日 星期三

homebrew

command

  • brew help
  • brew update
  • brew upgrade
  • brew search
  • brew list
  • brew info
  • brew install
  • brew uninstall

Services

brew services list //list all services
brew services start redis
brew serivces stop redis

問題

安裝某個formula 之後,執行發生 command not found

brew需要寫入某些自己的資料夾,
如果裡面有檔案是owner是root,會出錯。
把/usr/local下,所有brew 開頭資料夾,裡面的檔案, owner 改成自己,並且加上 R 屬性。

2019年11月24日 星期日

Linux command top

top

top 指令說明

b  Batch mode
d  指定更新時間(單位:秒)
o  指定要輸出的欄位名稱
p  指定要觀察的pid
u  指定要觀察的使用者名稱

top -b -d 2 -p 6164  //使用batch mode,觀察pid 6164的行程並每2秒更新一次
top -d 2 -u abc   //觀察使用者abc所執行的行程並每2秒更新一次
top -b -o +%MEM | head -n 17     //使用batch mode,以記憶體做為排序依據,並只列出前17行

top上半部欄位說明

第1行
08:15:01:目前時間
up 44 min:主機開機時間
1 user:目前登入系統使用者數
load average: 1.00, 1.23, 0.86:系統每1分鐘、5分鐘、15分鐘的負載情況。
      load average資料是每隔5秒鐘檢查一次活躍的進程數,然後按特定演算法計算出的數值。
      如果這個數除以邏輯CPU的數量,結果高於5的時候就表明系統在超負荷運轉了。

第2行
Tasks:  63 total,   1 running,  62 sleeping,   0 stopped,   0 zombie:
  目前系統共63個行程,1個正在執行(running),62個休眠狀態(sleeping),
  0個已停止(stopped),0個僵屍狀態(zombie)

第3行
%Cpu(s):  1.1 us,  0.3 sy,  0.0 ni,100.0 id,  0.4 wa,  0.02 hi,  0.1 si,  0.0 st:
1.1% us — user cpu time(or)%CPU time spent in user space
0.3% sy — system cpu time(or)% CPU time spent in kernel space
0.0% ni — user nice cpu time(or) % CPU time spent on low priority processes
100% id -idle cpu time(or)%CPU time spent idle
0.4% wa — io wait cpu time(or)% CPU time spent in wait (on disk)
0.1% hi — hardware irq(or)%CPU time spent servicing/handling hardware interrupts
0.0% si — hardware irq(or)%CPU time spent servicing/handling hardware interrupts
0.0% st - steal time % CPU time in involuntary wait by virtual 
       cpu while hypervisor is servicing another processor (or) % 
      CPU time stolen from a virtual machine

第4行
KiB Mem :  1741400 total,  1600560 free,    73184 used,    67656 buff/cach
記憶體使用狀態:
1741400 total:實體記憶體容量
1600560 free:剩餘的記憶體容量
73184 used:已使用的記憶體容量
67656 buff/cache:linux用作block dev寫入buffer/block dev讀取cache記憶體容量
第5行
KiB Swap:        0 total,        0 free,        0 used.  1557120 avail Mem 
swap使用狀態:
0 total:swap總容量
0 free:剩餘的swap容量
0 used:已使用swap容量
1557120 avail Mem (available)新process在不使用swap請況下啟動
              可配置多少記憶體,應是mem free + buff/cache

top下半部欄位說明

PID — 行程id(Process ID)
USER — 該行程使用者
PR — 行程優先順序
NI — nice值(值越小優先權優高)
VIRT — 該行程使用的虛擬記憶體總量,單位kb(VIRT=SWAP+RES)
RES — 該行程使用的、未被換出的實體記憶體大小,單位kb(RES=CODE+DATA)
SHR — 共用記憶體大小(單位kb)
S — 行程狀態。D=不可中斷的睡眠狀態 R=運行 S=睡眠 T=跟蹤/停止 Z=僵屍進程
%CPU — 上次更新到現在的CPU時間佔用百分比
%MEM — 行程使用的實體記憶體百分比
TIME+ — 行程使用的CPU時間總計,單位1/100秒
COMMAND — 行程名稱

e/E  時間顯示單位切換,可分s,ms,gs,ts,ps
f/F  Fields Management可自行選擇要顯示的欄位
h  叫出help說明
k  刪除指定行程
l  隱藏/顯示load average(第1行)資訊切換
L  尋找字串
M  隱藏/顯示memory info (第4行)資訊切換
P  依據cpu用量進行排序
q  離開top
R  修改nice值
S  更改延遲時間(預設3秒)
t  隱藏/顯示tasks(第2.3行)資訊切換
T  依據Time+時間/累計時間進行排序

top按鈕說明

Summary Area Commands

按鍵  說明
c show scroll coordinates toggle
l load average/uptime toggle
t task/cpu statues toggle
m  memroy/swap toggle
1  single/seprate cpu states toggle
   顯示單一/多cpu使用情形切換
2 numa node /cpu summary toggle
3 expand numa node

Task Area Commands

按鍵  說明
j
J
B  將欄位值做粗體/一般字體切換
x
y
z  顯示顏色切換
Z  更改排序方式/顏色切換時之顏色

Linux 系統資源監控 top mpstat sar uptime

top 監控整體

top裡按1,看每個cpu使用狀況
top裡按2,看每個node使用狀況


//如果是centos,以下須先安裝 yum -y install sysstat

iostat 監控 cpu 及 block 設備(及分割槽) io

# iostat -xz 1 
Linux 2.6.32-696.el6.x86_64 (DX_JQ_2) 12/07/2018 _x86_64_   (32 CPU)

avg-cpu: %user %nice %system %iowait %steal %idle
           0.05 0.00 0.07 0.01 0.00 99.87

Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 0.00 3.29 0.02 1.13 1.28 35.30 31.88 0.02 20.90 7.84 21.13 5.19 0.60
dm-0 0.00 0.00 0.02 4.41 1.27 35.29 8.25 0.07 14.68 8.78 14.70 1.34 0.60

avg-cpu: %user %nice %system %iowait %steal %idle
           0.06 0.00 0.06 0.00 0.00 99.87
iostat [參數] [間隔秒][次數] 
iostat -x 1 10 //監控詳細信息,間隔1秒,共10次
iostat -C
iostat -dk

    -C 显示 CPU 使用情况
    -d 显示磁盘使用情况
    -k 以 KB 为单位显示
    -m 以 M 为单位显示
    -N 显示磁盘阵列(LVM) 信息
    -n 显示 NFS 使用情况
    -p[磁盘] 显示磁盘和分区的情况
    -t 显示终端和 CPU 的信息
    -x 显示详细信息
    -V 显示版本信息

監控cpu的屬性
%user  CPU 处在用户模式下的时间百分比
%nice  CPU 处在带 NICE 值的用户模式下的时间百分比
%system  CPU 处在系统模式下的时间百分比
%iowait  CPU 等待输入输出完成时间的百分比
%steal  管理程序维护另一个虚拟处理器时,虚拟 CPU 的无意识等待时间百分比
%idle  CPU 空闲时间百分比

監控disk的屬性
Device  监测设备名称
rrqm/s  每秒需要读取需求的数量
wrqm/s  每秒需要写入需求的数量
r/s  每秒实际读取需求的数量
w/s  每秒实际写入需求的数量
rsec/s  每秒读取区段的数量
wsec/s  每秒写入区段的数量
rkB/s  每秒实际读取的大小,单位为 KB
wkB/s  每秒实际写入的大小,单位为 KB
rMB/s  每秒实际读取的大小,单位为 MB
wMB/s  每秒实际写入的大小,单位为 MB
avgrq-sz  需求的平均大小区段
avgqu-sz  需求的平均队列长度
await  等待 I/O 平均的时间(milliseconds)
svctm  I/O 需求完成的平均时间
%util  被 I/O 需求消耗的 CPU 百分比


tps屬性
tps  该设备每秒的传输次数
kB_read/s  每秒从设备读取的数据量
kB_wrtn/s  每秒向设备写入的数据量
kB_read  读取的总数据量
kB_wrtn  写入的总数据量

mpstat 監控cpu

mpstat -A //監控所有狀態
mpstat -P 0/1/ALL   // 指定cpu編號
mpstat -P ALL 2 3  //每兩秒產生一次所有cpu資料,共3次


範例 mpstat -P All 2

11時38分44秒  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
11時38分46秒  all   55.25    0.00    1.00    0.75    0.00    0.25    0.00    0.00    0.00   42.75
11時38分46秒    0   10.10    0.00    1.52    1.52    0.00    0.00    0.00    0.00    0.00   86.87
11時38分46秒    1  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00

%user      在internal时间段里,用户态的CPU时间(%),不包含nice值为负进程  (usr/total)*100
%nice      在internal时间段里,nice值为负进程的CPU时间(%)   (nice/total)*100
%sys       在internal时间段里,内核时间(%)       (system/total)*100
%iowait    在internal时间段里,硬盘IO等待时间(%) (iowait/total)*100
%irq       在internal时间段里,硬中断时间(%)     (irq/total)*100
%soft      在internal时间段里,软中断时间(%)     (softirq/total)*100
%idle      在internal时间段里,CPU除去等待磁盘IO操作外的因为任何原因而空闲的时间闲置时间(%) (idle/total)*100

sar 監控cpu,mem,i/o,net io

sar -P All 1 2  //監控所有cpu狀態,間隔1秒,共2次
sar -u 2 3      //監控所有cpu狀態,間隔2秒,共3次
sar -r //記憶體使用統計
sar -R //記憶體整體統計
sar -W //swap統計
sar -b //I/O及傳輸率統計
sar -d //block device statistics. 
       //block device is a kind of file,
       //which represents a device of some kind, with data 
       //that can be read or written to it in block.
       //可用block形式讀寫資料的裝置(linux中都當成檔案)


 -b I/O and transfer rate statistics
 -B Paging statistics
 -d Block device statistics
 -F [ MOUNT ]
  Filesystems statistics
 -H Hugepages utilization statistics
 -I {  | SUM | ALL | XALL }
  Interrupts statistics
 -m { [,...] | ALL }
  Power management statistics
  Keywords are:
  CPU CPU instantaneous clock frequency
  FAN Fans speed
  FREQ CPU average clock frequency
  IN Voltage inputs
  TEMP Devices temperature
  USB USB devices plugged into the system
 -n {  [,...] | ALL }
  Network statistics
  Keywords are:
  DEV Network interfaces
  EDEV Network interfaces (errors)
  NFS NFS client
  NFSD NFS server
  SOCK Sockets (v4)
  IP IP traffic (v4)
  EIP IP traffic (v4) (errors)
  ICMP ICMP traffic (v4)
  EICMP ICMP traffic (v4) (errors)
  TCP TCP traffic (v4)
  ETCP TCP traffic (v4) (errors)
  UDP UDP traffic (v4)
  SOCK6 Sockets (v6)
  IP6 IP traffic (v6)
  EIP6 IP traffic (v6) (errors)
  ICMP6 ICMP traffic (v6)
  EICMP6 ICMP traffic (v6) (errors)
  UDP6 UDP traffic (v6)
 -q Queue length and load average statistics
 -r Memory utilization statistics
 -R Memory statistics
 -S Swap space utilization statistics
 -u [ ALL ]
  CPU utilization statistics
 -v Kernel table statistics
 -w Task creation and system switching statistics
 -W Swapping statistics
 -y TTY device statistics

uptime

檢視服務器負載狀況
uptime
 22:37:31 up 65 days, 3:15, 1 user, load average: 40.00, 25.01, 10.05
檢視當前服務器1min, 5min, 15min 平均負載
是cpu負載、IO負載的整體評估
如果1min 的值大於 15min的值,表示負載越來越高。

dmesg

檢視資訊
$ dmesg |tail
[ 2567.534310] IPv6: ADDRCONF(NETDEV_UP): em3: link is not ready
[ 2570.386536] tg3 0000:02:00.0 em3: Link is up at 1000 Mbps, full duplex
[ 2570.386550] tg3 0000:02:00.0 em3: Flow control is off for TX and off for RX
[ 2570.386555] tg3 0000:02:00.0 em3: EEE is disabled
[ 2570.386584] IPv6: ADDRCONF(NETDEV_CHANGE): em3: link becomes ready
[20907.774686] perf: interrupt took too long (2502 > 2500), lowering kernel.perf_event_max_sample_rate to 79000
[50276.167405] perf: interrupt took too long (3173 > 3127), lowering kernel.perf_event_max_sample_rate to 63000
[96662.454537] perf: interrupt took too long (4010 > 3966), lowering kernel.perf_event_max_sample_rate to 49000
[143249.558510] perf: interrupt took too long (5025 > 5012), lowering kernel.perf_event_max_sample_rate to 39000
[204028.503117] perf: interrupt took too long (6286 > 6281), lowering kernel.perf_event_max_sample_rate to 31000

vmstat

檢視虛擬記憶體狀況
記憶體、分頁、IO、cpu
$ vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r b swpd free buff cache si so bi bo in cs us sy id wa st
 0 0 0 3082644 2076 4156120 0 0 1 210 12 9 1 0 99 0 0
 0 0 0 3083180 2076 4156112 0 0 0 473 2231 2420 1 0 99 0 0
 0 0 0 3083212 2076 4156112 0 0 0 12510 3946 3945 1 2 97 0 0
 0 0 0 3083684 2076 4156096 0 0 0 1403 2333 1725 1 0 99 0 0
 1 0 0 3082868 2076 4156388 0 0 0 505 2307 1795 4 1 95 0 0
 0 0 0 3083508 2076 4156040 0 0 0 475 1888 1570 1 1 99 0 0
 0 0 0 3083152 2076 4156040 0 0 0 8090 1694 1910 1 1 99 0 0

procs

r:在cpu上執行和等待執行的程式數量
b:阻塞的程式

memory

swpd:虛擬記憶體使用大小
free:空閒的記憶體大小
buffer:用作 buffer 的記憶體大小
cache:用作 cache 的記憶體大小

swap

si:每秒從交換區寫道記憶體的大小
so:每秒寫入到交換區的大小

IO

bi:block device 每秒接收的 block 數量
bo:block device 每秒傳送的 block 數量

system

in:每秒 cpu 中斷次數
cs:每秒 content 切換次數(越小越好)

CPU

us:使用者的 cpu 時間
sy:系統核心執行 cpu 時間
id:空閒的 cpu 時間
wa:等待 IO 的時間
st:從虛擬機中偷走的時間?

free

檢視系統整體記憶體
$ free -h
    total used free shared buff/cache available
Mem: 7.6G   697M   3.0G   23M   3.9G    6.5G
Swap: 8.0G   0B   8.0G
buffer cache 是block device 的記憶體快取
page cache 是檔案資料的快取

2019年11月13日 星期三

Gitlab and Sourcetree

Generate ssh key

cd ~/.ssh
ssh-keygen -t rsa -C "email@ex.com"  //email 可隨便打
//產生 id_rsa //密鑰
//id_rsa.pub //公鑰
//檔名一定要是id_rsa,因為sourcetree 不能匯入ssh key, 只會找預設的key

Gitlab ssh setting

登入gitlab,
左側欄有SSH Keys,
貼上 id_rsa.pub的內容,
最後的email不用貼

Testing

ssh://git@host/test.git
//user 一定要是git,這是自架的gitlab預設的
//有反應就是設定正確

Sourcetree setting

在sourcetree的設定裡,改成跟ssh 測試一樣。
ssh://git@host/test.git

問題

1 如果domain不行,改用ip
2 記得user 要用git

2019年10月23日 星期三

go modules


用途

可以在$GOPHTH外開專案
怕依賴package不見,可以保留依賴檔案(用vendor功能)
同一依賴package,可已有多版本共存在依賴目錄
可以用Goproxy解決有些地方無法使用o get的問題

說明

環境變數要打開 GO111MODULE=on
專案目錄要設定成使用 module : go mod init
專案的外部 package 還是放在$GOPATH/pkg/mod 下
可以保留目前用的外部 package 到 vendor目錄
可選擇使用 vendor 目錄的 package 或用 go mod 抓

流程

  1. 環境變數設置 GO111MODULE=on(使用 go module 模式)
  2. 專案設定使用 module 。 專案目錄下 go mod init。產生 go.mod 檔案。
  3. 安裝外部 package 方法1:go get -u github.com/xxxx/xxx。會更新 go.mod。
    如果 package 沒有在 go.mod 中指定版本,會抓最新的 tag。沒有 tag 會抓最新 commit。
  4. 安裝外部 package 方法2:go mod downloac。下載全部依賴到 $GOPATH/pkg/mod 。
  5. 然後 go 就會到 $GOPATH/pkg/mod 去找需要的外部 package。

原則

  • go get 如果 package 沒有在 go.mod 中指定版本,會抓最新的 tag。沒有 tag 會抓最新 commit。
  • go get 可抓指定的 tag 或 指定的 commit hash。
  • 要更改專案使用的 package 版本,可以直接改 go.mod,然後重新 go get .....
  • 要使用 vendor 目錄的 package: go build, go install, go test 都加上 -mod=vendor 參數。

go111MODULE 設定

  • 只要有設定GO111MODULE=on,就是使用module。
  • 1.13起,專案在 GOPATH 外,且有 go.mod 文件。無論有無設定都等同于 GO111MODULE=on。
  • 專案在 GOPATH 内,且没有 go.mod 文件時,等同於 GO111MODULE=off

go.mod 設定

  • module 定義模組位置
  • require 指定外部 package 及其版本
  • exclude 排除指定的 package
  • replace 只用不同的 package 取代原本的某個 package
module modtest
go 1.12
require (

 github.com/mattn/go-isatty v0.0.8 // indirect
 github.com/sirupsen/logrus v1.4.2
 github.com/ugorji/go v1.1.7 // indirect
 gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
 gopkg.in/yaml.v2 v2.2.2 // indirect
)
exclude github.com/gin-gonic/gin v1.4.0
replace github.com/tedmax100/modtest => ../gomodtest  //用本地目錄或遠端 repo 取代 modtest

go mod 命令

  • go mod init [project dir / or this dir]
  • go mod download
  • go mod tidy
  • go mod vendor
  • go mod edit
  • go mod why

goland IDE

A module 專案時,IDE的 build/test working dir 要在 module 目錄

B module 專案時,IDE的 build/test output dir 不需要在 module 目錄

C Preference/Go/Go Modules(vgo) => Enable



1 Init project to create go.mod

in goland IDE terminal
>go mod init
will create go.mod

2 Create go.sum

in goland IDE terminal
>go test / go build
will create go.mod

or go mod tidy
will create go.mod and add / remove dependencies

go.mod 裡面會紀錄這個project用到的dependencies及版本。
因為是紀錄版本,所以如果作者commit卻沒有tag 版本,那就會對應錯。

3 Dependency 的版本不一定對,要自己確認。

因為dependency 是從某個公開的repository 抓的(多半是github),
他抓的原則是:
a 如果有指定版本,就抓指定版本。
b 抓有tag release版的最新版。

問題

有可能作者commit很多版,但是都沒更新 release tag,這樣就抓不到你要的版本。

方法:

in project direcroty
go get -u github.com/satori/go.uuid@b2ce2384e17bbe0c6d34077efa39dbab3e09123b

這樣會抓回指定commit的版本
go.mod / go.sum裡面,這個 package的版號,會跟著改。

如果是自己寫的package


go.mod檔案的 module 設定要正確,go get 是用這個判斷 module path,
因為 go mod 要從 git server repo 抓 package,
他不會從 local 電腦 的 $GOPATH 抓 package,
所以要把自己寫的 package 也加入 go mod,
要把這個 package 放到 github。
或放到某個自設的 git server 的 repo。

否則使用go mod 會出現  cannot find module providing package  ...... error

go get 抓 github private repository

get get 只使用https
可能需要社 GOPRIVATE 變數
go env -w GOPRIVATE=github.com/abcd

//設定 global config
//好像一定要設成 global, 設local沒用
git config --global url."https://$USERNAME:$ACCESS_TOKEN@github.com".insteadOf "https://github.com"
$USERNAME 就是github username
$ACCESS_TOKEN 就是 user access token, 在 setting => Developer settings => Personal access tokens


2019年8月23日 星期五

Git

Git global setup

git config --global user.name "cruise"
git config --global user.email "cruisecg.cg@gmail.com"

Create a new repository

git clone https://gitlab.3ag.xyz/backend/service.git
cd service
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

Push an existing folder

cd existing_folder
git init
git remote add origin https://gitlab.3ag.xyz/backend/service.git
git add .
git commit -m "Initial commit"
git push -u origin master

Push an existing Git repository

cd existing_repo
git remote rename origin old-origin
git remote add origin https://gitlab.3ag.xyz/backend/service.git
git push -u origin --all
git push -u origin --tags

用ssh 取代 https

教學
//可以每次手打
git remote set-url origin git@gitlab.com/...

//globally config (~/.gitconfig) or per-repo (.git/config)
git config url.ssh://git@github.com/.insteadOf https://github.com/
git config url.ssh://git@gitlab.com/.insteadOf https://gitlab.com/

or
git config --global url.ssh://git@github.com/.insteadOf https://github.com/
git config --global url.ssh://git@gitlab.com/.insteadOf https://gitlab.com/

//這幾種格式都試一下
git config url.git@github.com/.insteadOf https://github.com/
git config url.git@github.com:.insteadOf https://github.com/
git config url.ssh://git@github.com:.insteadOf https://github.com/

檔案

檔案狀態
1 untracked  commit過後加入的檔案,預設沒有 track
2 tracked    有 track 的檔案,有任何變更,會追蹤
3 unstaged   有追蹤,但是沒有加入預備區(預備提交)
4 staged     有加入預備區
5 uncommitted  在預備區,尚未提交
6 committed    已經提交(push 就會上到 remote repos)

變更檔案狀態
改變檔案狀態,都是使用 add 命令。無論是要 track , stage 檔案,都是 add 。
add 之後,檔案會變成 uncommitted 狀態。

2019年5月29日 星期三

Zabbix



安裝流程

1 安裝server
2 安裝 mysql
3 建立 db
4 改設定

參考官方安裝文件
https://www.zabbix.com/download?zabbix=4.0&os_distribution=centos&os_version=7&db=mysql

https://www.zabbix.com/documentation/4.0/manual/installation/install_from_packages/rhel_centos

設定 zabbix mysql password

grant all privileges on zabbix.* to zabbix@localhost identified by 'password';
flush privileges;

設定 php time zone

vi /etc/httpd/conf.d/zabbix.conf,
php_value date.timezone Asia/Shanghai

2019年5月16日 星期四

MySQL InnoDB Lock

詳細說明
詳細說明

InnoDB在transaction中,會視語法不同,自動加鎖。
Transaction中的鎖,都是以Row為標的。

非transaction中,因為會自動COMMIT,所以使用加鎖的語法也沒用。

InnoDB三種加鎖類型

  • Record lock: This is a lock on an index record.
  • Gap lock: This is a lock on a gap between index records, or a lock on the gap before the first or after the last index record.
  • Next-key lock: This is a combination of a record lock on the index record and a gap lock on the gap before the index record.
  • 官網說明:http://dev.mysql.com/doc/refman/5.7/en/innodb-record-level-locks.html
  • 重點1:Next-key locking combines index-row locking with gap locking.
  • InnoDB performs row-level locking in such a way that when it searches or scans a table index, it sets shared or exclusive locks on the index records it encounters (select 過程搜尋遇到的資料列都會被加鎖)

InnoDB鎖定模式

  • 共享鎖定 (shared lock) (S lock):permits the transaction that holds the lock to read a row.
  • 排他鎖定 (exclusive lock) (X lock):permits the transaction that holds the lock to update or delete a row.

不同語句的加鎖效果

  • SELECT ... FROM 基本不加鎖
    但在 SERIALIZABLE 隔離層級時,會自動在遇到的資料加鎖
  • SELECT ... FROM ... LOCK IN SHARE MODE (加共享鎖)
  • SELECT ... FROM ... FOR UPDATE (加排它鎖)
  • UPDATE ... WHERE ... (加排它鎖)
  • DELETE FROM ... WHERE ... (加排它鎖)
  • INSERT (加排它鎖)

MySQL AUTOCOMMIT命令

Autocommit 是設定MySQL是否自動提交變更

AUTOCOMMIT 的設定值,預設一般都是 1

查詢目前 AUTOCOMMIT 的設定值:SELECT @@AUTOCOMMIT

將 AUTOCOMMIT 改為 0 時 ( SET AUTOCOMMIT=0 ),就算沒使用transaction( START TRANSACTION 或 BEGIN ),整個連線執行的 SQL 指令,都會等到下達 COMMIT 提交後,才會真正儲存變更。

也就是當 AUTOCOMMIT=0 時,跟在交易模式下相同。

更改COMMIT狀態指令:
SET AUTOCOMMIT=0; // AUTOCOMMIT=1;

提交指令:
COMMIT;

2019年3月26日 星期二

Linux 常用指令

Process 管理

//列出系統上最耗費記憶體的程式
//-e 輸出所有行程的資訊
//-o 指定輸出欄位
//--sort 排序
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head

//取當前pid
echo $$

//process 資源限制
ls /proc/{pid}/fd
cat /proc/{pid}/limits

//開檔狀況
ls -l /proc/{pid}/fd | less

//總開檔數
ls -l /proc/{pid}/fd | wc -l

網路相關

//查看80是哪個procese監聽的
netstat -tlnp | grep :22

tcp    0  0 0.0.0.0:22  0.0.0.0:*  LISTEN   1444/sshd
tcp6   0  0 :::22       :::*       LISTEN   1444/sshd

User 管理

sudo -i 切換為root

ssh 管理

//scp
scp "fileName" ubuntu@xxx.xx.xx.xx:/home/ubuntu/
scp -i "keyfile" filename aa@xxx.xxx.xxx.xxx:/home/aa/
sshpass -p "pass" scp "fileName" ubuntu@xxx.xx.xx.xx:/home/ubuntu/

//指定port P大寫
scp -P port root@xxx.xxx.xxx:/root/abc.jpg abc.jpc 
scp -P port abc.jpg root@xxx.xxx.xxx:/root/abc.jpg

//指定key
ssh -i 'keyfile' aa@xxx.xxx.xxx.xxx

//設定ssh timeout 時間
vim /etc/ssh/sshd_config

//找到ClientAliveInterval 参数,如果没有就自己加一行。
//client alive 檢查,秒
ClientAliveInterval=600 

//client alive 檢查次數 
ClientAliveCountMax=12

//依據以上設定,就是600*12=7200秒都沒反應就斷線
//開啟root ssh login
sudo passwd root    //更改root密碼
sudo vi /etc/ssh/sshd_config
PermitRootLogin yes
PasswordAuthentication yes

//centos 
service sshd restart
systemctl restart sshd

//debain
service ssh restart
systemctl restart sshd

ssh agent

1. Start ssh-agent in the background.
 > Agent pid xxxxx
2. Add ssh key to agent
  $ ssh-add -K pathToPrivateKey
  (-K 只限mac 內建的ssh-add, 表示存 passphase in your keychain)

3. if you key with passphase, agent will ask you to input pass.

//看ssh-agent執行狀況
看變數,是否有輸出
echo $SSH_AUTH_SOCK
echo $SSH_AGENT_PID

//in shell script
//自動加入所有預設rsa的key
if [ -z "$SSH_AUTH_SOCK" ] ; then
  eval `ssh-agent -K`
  ssh-add
fi

//kill ssh-agent
ssh-agent -k

github 使用多個 ssh key 與帳號

Host   連線至遠端的別稱(可以隨便填)
HostName   遠端的Domain或IP
Port   遠端的SSH連接埠(為預設的話可以不填)
User   使用者名稱
PreferredAuthentications   偏好使用的驗證(這邊的值都為publickey)
IdentityFile   私鑰的位置(可為相對或絕對位置,但建議使用絕對位置)

//edit ~/.ssh/config

Host gh.aaa.my
 HostName github.com
 User Hi_AAA
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/id_rsa_aaa_github

Host gh.bbb.company
 HostName github.com
 User Hi_BBB
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/id_rsa_bbb_github

Host bb.aaa.my
 HostName bitbucket.org
 User Yo_AAA
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/id_rsa_aaa_bitbucket

原本 repo 位置會改變
原本 git@github.com:Hi_AAA/my_project.git  
新的 git@gh.aaa.my:Hi_AAA/my_project.git

Domain的部分可以使用設定檔的 Host 名稱取代

//key 加入 ssh-agent 管理
ssh-add 

apt


apt-cache policy mysql-server       列出mysql所有版本
apt-get install mysql-server=5.6.29 安裝mysql指定版本

2019年3月4日 星期一

網路 http

NotFalse 技術客詳細說明

HTTP

1.0 /1.1 /2.0

主要特色

簡單:request - response
不限傳輸資料內容(content-type定義)
無連接:一個request一個連接
無狀態

URI 與URL 參考

URI (Uniform Resource Identifier)統一資源識別符

web上的資源,例如檔案、圖片、程式,都是用URI定義的。
能定位到資源的就是URI,例如:
localhost:80/index.html
localhost:80/user/save

URL(Uniform Resource Location)統一資源定位符

URL是URI的子集。
URL是路徑,URI是實際資源。
URI:localhost:80/user/save
URL:localhost:80/user

Message

HTTP/1.1傳輸是以message為單位。
(HTTP/2是以message frame訊息框為單位)
Message稱為訊息、報文、消息。

Message格式

Start-line(起始行)
Headers(多個header-field+CRLF)
CRLF
Message-body(可選的)

Request Message範例

POST /?id=1 HTTP/1.1        (Start-line=>request line)
Host: echo.paw.cloud        (header-field)
Content-Type: application/json; charset=utf-8 (header-field)
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:53.0) Gecko/20100101 Firefox/53.0 (header-field)
Connection: close           (header-field)
Content-Length: 136         (header-field)
(CRLF)
(以下為message-body)
{
  "status": "ok",
  "extended": true,
  "results": [
    {"value": 0, "type": "int64"},
    {"value": 1.0e+3, "type": "decimal"}
  ]
}

Response Message範例

HTTP/1.1 200 OK                      (Start-line=>status line)
Content-Type: text/html; charset=utf-8    (header-field)
Date: Sat, 18 Feb 2017 00:01:57 GMT       (header-field)
Server: nginx/1.11.8                      (header-field) 
transfer-encoding: chunked                (header-field)
Connection: Close                         (header-field)
(CRLF)
(以下為message-body)
<html lang="en">
<head>
<title>echo<title>
</head>

Start-line 起始行

Request start line,依序是:
方法(method)、空白(space)、請求目標(request target)、空白(space)、HTTP版本(http version)、CRLF

Response start line,依序是:
HTTP版本(http version)、空白(space)、狀態碼(status code)、空白(space)、原因短語(reason phrase)、CRLF

Method

HTTP1.0 定義三種方法:post,get,head
HTTP1.1 定義的方法:OPTIONS, PUT, DELETE, TRACE 和 CONNECT

GET  请求指定的页面信息,并返回实体主体。
HEAD     类似于get请求,只不过返回的响应中没有具体的内容,用于获取报头
POST     向指定资源提交数据进行处理请求(例如提交表单或者上传文件)。数据被包含在请求体中。POST请求可能会导致新的资源的建立和/或已有资源的修改。
PUT      从客户端向服务器传送的数据取代指定的文档的内容。
DELETE   请求服务器删除指定的页面。
CONNECT  HTTP/1.1协议中预留给能够将连接改为管道方式的代理服务器。
OPTIONS  允许客户端查看服务器的性能。
TRACE    回显服务器收到的请求,主要用于测试或诊断
  • POST 
  • 規定資料要放在body中
    四種常用的content-type:
    application/x-www-form-urlencoded
    multipart/form-data
    application/json
    text/xml

Status Code

1xx 請求接受,繼續處理。
2xx 請求成功。
3xx 重定向-要完成請求要進一步處理。
4xx 客戶端錯誤,例如請求語法錯誤。請求無法實現:403。
5xx 服務器端錯誤。

Content Type

NotFalse 技術客詳細說明
  • application/x-www-form-urlencoded
  • 常用於POST表單,是key-value形式

    POST http://www.example.com HTTP/1.1
    Content-Type: application/x-www-form-urlencoded;charset=utf-8
    .....省略
    title=test&sub%5B%5D=1&sub%5B%5D=2&sub%5B%5D=3
    

  • multipart/form-data
  • 使用表單上傳文件時,必須對form的enctype設定為multipart-/form-data

    POST http://www.example.com HTTP/1.1
    Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryrGKCBY7qhFd3TrwA
    
    ------WebKitFormBoundaryrGKCBY7qhFd3TrwA
    Content-Disposition: form-data; name="text"
    
    title
    ------WebKitFormBoundaryrGKCBY7qhFd3TrwA
    Content-Disposition: form-data; name="file"; filename="chrome.png"
    Content-Type: image/png
    
    PNG ... content of chrome.png ...
    ------WebKitFormBoundaryrGKCBY7qhFd3TrwA--
    

  • application/json
  • POST http://www.example.com HTTP/1.1 
    Content-Type: application/json;charset=utf-8
    
    {"title":"test","sub":[1,2,3]}
    


  • text/xml
  • POST http://www.example.com HTTP/1.1 
    Content-Type: text/xml
    
    
    <methodcall>
        <methodname>examples.getStateName</methodname>
        <params>
            <param />
                <value><i4>41</i4></value>
            
        </params>
    </methodcall>

keep alive模式

參考
keep alive 需client-server兩邊配合

http 1.0中默認是關閉的,需要在http頭加入"Connection: Keep-Alive”,才能啓用Keep-Alive;

http 1.1中默認啓用Keep-Alive,如果加入"Connection: close “,才關閉。

目前大部分瀏覽器都是用http1.1協議,也就是說默認都會發起Keep-Alive的連接請求了

所以是否能完成一個完整的Keep- Alive連接就看服務器設置情況。

2019年2月15日 星期五

網路 Socket

Beejs guide to network programming詳細說明

Socket Types

  • Stream Socket
  • SOCK_STREAM 串流式
    基於TCP
    資料需要可靠的、需要順序的多半用stream socket
    例如:HTTP/TELNET

  • Datagram Socket
  • SOCK_DGRAM 訊息式
    基於UDP
    不可靠、不一定照順序、但是如果送達(封包重組完成,如果有分割的話),資料就是正確的。
    遊戲玩家位置、音樂、音效,等可容許小量丟失的。
    例如:TFT/DHCPD

  • Row Socket


  • Link Layer Socket

2019年2月13日 星期三

RabbitMQ

基本概念

官方網頁

RabbitMQ就像郵局,唯一不同的是它送數位資料,郵局送信。
Queue就是郵筒,存在郵局裡(RabbitMQ),只受限於ram跟disk。

message只存在queue裡。
多個producers可以送message給同一個queue。
多個consumer可以從同一個queue收message。
基本上每個message被收走就沒有了。
  • RabbitMQ=郵局
  • Queue=郵筒(存在於郵局RabbitMQ)
  • Message=信
  • Exchange=在producer這邊,他是收信的工具、在consumer這邊,他是送信的工具(存在於郵局RabbitMQ)
  • Producer=寄信者
  • Consumer=收信者

連線架構

TCP connection -> connection -> channel

Message 送出後,consumer會回傳ack(acknowledgement),
確認message正確收取,否則會re-queue此message。

訊息模型

Producer -> exchange -> Queue -> exchange -> Consumer
                        Queue -> exchange -> Consumer

以下為完成傳遞必要項目,缺一不可

  • 1 由Producer產生Message
  • 2 Message送到左邊的Exchange
  • 3 左邊的Exchange把Message送到綁定的Queue
  • 4 右邊的Exchange從Queue取出Message
  • 5 右邊的Exchange把Message送給Consumer
Producer及Consumer都可以宣告Exchange及Queue
若是沒宣告就是使用預設的,或是RabbitMQ會自行建立
宣告同名的Exchange或是Queue,就表示用現有的,RabbitMQ不會新建立一個
宣告同名的Exchange或是Queue,參數必須完全相同,否則報錯

基本模型(Producer/Consumer)
Producer -> Queue -> Consumer
Producer/Consumer是位於app,Queue是位於RabbitMQ
多個consumer模型(Worker Queues)
1 massage平均分配給多個consumer
2 等consumer回覆之後,才送新的message下去
Exchange模型(Publish/Subscribe)
所有要收的consumer,自行綁定上該exchange

一 Produce/Consumer(基本模型)

官方網頁

二 Work Queues(message acknowledgement、message durable)

官方網頁
  • Round-robin dispatching 輪詢式派送
    輪流派送給每個註冊的worker

  • Massage acknowledgement 訊息確認
    msgs, err := ch.Consume(
      q.Name, // queue
      "",     // consumer
      false,  // auto-ack
      false,  // exclusive
      false,  // no-local
      false,  // no-wait
      nil,    // args
    )
    
    //幾本上auto-ack都要設成true,不然就要自己送
    //否則rabbitmq 會保留此message,造成資源消耗
    
    go func() {
      for d := range msgs {
        log.Printf("Received a message: %s", d.Body)
        dot_count := bytes.Count(d.Body, []byte("."))
        t := time.Duration(dot_count)
        time.Sleep(t * time.Second)
        log.Printf("Done")
        d.Ack(false)
      }
    }()
    

  • Massage durability 訊息耐用(即便rabbitmq重開,未送出的message也不會消失)
    q, err := ch.QueueDeclare(
      "hello",      // name
      true,         // durable
      false,        // delete when unused
      false,        // exclusive
      false,        // no-wait
      nil,          // arguments
    )
    //producer、consumer宣告的queue durable要設成true
    //宣告同名的queue參數必須相同,否則錯誤
    
    err = ch.Publish(
      "",           // exchange
      q.Name,       // routing key
      false,        // mandatory
      false,
      amqp.Publishing {
        DeliveryMode: amqp.Persistent,
        ContentType:  "text/plain",
        Body:         []byte(body),
    })
    //publishing DeliveryMode 要設成 amqp.Persistent
    
    

  • 完成一個work才繼續派送(Fair dispatch 一節)
    基本上RabbitMQ的派送是平均派送,當他收到就派送,
    所以有可能派送給還沒完成工作的worker。
    可設定prefetch count=1
    這時rabbitMQ會收到前一個message的acknowledgement才會派送下一個。
    
    注意,此時queue有可能被填滿,你需要多設定幾個woker。
    
    err = ch.Qos(
      1,     // prefetch count
      0,     // prefetch size
      false, // global
    )
    

三 Publish/Subscribe(one message to multiple consumers)

官方網頁

Published message are going to be broadcast to all the receivers.
  • Exchanges
    4 types of exchange:direct,topic,headers,fanout
    
    err = ch.ExchangeDeclare(
      "logs",   // name
      "fanout", // type
      true,     // durable
      false,    // auto-deleted
      false,    // internal
      false,    // no-wait
      nil,      // arguments
    )
    
    //fanout:broadcast all messages to all the queues it knows
    
    

  • Bindings(Binding exchange and queue)
    err = ch.QueueBind(
      q.Name, // queue name
      "",     // routing key
      "logs", // exchange
      false,
      nil
    )
    

四 Routing(用路由方式傳送message給不同Queue)

官方網頁

Subscribe only to a subset of the messages.
  • 基本概念
    exchange publish時,可以加上路由(routing key)
    queue可以綁定exchange及routing key
    一個queue可以綁定一個exchange及多個routing key
    這樣就可以一個exchange 用多種routing發布,然後consumer利用不同routing接收
    

  • Bindings
    //Binding 是綁定exchange 及 queue
    //Binding時可以設定路由,用routing key parameter
    
    err = ch.QueueBind(
      q.Name,    // queue name
      "black",   // routing key
      "logs",    // exchange
      false,
      nil)
    
    //route to black
    
    

  • Direct exchange
    //用direct exchange才能使用binding的routing key
    //用fanout exchange會忽略routing key參數,合理,所以他才叫fanout
    
    err = ch.ExchangeDeclare(
                    "logs_direct", // name
                    "direct",      // type
                    true,          // durable
                    false,         // auto-deleted
                    false,         // internal
                    false,         // no-wait
                    nil,           // arguments
    )
    
     q, err := ch.QueueDeclare(
                    "",    // name
                    false, // durable
                    false, // delete when usused
                    true,  // exclusive
                    false, // no-wait
                    nil,   // arguments
     )
            
    err = ch.QueueBind(
      q.Name, // queue name
      "",     // routing key
      "logs", // exchange
      false,
      nil
    )
    

五 Publish/Subscribe(one message to multiple consumers)

官方網頁

Published message are going to be broadcast to all the receivers.
  • Exchanges
    4 types of exchange:direct,topic,headers,fanout
    
    err = ch.ExchangeDeclare(
      "logs",   // name
      "fanout", // type
      true,     // durable
      false,    // auto-deleted
      false,    // internal
      false,    // no-wait
      nil,      // arguments
    )
    
    //fanout:broadcast all messages to all the queues it knows
    
    

  • Bindings(Binding exchange and queue)
    err = ch.QueueBind(
      q.Name, // queue name
      "",     // routing key
      "logs", // exchange
      false,
      nil
    )
    
  • 
    

  • 
    

正式產品需要注意的主題

官方說明文件

  • Connection Management

  • Error Handling

  • Connection Recovery

  • Concurrency

  • Metric Collection

參考頁面
Publisher Confirms and Consumer Acknowledgements
Production Checklist
Mornitoring

linux安裝

主目錄 /opt/rabbitmq
看相關變數 printenv | grep rabbitmq
執行檔 /opt/rabbitmq/sbin
執行檔 /usr/local/bin

服務啟動
systemctl start rabbitmq-server


mac home brew 安裝

官網說明

相關的scripts及cli tools安裝在/usr/local/opt/rabbitmq/sbin,
需要自己加到path
export PATH=$PATH:/usr/local/opt/rabbitmq/sbin

服務啟動
brew services start rabbitmq
brew services stoprabbitmq
啟動在前景
/usr/local/Cellar/rabbitmq/3.7.11/sbin/rabbitmq-server

管理 rabbitmqctl

用這個程式管理rabbitmq大部分項目,像是增加使用者、列出使用者、重啟、列出參數等。

範例



增加使用者 add_user [userName][password]
rabbitmqctl add_user admin admin
更改使用者tag,改成管理者 set_user_tags [...]
rabbitmqctl set_user_tags admin administrator
更改使用者權限 set_permissions [name]
rabbitmqctl set_permissions -p / username ".*" ".*" ".*"
更改密碼 change_password [username] [password]
rabbitmqctl change_password admin admin
查詢未確認的message(unacknowledged) list_queues [queueName] messages_ready messages_unacknowledged
sudo rabbitmqctl list_queues queueName messages_ready messages_unacknowledged

監控

監控程式啟動在 http://localhost:15672 使用瀏覽器就可以開啟,要先建立一個user
rabbitmq-plugins enable rabbitmq-management

外掛管理

rabbitmq-plugins [-n [node]] [-t [timeout]] [-l] [-q] [command] [command options]

Eg.
rabbitmq-plugins [-n ] [-t ] [-l] [-q] is_enabled [plugin1] [plugin2] 

參數

-n node

-q quiet

-h  help

list 列出所有外掛

is_enabled [plugin1][,[plugin2]]   查看plugin是否啟用

enable [plugin]  啟用外掛

範例


啟用外掛
rabbitmq-plugins enable rabbitmq_management

管理工具

rabbitmqadmin

2019年2月5日 星期二

Docker commands

build

官方

說明:用來建立image

常用參數

-t 指定repository:tag
docker build . -t repository:tag
eg: docker build . it myname/golang:1.14-centos7

-f 指定dockerfile
docker build -f dockerfile.debug

build with RUL

docker build github.com/create/docker-firefox 

run

官方

說明:用來執行docker

使用概念

  • 1 參數
  • 2 綁定
  • 3 執行的image

常用參數

--interactive,-i Keep STDIN open even if not attached

--tty,-t Allocate a pseudo-TTY

--ulimit Ulimit options

--mount Attach a filesystem mount to the container

--add-host Add a custom host-to-IP mapping(host:ip)

--attach,-a Attach to STDIN,STDOUT or STDERR

--link Add link to another container

--name Assign a name to the container

--publish,-p Publish a container's port(s) to the host

--publish-all, -P Publish all exposed ports to random ports

--expose expose a port

--rm Automatically remove the container when it exits

--ip IPv4 Address

--ip6 IPv6 address

-w lets the container command being executed inside directory given

Example

Assign name and allocate pseudo-TTY

$ docker run --name test -it debian
Set working directory

$  docker  run -w /path/to/dir/ -i -t  ubuntu pwd
Exporse port 80 of the container without publishing the port to host
Expose port但是沒有連結到host的外面。
$ docker run --expose 80 ubuntu bash
Publish a port
把container port 發布到host介面。
這裡是綁定8080到 host tcp 80 on 127.0.0.1
$ docker run -p 127.0.0.1:80:8080/tcp ubuntu bash

2019年1月24日 星期四

Linux command netstat

netstat

netstat常用

說明:用來顯示網路系統的資訊(連線、路由表、網路卡等等)

使用概念

  • 第一個參數,決定資訊類型選擇
  • 輸出格式控制(format)

Type of information

(none)  By default, netstat displays a list of open sockets.  
        If you don't specify any address families, 
        then the active sockets of all configured address families will be printed.

--route , -r
       Display the kernel routing tables. See the description in route(8) for details.  
       netstat -r and route -e produce the same output.

--groups , -g
       Display multicast group membership information for IPv4 and IPv6.

--interfaces=iface , -I=iface , -i
       Display a table of all network interfaces, or the specified iface.

--masquerade , -M
       Display a list of masqueraded connections.

--statistics , -s(統計)
       Display summary statistics for each protocol.

None Options(none時候的參數)

netstat  [address_family_options]  
     [--tcp|-t]  [--udp|-u]  [--udplite|-U]  [--sctp|-S]  [--raw|-w]  
     [--listening|-l]  
     [--all|-a]  
     [--numeric|-n]  [--numeric-hosts] [--numeric-ports] [--numeric-users] 
     [--symbolic|-N] 
     [--extend|-e[--extend|-e]] 
     [--timers|-o] [--program|-p] 
     [--verbose|-v] 
     [--continuous|-c] 
     [--wide|-W] 
     [delay]

Common Options

 --verbose , -v
       Tell the user what is going on by being verbose. 
       Especially print some useful information about unconfigured address families.

   --wide , -W
       Do not truncate IP addresses by using output as wide as needed. 
       This is optional for now to not break existing scripts.

   --numeric , -n
       Show numerical addresses instead of trying to determine symbolic host, 
       port or user names.

   --numeric-hosts
       shows numerical host addresses but does not affect the resolution 
       of port or user names.

   --numeric-ports
       shows numerical port numbers but does not affect the resolution 
       of host or user names.

   --numeric-users
       shows numerical user IDs but does not affect the resolution of host or port names.

   --protocol=family , -A
       Specifies  the address families (perhaps better described as low level protocols) 
       for which connections are to be shown.  family is a comma (',') separated list of
       address family keywords like inet, inet6, unix, ipx, ax25, netrom, econet, and ddp.  
       This has the same effect as using the --inet|-4, --inet6|-6, --unix|-x, --ipx,
       --ax25, --netrom, and --ddp options.

       The address family inet (Iv4) includes raw, udp, udplite and tcp protocol sockets.
   -c, --continuous
       This will cause netstat to print the selected information every second continuously.

   -e, --extend
       Display additional information.  Use this option twice for maximum detail.

   -o, --timers
       Include information related to networking timers.

   -p, --program
       Show the PID and name of the program to which each socket belongs.

   -l, --listening
       Show only listening sockets.  (These are omitted by default.)

   -a, --all
       Show both listening and non-listening 
      (for TCP this means established connections) sockets.  
       With the --interfaces option, show interfaces that are not up

   -F
       Print routing information from the FIB.  (This is the default.)

   -C
       Print routing information from the route cache.

   delay
       Netstat will cycle printing through statistics every delay seconds.

Output(重要的輸出項目)

Proto
       The protocol (tcp, udp, udpl, raw) used by the socket.

   Recv-Q
       Established: The count of bytes not copied by the user program connected to this socket.  
       Listening: Since Kernel 2.6.18 this column contains the current syn back‐log.

   Send-Q
       Established: The count of bytes not acknowledged by the remote host.  
       Listening: Since Kernel 2.6.18 this column contains the maximum size of the syn backlog.

   Local Address
       Address and port number of the local end of the socket.  
       Unless the --numeric (-n) option is specified, 
       the socket address is resolved to its canonical  host  name
       (FQDN), and the port number is translated into the corresponding service name.

   Foreign Address
       Address and port number of the remote end of the socket.  Analogous to "Local Address."

   State
       The  state  of the socket. Since there are no states in raw mode 
       and usually no states used in UDP and UDPLite, this column may be left blank. 
       Normally this can be one of several values:
       ESTABLISHED
              The socket has an established connection.

       SYN_SENT
              The socket is actively attempting to establish a connection.

       SYN_RECV
              A connection request has been received from the network.

       FIN_WAIT1
              The socket is closed, and the connection is shutting down.

       FIN_WAIT2
              Connection is closed, and the socket is waiting for a shutdown from the remote end.

       TIME_WAIT
              The socket is waiting after close to handle packets still in the network.

       CLOSE  The socket is not being used.

       CLOSE_WAIT
              The remote end has shut down, waiting for the socket to close.

       LAST_ACK
              The remote end has shut down, and the socket is closed. Waiting for acknowledgement.

       LISTEN The socket is listening for incoming connections.  
              Such sockets are not included in the output 
              unless you specify the --listening (-l) or --all (-a) option.

       CLOSING
              Both sockets are shut down but we still don't have all our data sent.

       UNKNOWN
              The state of the socket is unknown.

   User
       The username or the user id (UID) of the owner of the socket.

   PID/Program name
       Slash-separated pair of the process id (PID) and process name of the process that owns the socket.  
       --program causes this column to be  included.   You  will  also
       need superuser privileges to see this information on sockets you don't own.  
       This identification information is not yet available for IPX sockets.

Example

顯示所有listening or established的tcp

netstat -at


顯示所有listening的tcp

netstat -lt


顯示tcp的統計

netstat -st


顯示每個tcp socket所屬的程式PID跟名稱

netstat -apt  //listening and established
netstat -pt   //established only
netstat -ap | grep ssh   //使用grep篩選

Linux command ulimit

ulimit

說明:顯示/設定使用者、群組等級的shell、process使用的資源限制

使用概念

  • 針對本session shell或session shell中的process做設置
  • 直接顯示或設定資源限制
  • 設定分為soft and hard
  • 使用ulimit做的設定,都是暫時性的(只針對本session)
  • 全系統的永久性修改,須改 /etc/security/limits.conf

Options

  
ulimit [-HSTabcdefilmnpqrstuvx [limit]]

-H: hard limit 執行緒不能在執行中改變數值(除非有root權限)
-S: soft limit 執行緒可在執行中改變數值

不加 -H,-S,默认兩者都设置。

LIMIT 的值,除了可以是数字,也可以是 
    hard, soft, unlimited

    hard: 当前 hard 限制值
    soft: 当前 soft 限制值
    unlimited: 无限制

不加 LIMIT,表示打印对应选项的资源(有多个选项时,会显示资源名和单位):

#ulimit -f
unlimited

#ulimit -c
0

#ulimit -c -f
core file size          (blocks, -c) 0
file size               (blocks, -f) unlimited

             
    -a     All current limits are reported
    -b     The maximum socket buffer size
    -c     The maximum size of core files created
    -d     The maximum size of a process's data segment
    -e     The maximum scheduling priority ("nice")
    -f     The  maximum  size  of files written by the shell and its
          children
    -i     The maximum number of pending signals
    -l     The maximum size that may be locked into memory
    -m     The maximum resident set size (many systems do not  honor
          this limit)
    -n     The maximum number of open file descriptors (most systems
          do not allow this value to be set)
    -p     The pipe size in 512-byte blocks (this may not be set)
    -q     The maximum number of bytes in POSIX message queues
    -r     The maximum real-time scheduling priority
    -s     The maximum stack size
    -t     The maximum amount of cpu time in seconds
    -u     The maximum number of processes  available  to  a  single user
    -v     The  maximum  amount  of  virtual memory available to the
          shell and, on some systems, to its children
    -x     The maximum number of file locks
    -T     The maximum number of threads

    If limit is given, and the -a option is not used, limit  is  the
    new  value  of  the  specified resource.  If no option is given,
    then -f is assumed.  Values are in 1024-byte increments,  except
    for  -t,  which is in seconds; -p, which is in units of 512-byte
    blocks; and -T, -b, -n, and -u, which are unscaled values.   The
    return  status is 0 unless an invalid option or argument is sup‐
    plied, or an error occurs while setting a new limit.

Example

顯示當前user 的open files(n)的hard設定

ulimit -Hn
1024

顯示當前user 所有設定

ulimit -a

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 31204
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024         (file descriptor)
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 31204
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited


把當前user的file descriptor數量暫時修改為10240

#ulimit -n 10240

MySQL 維運

查詢連線數

mysqladmin -u root -p -h127.0.0.1 status

Threads 就是連線數
Uptime: 168765  Threads: 3  Questions: 2265  Slow queries: 0  Opens: 394  

Flush tables: 2  Open tables: 250  Queries per second avg: 0.013

mysql>show full processlist

+----+-----------------+-----------------+------+---------+--------+------------------------+-----------------------+
| Id | User            | Host            | db   | Command | Time   | State                  | Info                  |
+----+-----------------+-----------------+------+---------+--------+------------------------+-----------------------+
|  4 | event_scheduler | localhost       | NULL | Daemon  | 169192 | Waiting on empty queue | NULL                  |
| 16 | root            | localhost:56512 | live | Sleep   |  23923 |                        | NULL                  |
| 18 | root            | localhost       | NULL | Query   |      0 | starting               | show full processlist |
+----+-----------------+-----------------+------+---------+--------+------------------------+-----------------------+

2019年1月23日 星期三

Linux command ps

ps(process status)

說明:用來顯示執行緒的資訊

支援多種options:
UNIX options
BSD options
GNU long options

使用概念

  • 選擇process,simple selection / selection by list
  • 輸出格式控制(format)

Simple selection 詳細參考man ps

 a      Lift the BSD-style "only yourself" restriction, 
             which is imposed upon the set of all processes when some BSD-style (without "-") 
             options are used or when the ps personality setting is BSD-like.  
             The set of processes selected in this manner is in addition to the set of processes selected by other means.  
             An alternate description is that this option causes ps to list all processes with a terminal (tty), 
             or to list all processes when used together with the x option.

       -A     Select all processes.  Identical to -e.

       -a     Select all processes except both session leaders (see getsid(2)) and processes not associated with a terminal.

       -d     Select all processes except session leaders.

       --deselect
              Select all processes except those that fulfill the specified conditions (negates the selection).  
              Identical to -N.

       -e     Select all processes.  Identical to -A.

       g      Really all, even session leaders.  This flag is obsolete and may be discontinued in a future release.  
              It is normally implied by the a flag, and is only
              useful when operating in the sunos4 personality.

       -N     Select all processes except those that fulfill the specified conditions (negates the selection).  
              Identical to --deselect.

       T      Select all processes associated with this terminal.  Identical to the t option without any argument.

       r      Restrict the selection to only running processes.

       x      Lift the BSD-style "must have a tty" restriction, which is imposed upon the set of all processes 
              when some BSD-style (without "-") options are used or
              when the ps personality setting is BSD-like.  The set of processes selected in this manner is in addition 
              to the set of processes selected by other means.
              An alternate description is that this option causes ps to list all processes owned by you (same EUID as ps), 
              or to list all processes when used together
              with the a option.

Selection by list

 ps -p "1 2" -p 3,4
       -123   Identical to --pid 123.
       123    Identical to --pid 123.
-C cmdlist
              Select by command name.  This selects the processes whose executable name is given in cmdlist.
-g grplist
              Select by session OR by effective group name.  Selection by session is specified by many standards, 
              but selection by effective group is the logical behavior that several other operating systems use.  
              This ps will select by session when the list is completely numeric (as sessions are).  Group ID
              numbers will work only when some group names are also specified.  See the -s and --group options.
p pidlist
              Select by process ID.  Identical to -p and --pid.

--sid sesslist
              Select by session ID.  Identical to -s.

-u userlist
              Select by effective user ID (EUID) or name.  This selects the processes 
              whose effective user name or ID is in userlist.

Output control

-f Do full-format listing. 
        This option can be combined with many other UNIX-style options to add additional columns.  
        It also causes the command arguments to be printed.  
        When used with -L, the NLWP (number of threads) and LWP (thread ID) columns will be added.  
        See the c option, the format keyword args, and the format keyword comm.
-F Extra full format.
-j Jobs format
 -o format List user-defined format. 

EXAMPLES

顯示使用者當次登入資訊

[root@li1548-65 live]# ps
  PID TTY          TIME CMD
24539 pts/1    00:00:00 bash
24761 pts/1    00:00:00 ps

列出最耗費記憶體的執行緒

ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head

-e:輸出所有行程
-o:指定輸出欄位,後面接著所有想要輸出的欄位名稱:
pid:行程 ID(process ID)
ppid:父行程 ID(parent process ID)
cmd:程式名稱
%mem:記憶體使用量(百分比)
%cpu:CPU 使用量(百分比)

--sort 參數則是指定排序的依據欄位,
預設會依照數值由小到大排序,
若要由大到小則在欄位名稱前加負號。
-%mem,就是記憶體使用量從大到小排序。

  PID  PPID CMD                         %MEM %CPU
 2124  1278 /usr/lib/chromium-browser/c 25.0 14.4
 1446  1278 /usr/lib/chromium-browser/c 21.5 15.4
 1253     1 /usr/lib/chromium-browser/c 19.2 13.0
 1328  1278 /usr/lib/chromium-browser/c  7.6  2.9
 1392  1278 /usr/lib/chromium-browser/c  7.6  0.5
  732   669 /usr/bin/X :0 -seat seat0 -  5.7  2.0
 1060     1 /usr/lib/arm-linux-gnueabih  1.8  0.0
 1086   758 pcmanfm --desktop --profile  1.5  0.2
 1085   758 lxpanel --profile LXDE-pi    1.5  0.3

輸出pid,ppid,cpu,mem,起始時間....

ps -eo uname,pid,ppid,nlwp,pcpu,pmem,psr,start_time,tty,time,args

USER   PID  PPID NLWP %CPU %MEM PSR START TT       TIME COMMAND
root     1     0    1  0.0  0.1   1 Feb20 ?    00:00:01 /sbin/init
root     2     0    1  0.0  0.0   0 Feb20 ?    00:00:00 [kthreadd]
root     3     2    1  0.0  0.0   0 Feb20 ?    00:02:23 [ksoftirqd/0]
root     6     2    1  0.0  0.0   0 Feb20 ?    00:00:00 [migration/0]
root     7     2    1  0.0  0.0   1 Feb20 ?    00:00:00 [migration/1]
root     9     2    1  0.1  0.0   1 Feb20 ?    00:13:52 [ksoftirqd/1]


列出記憶體使用

ps aux | grep apache2 | awk '{ total += $6; } END { print total/1024"MB" }'


//寫成function
$ vim .bashrc

function memusage() {
    ps aux | grep "$1" | awk '{ total += $6; } END { print total/1024"MB" }'
}

//用法
memusage apache2  //就可以印出記憶體使用量

列出CPU使用

ps aux | grep apache2 | awk '{ total += $3; } END { print total"%" }'

//寫成function
$ vim .bashrc
function cpuusage() {
    ps aux | grep "$1" | awk '{ total += $3; } END { print total"%" }'
}

cpuusage apache2 # 就可以印出總 CPU 使用量
註:cpu 算法是比較奇怪(因為包含多顆 CPU),不過可以把自己的 CPU 數量 * 100%,再來看這個比例。

Linux process 資源使用查詢

cat /proc/[pid]/limits

本process的資源限制設定

Limit                     Soft Limit           Hard Limit           Units     
Max cpu time              unlimited            unlimited            seconds   
Max file size             unlimited            unlimited            bytes     
Max data size             unlimited            unlimited            bytes     
Max stack size            8388608              unlimited            bytes     
Max core file size        0                    unlimited            bytes     
Max resident set          unlimited            unlimited            bytes     
Max processes             31204                31204                processes 
Max open files            1024                 4096                 files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       31204                31204                signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us 

cat /proc/[pid]/statm

180024 1241 1046 35 0 171703 0

Size (pages) 任务虚拟地址空间的大小 VmSize/4
Resident(pages) 应用程序正在使用的物理内存的大小 VmRSS/4
Shared(pages) 共享页数 0
Trs(pages) 程序所拥有的可执行虚拟内存的大小 VmExe/4
Lrs(pages) 被映像到任务的虚拟内存空间的库的大小 VmLib/4
Drs(pages) 程序数据段和用户态的栈的大小 (VmData+ VmStk )4

cat /proc/[pid]/status

本process當前資源使用狀態

Name: cat 
State: R (running) 
SleepAVG: 88% 
Tgid: 5783 
Pid: 5783 
PPid: 5742 
TracerPid: 0 
Uid: 0 0 0 0 
Gid: 0 0 0 0 
FDSize: 256 
Groups: 0 1 2 3 4 6 10 
VmSize: 6588 kB 
VmLck: 0 kB 
VmRSS: 400 kB 
VmData: 144 kB 
VmStk: 2040 kB 
VmExe: 14 kB 
VmLib: 1250 kB 
StaBrk: 0804e000 kB 
Brk: 088df000 kB 
StaStk: bfe03270 kB 
ExecLim: 0804c000 
Threads: 1 
SigPnd: 0000000000000000 
ShdPnd: 0000000000000000 
SigBlk: 0000000000000000 
SigIgn: 0000000000000000 
SigCgt: 0000000000000000 
CapInh: 0000000000000000 
CapPrm: 00000000fffffeff 
CapEff: 00000000fffffeff 

參數解釋

Name 应用程序或命令的名字 
State 任务的状态,运行/睡眠/僵死/ 
SleepAVG 任务的平均等待时间(以nanosecond为单位),交互式任务因为休眠次数多、时间长,它们的 sleep_avg 也会相应地更大一些,所以计算出来的优先级也会相应高一些。 
Tgid 线程组号 
Pid 任务ID 
Ppid 父进程ID 
TracerPid 接收跟踪该进程信息的进程的ID号 
Uid Uid euid suid fsuid 
Gid Gid egid sgid fsgid 
FDSize 當前文件描述符的分配個數,32位元系統將以32倍數遞增,
64位元系統以64倍數遞增。
此數字只會增加不會減少。

列出當前使用的文件
ls -l /proc/`pgrep rsyslogd|grep -v grep`/fd  


Groups 
VmSize(KB) 任务虚拟地址空间的大小 (total_vm-reserved_vm),其中total_vm为进程的地址空间的大小,reserved_vm:进程在预留或特殊的内存间的物理页 
VmLck(KB) 任务已经锁住的物理内存的大小。锁住的物理内存不能交换到硬盘 (locked_vm) 
VmRSS(KB) 应用程序正在使用的物理内存的大小,就是用ps命令的参数rss的值 (rss) 
VmData(KB) 程序数据段的大小(所占虚拟内存的大小),存放初始化了的数据; (total_vm-shared_vm-stack_vm) 
VmStk(KB) 任务在用户态的栈的大小 (stack_vm) 
VmExe(KB) 程序所拥有的可执行虚拟内存的大小,代码段,不包括任务使用的库 (end_code-start_code) 
VmLib(KB) 被映像到任务的虚拟内存空间的库的大小 (exec_lib) 
VmPTE 该进程的所有页表的大小,单位:kb 
Threads 共享使用该信号描述符的任务的个数,在POSIX多线程序应用程序中,线程组中的所有线程使用同一个信号描述符。 
SigQ 待处理信号的个数 
SigPnd 屏蔽位,存储了该线程的待处理信号 
ShdPnd 屏蔽位,存储了该线程组的待处理信号 
SigBlk 存放被阻塞的信号 
SigIgn 存放被忽略的信号 
SigCgt 存放被俘获到的信号 
CapInh Inheritable,能被当前进程执行的程序的继承的能力 
CapPrm Permitted,进程能够使用的能力,可以包含CapEff中没有的能力,这些能力是被进程自己临时放弃的,CapEff是CapPrm的一个子集,进程放弃没有必要的能力有利于提高安全性 

幾個 process 相關的

ls /proc/PID/cmdline : process arguments
ls /proc/PID/cwd : process current working directory (symlink)
ls /proc/PID/exe : path to actual process executable file (symlink)
ls /proc/PID/environ : environment used by process
ls /proc/PID/root : the root path as seen by the process. For most processes this will be a link to / 
        unless the process is running in a chroot jail.
cat /proc/PID/status : basic information about a process including its run state and memory usage.

ls /proc/PID/task : hard links to any tasks that have been started by this (the parent) process.

列出 process fd

//看當前配置的 fd size
cat /proc/pid/status

//列出詳細 fd 內容
sudo ls -la /proc/pid/fd
//計算總數
ls -l /proc/28290/fd | wc -l

sudo lsof -p 28290
sudo lsof -a -p 28290


顯示 socket fd 詳細內容

sudo lsof -i | grep 4654214  //socket fd 
sudo netstat -alep | erep -i 4654214  //socket fd

2019年1月21日 星期一

MySQL 重整及optimize

REPARE TABLE table_name;
OPTIMIZE TABLE table_name;

or

$mysqlcheck -u root -p --auto-repair --optimize --all-databases


INNODB table不直接支援optimize,
而是建立新table再把資料copy過去,然後分析,

所以會顯示:
Table does not support optimize, doing recreate + analyze instead

MySQL 字元集 character set

How to support full Unicode in MySQL databases
Mysql字元集詳解
Mysql utf8mb4說明

character set 字元集

utf8mb4=utf8 most byte 4 是比utf8更完整的字元集

collation 字元排序方式

utf8mb4_unicode_ci 是依據unicode排序
utf8mb4_general_ci 無依據unicode排序,所以排序可能某些字元會不如預期

實作上只要全部統一就好,table跟mysql 系統相同,
用utf8mb4_unicode_ci或utf8mb4_general_ci都可以

檢查字元集指令

mysql> SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' 
OR Variable_name LIKE 'collation%';

字元集變數

character_set_client | utf8mb4 |
character_set_connection | utf8mb4 |
character_set_database | utf8mb4 |
character_set_filesystem | binary |
character_set_results | utf8mb4 |
character_set_server | utf8mb4 |
character_set_system | utf8 |
character_sets_dir | /usr/share/mysql-8.0/charsets/

變數說明

character_set_client
客户端请求数据的字符集

character_set_connection
从客户端接收到数据,然后传输的字符集

character_set_database
默认数据库的字符集,无论默认数据库如何改变,都是这个字符集;如果没有默认数据库,那就使用 character_set_server指定的字符集,这个变量建议由系统自己管理,不要人为定义。

character_set_filesystem
把os上文件名转化成此字符集,即把 character_set_client转换character_set_filesystem, 默认binary是不做任何转换的

character_set_results
结果集的字符集

character_set_server
数据库服务器的默认字符集

character_set_system
这个值总是utf8,不需要设置,是为存储系统元数据的字符集


設定完整支援utf8mb4

分兩個部分 1 config中設定  2 建立table及建立column時設定

1.
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

2.
# For each database:
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

# For each table:
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

# For each column:
ALTER TABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;