Sunday, July 16, 2017

MySQL难点


select ... for update将会锁住任何其他connection的select for update, update, insert。当然还要放入到transaction中。

CREATE DEFINER=`qfu`@`%` PROCEDURE `updateIndex`(IN batchNum INT)
BEGIN

  DROP TEMPORARY TABLE IF EXISTS temp;
  create temporary table temp(id int);
  START TRANSACTION;
  insert into temp
  select id from Dow_Jones_Index2 where isUpdated=0 limit batchNum for update;
  update Dow_Jones_Index2 set isUpdated=1 where id in (select id from temp);
  commit;
  select * from Dow_Jones_Index2 where id in (select id from temp);
  DROP TEMPORARY TABLE IF EXISTS temp;  
END

临时表生命期是session内,如果数据库连接断了,临时表也会消失。相当于mssql中的#temp

---
explain select * from Student where id=5;
可以解释是否用了index。
Using where: 没用到index
Using index: index含有所有信息不需要再查表。The difference is that "Using index" doesn't need a lookup from the index to the table, while "Using index condition" sometimes has to.
Using index condition: 需要再查表

clear cache for performance tuning (workbench)
RESET QUERY CACHE;

ref:
select for update

Saturday, June 24, 2017

Alexa Skill: Word Helper

Word Helper

{
  "intents": [
    {
      "intent": "AMAZON.HelpIntent"
    },
    {
      "intent": "AMAZON.StopIntent"
    },
    {
      "intent": "AMAZON.CancelIntent"
    },
    {
      "intent": "WordHelperAddIntent",
      "slots": [
        {
          "name": "Word",
          "type": "AMAZON.Food"
        }
      ]
    },
    {
      "intent": "WordHelperGetIntent"
    },
    {
      "intent": "WordHelperDeleteIntent",
      "slots": [
        {
          "name": "Word",
          "type": "AMAZON.Food"
        }
      ]
    }
  ]
}

WordHelperAddIntent add {Word}
WordHelperAddIntent put {Word}
WordHelperAddIntent save {Word}
WordHelperDeleteIntent remove {Word}
WordHelperDeleteIntent delete {Word}
WordHelperGetIntent tell me a word
WordHelperGetIntent  give me a word
WordHelperGetIntent tell me something
WordHelperGetIntent give me something

Lambda Test:
use Alexa Intent - GetNewFact


Skill Test:
add bacon
put scrambled egg
save chocolate cake
tell me a word
give me something
remove chocolate cake
delete lemon juice


Ref:
Create a skill steps
Example: Skill with Lambda
How to Config intent with parameter
Skill NodeJS lambda example
Skill NodeJS project example
Skill Java project example
Slot Type reference


https://stackoverflow.com/questions/41358552/how-to-get-the-account-info-of-the-user-when-the-user-uses-an-alexa-skill

Sunday, June 18, 2017

AWS Pipeline简介


CodeCommit: 代码repository。只要有代码commit就会触发pipeline
CodeBuild: 选用python:2.7.12来编译代码,Python需要写buildspec.yml (Python例子),target files表示要copy到目标机器的文件
CodeDeploy: 配置要deploy的目标机器,在目标机器安装CodeDeploy agent,以及触发初始run







































CodeDeploy official
CodeDeploy other

Thursday, June 15, 2017

Python常用library

Linux
sudo apt-get install python2.7


SQLAlchemy: python最流行的ORM工具
Logging: http://www.jianshu.com/p/feb86c06c4f4
YAML: http://www.ruanyifeng.com/blog/2016/07/yaml.html

__init__: 双下划线为保留函数
__name: 私有函数/属性

person_1.py:

class Person:

   def __init__(self):
       self.__name = 'haha'#私有属性
       self.age = 22

   def __get_name(self):##私有方法
       return self.__name

   def get_age(self):
       return self.age

person = Person()
print person.get_age()
print person.__get_name()

不带self就是与object无关
def read_tag(file_name, tag, freq_cutoff=-1, init_dict=True):

Dictionary.read_tag(file_name, 'dialogue', freq_cutoff=freq_cutoff)

带self就是与object相关
def add_word(self, word):

dictionary.add_word(token)

继承:
http://www.cnblogs.com/feeland/p/4419121.html



Python语法

Wednesday, June 7, 2017

Squid反向代理简介 - Linux



sudo su
yum update
yum upgrade
yum -y install squid

vi /etc/squid/squid.conf

http_access allow all

sudo service squid stop
sudo service squid start

https://www.youtube.com/watch?v=BkjKINJIHsk



Sunday, May 21, 2017

Squid反向代理简介 - Ubuntu

ifconfig

inet addr:172.31.64.201  Bcast:172.31.79.255  Mask:255.255.240.0
sudo su
vi /etc/network/interfaces

