唯一的真理

True or False

Redis Sentinel HA自动切换

Redis Sentinel HA自动切换

redis 2.8.11 下载

http://redis.io/download
http://download.redis.io/releases/redis-2.8.11.tar.gz

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
master 192.168.0.210
slave 192.168.0.211
slave 192.168.0.213

#安装redis 2.8.11
cd /srv/
tar xf redis-2.8.11.tar.gz
cd redis-2.8.11
make && make install 

#复制启动文件
cp -rp /srv/redis-2.8.11/utils/redis_init_script  /etc/init.d/redis
chmod +x /etc/init.d/redis


#master 配置文件
/etc/redis/redis.conf
####
daemonize yes
pidfile /var/run/redis/redis.pid
port 6379
bind 0.0.0.0
timeout 0
loglevel notice
logfile /var/log/redis/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /var/lib/redis/
slave-serve-stale-data yes
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
slowlog-log-slower-than 10000
slowlog-max-len 1024
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
requirepass angel01
slave-read-only yes
maxmemory 1G
########


#slave配置
########
daemonize yes
pidfile /var/run/redis/redis.pid
port 6379
bind 0.0.0.0
timeout 0
loglevel notice
logfile /var/log/redis/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /var/lib/redis/
slave-serve-stale-data yes
appendonly yes
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
slowlog-log-slower-than 10000
slowlog-max-len 1024
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
slave-serve-stale-data yes
slaveof 192.168.0.210 6379
masterauth angel01
slave-read-only yes
maxmemory 1G
#############

Python OptionParser

脚本原型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/python
#-*- coding:utf-8 -*
#django添加数据库用户密码
import sys
import os
from optparse import OptionParser

os.environ.setdefault( "DJANGO_SETTINGS_MODULE", "mysite.settings" )
from accounts.models import User
import hashlib



###编码转移,可以输出中文####
reload(sys)
sys.setdefaultencoding('utf-8')


###%prog表示脚本本身
MSG_USAGE = "%prog [ -u <username>][-p <password>][-t <user_type>][-h <help>]"
MSG_VERSION = "%prog 20140529"

###usage打印用法,version输出版本信息
optParser = OptionParser(usage=MSG_USAGE,version=MSG_VERSION)

###对optParser增加选项
optParser.add_option("-u","--username",action = "store",type = "string",dest = "UserName",help="添加数据库用户名(必填)")
optParser.add_option("-p","--password",action = "store",type = "string",dest = "Password",help="添加数据库密码(必填)")
optParser.add_option("-t","--user_type",action = "store",type = "string",dest = "User_Type",default="1",help="添加用户权限,0为管理员,1为普通,默认为1")

#fakeArgs = ['-u','leon','-p','123456','-t','a']
options,args = optParser.parse_args()

TYPE = options.User_Type
USERNAME = options.UserName
PASSWORD = options.Password

#User_Type返回不是1或者0则退出
if TYPE not in ('1','0'):
    optParser.print_help()
    sys.exit(1)

#判断参数-u,-p是否为空,返回帮助
args_list = [USERNAME,PASSWORD]
for i in args_list:
    if i is None:
        print '请使用(-h,--help)来获得帮助'
        sys.exit(0)


#User是在accounts中models.py的类,定义了数据库的一些信息,插入用户
#判断用户是否存在
def init():
    CHECK_NAME=User.objects.filter(name=USERNAME)
    if len(CHECK_NAME) > 0:
        print "%s 用户已经存在" % USERNAME
    else:
        u = User(name=USERNAME, password=hashlib.md5(PASSWORD).hexdigest(), user_type=TYPE)
        u.save()

if '__main__' == __name__:
    init()

Django 基础

django重点

django-admin.py startproject mysite 命令在当前目录创建一个mysite目录,这个作为一个项目目录

1
2
3
4
5
6
7
8
9
10
11
mysite/
├── __init__.py         #django 初始化模块
├── __init__.pyc
├── settings.py         #django 主要配置,全局环境变量
├── settings.pyc
├── urls.py             #django 主要配置,url跳转
├── urls.pyc
├── views.py            #django 主要配置,视图功能
├── views.pyc
├── wsgi.py             #django 默认加载的模块
└── wsgi.pyc

django请求流程,用户请求—>django-urls.py—>django-views.py

Awk and Sed

AWK

通过-F分割段落并将输出的内容,以一行2个组成2列

1
2
3
4
5
6
7
8
9
10
11
#文本内容
cat test
"111" : "33333"
"222" : "44444"
"333" : "55555"
"444" : "66666"

#结果
awk -F'[":]' 'NR%2{printf $5" ";next}{print $5}' test
33333 44444
55555 66666
返回顶部