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, 其他不需要

MySQL NDBCluster 與 InnoDB storage engine 差異

 Official document




MySQL 大量資料 insert 加速方式

 這裏


Bulk inserts of data(區塊式 insert)

  • Extended inserts
     一次 insert 多筆

  • Transactions
      把 insert 放到 transaction 裡

Avoid indexes

先 drop index, 等 insert 完成再加回去

Skipping the binary log

暫時關閉 binary log
set sql_log_bin=0;

Multiple connections


Truncate table with foreign keys


Index is used for sort 

搜尋覆蓋

2020年12月31日 星期四

MySQL 記一次還原指定日期資料且避開auto increment問題

 1 mysqldump 只能備份所有欄位

 無法跳過有auto increment column,

所以必須複製一個相同table, 然後刪掉不要的欄位,再dump出來

2 mysqldump 常用參數

//用cluster 請勿使用 --no-autocommit

mysqldump -u root -p --skip-add-drop-table --skip-add-locks --no-create-db --no-create-info --no-autocommit --complete-insert --net-buffer-length=1000000 --max-allowed-packet=1000000000 

--skip-add-drop-table  還原時不drop原有table

--skip-add-locks  還原時不lock

--no-create-db 還原時不create db

--no-create-info 還原時不create table

--no-autocommit 還原時不auto commit

--complete-insert 備份完整scheme

--net-buffer-length=1000000 加大網路buffer, 適用大檔還原

--max-allowed-packet=1000000000 加大網路packet,適用大檔還原

3 完整mysqldump參數

mysqldump -u root -p --skip-add-drop-table --skip-add-locks --no-create-db --no-create-info --complete-insert --no-autocommit --net-buffer-length=1000000 --max-allowed-packet=1000000000 --where="(date >='2020-12-21' and date<'2020-12-22')" db table | gzip >  table.sql.gz

只dump db 的 指定table內的 where 條件的資料

4 還原(因為備份時有指定參數,所以大檔也適用)

myslq login

use db;

source /home/..... sql file


5 完整流程

CREATE new_table LIKE old_table;

INSERT INTO new_table SELECT * FROM old_table;

ALTER TABLE new_table DROP COLUMN column;

mysqldump ....

restore ....

2020年12月2日 星期三

MySQL規格與基本優化

 

MySQL版本

5.7 (配合gcp 的 cloud sql)

DB引擎

InnoDB

DB規格

character 使用 UTF8mb4
collate 使用 utf8mb4_unicode_ci
CREATE DATABASE  database_name 
	character set UTF8mb4 collate utf8mb4_unicode_ci;

Table規格

character 使用 UTF8mb4
collate 使用 utf8mb4_unicode_ci
CREATE TABLE tbl_name (column_list) ENGINE=INNODB 
	CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

my.cnf設定

[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
#紀錄slow query 
log-slow-queries = /var/log/mysql/mysql-slow.log 
long_query_time = 2
log-long-format
key_buffer 能夠把所有index 載入最好

基本優化觀念

至少看以下項目:
  • 1 資料正規化/反正規化
  • 2 Primary Key / Index 建立概念
  • 3 資料庫記憶體管理
  • 4 基本效能評估

  • 基本優化方向

  • 1 基本上遵循資料正規化,有必要時就反正規化,不必拘泥於規則。
  • 2 減少資料量
  • 3 使用速度快的資料類型
  • 4 優化查詢語句速度
  • 5 硬體的影響:RAM > SSD > CPU。MySQL可以快取,RAM越大越好。

  • 基本優化項目

  • 1 RAM最好>=DB資料的3倍,避免swap SSD。
  • 2 資料類型的SQL計算速度
  • 1boolean > int > timestamp > datetime > double > char > varchar
  • 3 日期盡量用timestamp,因為速度快,且有cache
  • timestamp儲存UTC時間,會依據time zone設定返回不同時間


  • 4 有where就要建index,有order就要建index
  • primary key / index 屬性速度
    1boolean > int > char > varchar
    
  • 5 ID盡量不要使用string,因為ID是所有查詢的基本,使用string 佔空間、速度又慢。
  • 6 可以算的先算,不要讓MySQL去算。
  • 雖然MySQL有很多內建函式,但是不會比你程式算的快,而且不好維護。
    
    //使用MySQL內建函式
    SELECT * FROM table WHERE DATE_ADD(NOW(),INTERVAL 10 DAY) > create_date;
    
    //更好,先算好時間
    SELECT * FROM table WHERE DATE_ADD('2019-01-01 00:00:00',INTERVAL 10 DAY) > create_date;
    
    //更好,改成int
    SELECT * FROM table WHERE DATE_ADD(1324335674,INTERVAL 10 DAY) > create_date;
    
  • 7 order by 的標的要設成index,因為order by會讓MySQL指定key做index搜尋,權重大於WHERE條件式。