iface lo inet static
 address 172.31.64.201
 netmask 255.255.240.0
 network 172.31.79.0
 broadcast 172.31.79.255
 gateway 172.31.79.1
 dns-nameservers 172.31.79.1 8.8.8.8


use ec2 console to reboot (status changed in 4 mins)

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install squid3

sudo nano /etc/squid3/squid.conf

--------------------------------------------------------
Ctrl+W search/next search result
Ctrl+X exit
Ctro+O write

(cache is disable by default)
visible_hostname testproxy
http_port 3128
acl network src 172.31.0.0/20
http_access allow network
(http_access allow all, http://ipcalc.nmonitoring.com/)

cache_peer 10.1.2.3 parent 80 0 no-query default login=my_username:my_password
never_direct allow all
--------------------------------------------------------

sudo service squid3 stop
sudo service squid3 start

inbound TCP rule: 3128 source: 0.0.0.0/0

Firefox:
in setting: 172.31.64.201(privite IP in AWS) and port 3128
in browser link type 172.31.64.201->see Access Denial page

Connection to 172.31.64.201 failed -> good

Log file:
/var/log/squid/access.log


Proxy:
Install node.js
sudo apt-get install build-essential curl git m4 ruby texinfo libbz2-dev libcurl4-openssl-dev libexpat-dev libncurses-dev zlib1g-dev

54.211.223.78



https://www.youtube.com/watch?v=iKtkp80gV04

http://www.cnblogs.com/derekchen/archive/2011/02/25/1964909.html

Thursday, May 18, 2017

AWS EC2和VPC简介

SSH连接EC2步骤:

1. 下载ppk文件(私钥文件)
2. 用PuTTYgen转化成Putty能读的私钥文件my-key-pair
3. 用PuTTY输入用户名ubuntu@DNS,SSH->Auth引入步骤2产生的文件作为密码。不同的EC2可以share同一个key,但用putty登录时同样要SSH->Auth引入步骤2产生的文件作为密码。
更多

模板

用现有instance模板产生更多此类实例(比如已经安装了很多软件)
1. 在“实例”页面上,选择要使用的实例。
2. 选择 Actions,然后选择 Launch More Like This。
更多
或者创建一个实例模板AMI(amazon machine image)
1. 在“实例”页面上,选择要使用的实例。
2. 选择 Actions,然后选择 Images->Create image。
这样创建实例时候可以从my AMIs中创建
更多


terminate vs stop

区别在于stop只是关机,硬盘(EBS)还在,terminate就是整个instance都会被删掉

界面GUI

1. 安装
sudo -s
sudo apt-get update
sudo apt-get install ubuntu-desktop
sudo apt-get install vnc4server
sudo apt-get install gnome-panel

2. 初始化进程
vncserver
vncserver -kill :1

3. 修改config
vi .vnc/xstartup
删掉第一行注释:
unset SESSION_MANAGER
加入
gnome-session -session=gnome-classic &
gnome-panel&

4. 开始进程
vncserver

5. 打开5901端口
在security groups中的inbound规则加入5901端口,而source为0.0.0.0/0。有一点要特别注意的是,一定要在该instance对应的security group加入端口,若不在正确的security group上加,就不能被访问。

6. TightVPC
连接publicIP::5901

https://www.youtube.com/watch?v=ljvgwmJCUjw (最好的指南)
http://stackoverflow.com/questions/25657596/how-to-set-up-gui-on-amazon-ec2-ubuntu-server

VPC:

如果EC2只是内部资源,不需要暴露在public(如非网站)。如果给ec2一个public IP,这样很容易被攻击,所以可以只给ec2内部IP,确保它在VPC内部,然后加入带public IP的bastion host作为进入VPC的入口。

bastion host: public IP, sg1->只允许公司内部IP的端口22的访问(不要开放给所有IP)
vm01:, private IP, sg2->只允许sg1的端口22的访问,且outbound允许所有IP的80和443端口流出

Putty只要登录bastion host,登陆后再ssh 到vm01。至于bastion host的设置在SSH->Auth中要勾上Allow agent forwarding并且留空Private key file for authentication,并且开着Pageant(windows service),把key放入即可(原理是把key放入内存中). 参见教程


















在private的EC2默认不能访问Internet,所以可以在VPC中的Elastic IPs创建一个分配给该EC2。private的EC2默认没有outbound rule,与public的EC2默认允许All trafic, all ports完全不同。所以对每种服务都要加入到outbound,如EC2要读写RDS,就要允许通过3306访问RDS的security group。

如下表,ec2的outbound和mysql的inbound对接起来,这样ec2就可以顺利访问RDS

security group Inbound Outbound IsPublic
bastion SSH:devIP ALL Yes
ec2 SSH:bastion HTTP(s):ALL
MySql:mysql
no
mysql MySql:ec2
MySql:devIP
ALL yes