แสดงบทความที่มีป้ายกำกับ vi editor แสดงบทความทั้งหมด

วันพุธที่ 26 สิงหาคม พ.ศ. 2558

Percona XtraDB Cluster 5.6 (3 node) With Docker

Percona XtraDB Cluster 5.6 (3 node) With Docker


    ในช่วงนี้ต้องบอกว่ายุคของ containers ผมก็เลยลองทำ Docker สักหน่อย อยากทำเป็น database cluster เลยเลือก Percona XtraDB Cluster 5.6 เพราะเป็น open source มาทำ (กำลังศึกษา อาจอธิบายอะไรผิดพลาดต้องขออภัยด้วยนะครับ ปล.ต้องขอบคุณคอร์สเอื้ออาทรสุดๆจาก Links Innovation)

1.ทำการ pull image ของ ubuntu มาก่อน
2.สร้าdirectoryไว้สำหรับเก็บ image ของเรา เช่น
mkdir ubuntu-percona
cd ubuntu-percona
3.สร้าง Dockerfile ที่จะรวมคำสั่งในการสร้าง image ของเราขึ้นมา
vi Dockerfile

FROM ubuntu:trusty
MAINTAINER GuutonG credit by [Percona.com]
CMD ["/bin/bash"]
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-key adv --keyserver keys.gnupg.net --recv-keys 1C4CBDCDCD2EFD2A
ADD percona.list /etc/apt/sources.list.d/percona.list
RUN apt-get update
RUN apt-get install -y percona-xtradb-cluster-56 qpress xtrabackup
RUN apt-get install -y python-software-properties vim wget curl netcat

และสร้าง percona.list สำหรับลง มำ repo ไปยัง percona
vi percona.list
deb http://repo.percona.com/apt trusty main
deb-src http://repo.percona.com/apt trusty main
4.ทำการbuild Dockerfile ที่เราสร้างไว้
docker build -t ubuntu:gtpxc .
5. ทำการ run ubuntu:gtpxc ออกมา3 containner

for n in {1..3}; do docker run --name gtpxc$n -i -t -d ubuntu:gtpxc bash; done
6.เรียกดู docker ที่ทำงานอยู่

docker ps
7.เช็คIP ของแต่ละ container
docker inspect gtpxc1 | grep IPAddress
docker inspect gtpxc2 | grep IPAddress
docker inspect gtpxc3 | grep IPAddress
ในที่นี้จะได้ IP

8.เปิด Docker Terminal ขึ้นมาใหม่ 3 อัน แล้วใช้คำสั่ง docker attach [name]เพื่อ remote เข้าไปยัง container

docker attach gtpxc1
docker attach gtpxc2
docker attach gtpxc3


Ubuntu:[gtpxc1]

vi /etc/mysql/my.cnf

#GT-PXC Node1
[mysqld]
user = mysql
default_storage_engine = InnoDB
basedir = /usr
datadir = /var/lib/mysql
socket = /var/run/mysqld/mysqld.sock
port = 3306
innodb_autoinc_lock_mode = 2
log_queries_not_using_indexes = 1
max_allowed_packet = 128M
binlog_format = ROW

wsrep_provider = /usr/lib/libgalera_smm.so
wsrep_cluster_name="docker-gtpxc"
#IP node1,node2,node3
wsrep_cluster_address = gcomm://[IP node1],[IP node2],[IP node3]
#Name node1
wsrep_node_address = [IP node1]
wsrep_node_name = gtpxc1
wsrep_slave_threads = 4
wsrep_sst_method = xtrabackup-v2
wsrep_sst_auth = gtuser:gtpassword

[sst]
streamfmt = xbstream

[xtrabackup]
compress
compact
parallel = 2
compress_threads = 2

rebuild_threads = 2

ใช้คำสั่งเพื่อ start percona xtradb cluster
/etc/init.d/mysql bootstrap-pxc
และสร้าง user pass กลางสำหรับใช้ร่วมกัน

mysql>CREATE USER 'gtuser'@'localhost' IDENTIFIED BY 'gtpassword';
mysql>GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'gtuser'@'localhost';
mysql>FLUSH PRIVILEGES;
ตรวจสอบค่าต่างของ cluster
mysql>show global status like 'wsrep%';
Ubuntu:[gtpxc2]
แก้ไขไฟล์ my.cnf และใช้คำสั่งเพื่อ start mysql
vi /etc/mysql/my.cnf
/etc/init.d/mysql start
Ubuntu:[gtpxc3]
แก้ไขไฟล์ my.cnf และใช้คำสั่งเพื่อ start mysql
#vi /etc/mysql/my.cnf
#/etc/init.d/mysql start
Ubuntu:[gtpxc1]

mysql>show global status like 'wsrep%';

+------------------------------+------------------------------------------------
--+
| Variable_name | Value
|
+------------------------------+------------------------------------------------
--+
| wsrep_local_state_uuid | 1bb5bf25-4c05-11e5-b0af-ebfa440f9b9e
|
| wsrep_incoming_addresses | 172.17.0.4:3306,172.17.0.5:3306,172.17.0.6:3306
|
| wsrep_evs_repl_latency | 0.00041831/0.000710346/0.000908118/0.000180007/
4 |
| wsrep_evs_state | OPERATIONAL
|
| wsrep_gcomm_uuid | 1bb555bd-4c05-11e5-95a6-cfcc3cc31436
|
| wsrep_cluster_conf_id | 3
|
| wsrep_cluster_size | 3
|
| wsrep_cluster_state_uuid | 1bb5bf25-4c05-11e5-b0af-ebfa440f9b9e
|
| 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 | 3.11(r93aca2d)
|
| wsrep_ready | ON
|
+------------------------------+------------------------------------------------
--+
[TEST]


Time
Ubuntu:[gtpxc1] Ubuntu:[gtpxc2] Ubuntu:[gtpxc3]
1
create database gtpxcDB;



2


show databases;
+--------------------+
| Database |
+--------------------+
| gtpxcDB |
+--------------------+


3




gtpxcDB;
create table tbl1 (id INT PRIMARY KEY, name VARCHAR(30));
INSERT INTO gtpxcDB.tbl1 VALUES (1, 'gtpxc1');
INSERT INTO gtpxcDB.tbl1 VALUES (2, 'gtpxc2');
INSERT INTO gtpxcDB.tbl1 VALUES (3, 'gtpxc3');
4
show tables;
+-------------------+
| Tables_gtpxcDB |
+-------------------+
| tbl1 |
+-------------------+




5


use gtpxcDB;
select * from tbl1;
+----+--------+
| id | name |
+----+--------+
| 1 | gtpxc1 |
| 2 | gtpxc2 |
| 3 | gtpxc3 |
+----+--------+






credit by: Docker,Percona XtraDB Cluster 5.6 Documentation,Jericho Rivera

วันอาทิตย์ที่ 14 มิถุนายน พ.ศ. 2558

[HOW TO] LINE Bot on Raspberry PI with LINE API Python

[HOW TO] LINE Bot on Raspberry PI with LINE API Python   

1. ทำการอัพเดท packet ของ raspbian ก่อน
sudo apt-get install update
sudo apt-get install upgrade
 2. ซึ่ง [LINE API Python] จำเป็นต้องลง packet python 
sudo apt-get install python
sudo apt-get install python-pip
3. ทำการ download [LINE API Python] จาก https://github.com/carpedm20/LINE ในนี้จะมีทั้งวิธีใช้ด้วย
wget https://github.com/carpedm20/LINE/archive/master.zip
unzip master.zip
cd LINE-master/
4. ทำการแก้ไข ไฟล์ api.py ให้สามารถเชื่อมต่อกับ server หลักได้
ทำการเพิ่ม สคริป ภายใต้
def ready(self):
  """
  After login, make `client` and `client_in` instance
  to communicate with LINE server
  """
เอาเครื่องหมาย + ออกด้วย
+        self.transport = THttpClient.THttpClient(self.LINE_HTTP_URL)
 +        self.transport_in = THttpClient.THttpClient(self.LINE_HTTP_IN_URL)
 +        self.transport.setCustomHeaders(self._headers)
 +        self.transport_in.setCustomHeaders(self._headers)
 +        self.protocol = TCompactProtocol.TCompactProtocol(self.transport)
 +        self.protocol_in = TCompactProtocol.TCompactProtocol(self.transport_in)
 +        self._client = CurveThrift.Client(self.protocol)
 +        self._client_in = CurveThrift.Client(self.protocol_in)
 +        self.transport.open()
 +        self.transport_in.open()

5. ทำการรันไฟล์ config.py และ ติดตั้งด้วย setup.py
python config.py
python setup.py install

6. หากไม่มี Error  ใดๆในการติดตั้ง ให้ทดลองการส่งข้อความด้วยไฟล์ echobot.py ใน examples (อย่าลืมเข้าไปแก้ไฟล์ โดยการใส่ ID และ Pass ด้วยนะครับ)
vi echobot.py



เพิ่มเติม script checks status
vi check.py
import subprocess
from line import LineClient, LineGroup, LineContact

try:
    client = LineClient("ID", "PASS")
    #client = LineClient(authToken="TOKEN")
except:
    print "Login Failed"

while True:
    op_list = []

    for op in client.longPoll():
        op_list.append(op)

    for op in op_list:
        sender   = op[0]
        receiver = op[1]
        message  = op[2]

        msg = message.text
        if 'GetRAM' in msg:
                proc=subprocess.Popen('egrep --color "Mem|Cache|Swap" /proc/meminfo', shell=True, stdout=subprocess.PIPE, )
                output=proc.communicate()[0]
                sender.sendMessage("[%s] %s" % (sender.name, output))

        if 'GetDATE' in msg:
                proc=subprocess.Popen('date', shell=True, stdout=subprocess.PIPE, )
                output=proc.communicate()[0]
                sender.sendMessage("[%s] %s" % (sender.name, output))

        if 'GetKERNAL' in msg:
                proc=subprocess.Popen('uname -a', shell=True, stdout=subprocess.PIPE, )
                output=proc.communicate()[0]
                sender.sendMessage("[%s] %s" % (sender.name, output))


ขอบคุณ Api ดีๆจาก https://github.com/carpedm20/LINE
เครดิต : ตาเล็ก วินโด้

วันอังคารที่ 28 เมษายน พ.ศ. 2558

[HOW TO] Generate word ด้วย Python script

Generate word ด้วย Python script(หัดทำ)
วิธีรัน 
1.เข้า Terminal ด้วยสิทธิ์ Root
2.เมื่อสร้างไฟล์เสร็จให้ทำการเปลี่ยนไฟล์ให้ execute ได้ ด้วย
chmod +x genword.py
3. รันด้วยคำสั่ง 
./genword.py -f <fileName.txt> -n <line Of Number> -w <word Generate>



#!/usr/bin/python
######################################################
#____________________________________________________#
#_______________Mr.Pornmongkon Pongsai_______________#
#________________Computer Engineering________________#
#_____________Generate Word for Project______________#
#____________Create Date : 28 April 2015_____________#
#____________________________________________________#
######################################################
import sys
import getopt

def main(argv):
 fileName = 'default'
 lineNumber = 0
 wordGen = 'default'
 try:
  opts, args = getopt.getopt(argv,"hf:n:w:",["file=","number=","word="])
 except getopt.GetoptError:
  print 'Help : genword.py -f  -n  -w '
  sys.exit(2)
 for opt, arg in opts:
  if opt == '-h':
   print 'Help : genword.py -f  -n  -w '
   sys.exit()
  elif opt in ("-f", "--file"):
   fileName = arg
  elif opt in ("-n", "--number"):
   lineNumber = int(arg)
  elif opt in ("-w", "--word"):
   wordGen = arg  
 print '[log] : generate word start!'
 dataFile = open(fileName, 'w')
 i = 0
 while ( i < lineNumber ):
  dataFile.write(wordGen+'\n')
  i = i + 1
 dataFile.close()
 print '[log] : generate word finish!'
if __name__ == "__main__":
 print '######################################################'
 print '#____________________________________________________#'
 print '#_______________Mr.Pornmongkon Pongsai_______________#'
 print '#________________Computer Engineering________________#'
 print '#_____________Generate Word for Project______________#'
 print '#____________Create Date : 28 April 2015_____________#'
 print '#____________________________________________________#'
 print '######################################################'
 print '!!!-h for help!!!'
 main(sys.argv[1:])

วันพุธที่ 20 สิงหาคม พ.ศ. 2557

[HOW TO] Use vi Editor Basic

การใช้งาน vi editor เบื้องต้น
vi เป็น text editor หรือ เป็นโปแกรม editor ใช้สำหรับสร้าง, เพิ ,ลบ หรือ แก้ไข ไฟล์ข้อมูล
เครดิตรูป:www.viemu.com
แต่ใน vim จะมีส่วนที่มีการแก้ไขและปรับปรุงให้ดีขึนกว่า vi และยังมี vimtutor ด้วย เพื่อฝึกการใช้ vi ให้ชำนาญมากขึ้
การติดตั้ง
-Linux Redhat , Fedora หรือ CentOS
yum -y install vim
yum -y install vim-enhanced
-Linux Debian หรือ Ubuntu
apt-get install vim
ลองดู
$ vimtutor

จะสอนการใช้ vi เป็นบทๆไป
บทนี้จะเป็นการเลื่อน cursor ใน vi
การออกจาก vi เป็นต้น ค่อยๆอ่านก็จะสามารถเรียนรู้ได้
การสร้างไฟล์ใหม่
การสร้างไฟล์ใหม่เมื่ออยู่บน shell prompt
    $  vi  <filename>
การสร้างไฟล์ใหม่ ขณะทำงานอยู่บนโปรแกรม vi             
              เข้าสู่โหมด command(Escape)  -> พิมพ์   :  (เครื่องหมาย colon ) เพื่อเข้าสู่ command prompt
              -> พิมพ์  e(หรือ edit)  ตามด้วยชื่อไฟล์        
e   <filename>
การแก้ไขไฟล์
การแก้ไขไฟล์เมื่ออยู่บน shell prompt
$  vi  <path/filename>
ตัวอย่าง
$  vi  /etc/httpd/conf/httpd.conf เป็นต้น
การเปลี่ยนโหมดการทำงาน
 command -> insert      พิมพ์  หรือ  a
 insert -> command      กดปุ่ม escape หรือ Ctrl+[  
ให้สังเกตที่มุมล่างซ้ายของโปรแกรม ถ้าเป็นโหมด insert
จะมีข้อความ --insert-- ปรากฏอยู่
การบันทึกไฟล์ระหว่างการทำงาน
มีขั้นตอนดังนี้
เข้าสู่โหมด command (กด Escape)
พิมพ์   :   (เครื่องหมาย colon) เพื่อเข้าสู่ command prompt
พิมพ์  w  ( หรือ write)  และกด enter เพื่อบันทึกไฟล์
     : w  <filename>
การออกจากการทำงาน
         command มีอยู่ 3 กรณีดังนี้
กรณีที่ 1 บันทึกไฟล์แล้ว 
พิมพ์ :  (เข้าสู่ command prompt) -> พิมพ์ q (หรือ quit) + Enter เพื่อออกจากโปรแกรม
กรณีที่ 2  บันทึกไฟล์พร้อมออกจากโปรแกรม
พิมพ์ : ( เข้าสู่ command prompt) -> พิมพ์ wq ( มาจากคำว่า write+quit ) + Enter เพื่อบันทึกและออกจากโปรแกรม
กรณีที่ 3 ออกจากโปรแกรมโดยไม่ต้องการบันทึกไฟล์
พิมพ์ : (เข้าสู่ command prompt) -> พิมพ์ q!  + Enter เพื่อออกจากโปรแกรม