清雨影的Blog
搜索文档…
备忘录

批量杀线程

1
#!/bin/bash
2
ps -ef|grep $1|grep -v grep|awk '{print $2}'|xargs -r kill -9
Copied!

MySQL

创建用户并且授权读写Schema

1
create user ${USERNAME} IDENTIFIED by '${PASSWORD}';
2
show grants for ${USERNAME};
3
grant all on ${SCHEMA}.* to ${USERNAME};
4
flush privileges;
Copied!

MongoDB 权限配置

管理员权限配置

1
db.createUser({user:"root",pwd:"123456",roles: [{ role:"root", db:"admin"}]})
Copied!

用户配置样例

1
db.createUser({
2
user: "someuser",
3
pwd: "somepassword",
4
roles:[
5
{ role: "readWrite", db: "somedb" }
6
]
7
})
Copied!

删除用户

1
db.dropUser("username")
Copied!

MongoDB 慢查询相关

查找当前大于10.0秒的、且最慢的一个查询的详情

1
mongo --quiet --eval "let limit_time = 10.0;" slow_query.js
Copied!

统计大于10秒的慢查数量

1
mongo --quiet --eval "db.currentOp().inprog.filter(x=>x['secs_running']>10).length"
Copied!
一般统计大于5秒,15秒,30秒,60秒,300秒的慢查数量。

查询Running总时间

这个指标可以从侧面反应数据库的操作体验和压力。
1
mongo --quiet --eval "db.currentOp().inprog.map(x=>x.microsecs_running/1000000.0).reduce((a,b)=>a+b)"
Copied!
1
// 检查大于 limit_time 秒的慢查询数据
2
// let limit_time = 10.0;
3
// 上一句在eval里面执行来达到动态设置的效果
4
db.currentOp().inprog.filter(
5
x=>x["microsecs_running"]>(limit_time*1000000)
6
).sort(
7
// 取最慢的一条进行展示
8
(a,b)=>{
9
const va = a["microsecs_running"];
10
const vb = b["microsecs_running"];
11
if(a==b){
12
return 0;
13
}else{
14
if(a>b){
15
return -1;
16
}else{
17
return 1;
18
}
19
}
20
}
21
)
Copied!

杀死超时任务

1
let over_time = 60.0;// Over 1 min
2
db.currentOp().inprog.forEach(
3
opInfo=>{
4
try{
5
const opId = opInfo["opid"];
6
const opDuration = opInfo["secs_running"];
7
if(opDuration>over_time){
8
db.killOp(opId);
9
console.log(`Operation ${opId} killed, details: ${JSON.stringify(opInfo)}`)
10
}
11
}catch(err){
12
console.error("Error while executing killing filter.")
13
console.error(err)
14
}
15
}
16
)
Copied!

Python Tricks

Lazy Evaluation

1
class lazy(object):
2
def __init__(self, func):
3
self.func = func
4
5
def __get__(self, instance, cls):
6
val = self.func(instance)
7
setattr(instance, self.func.__name__, val)
8
return val
9
10
def lazy_property(func):
11
attr_name = "_lazy_" + func.__name__
12
13
@property
14
def _lazy_property(self):
15
if not hasattr(self, attr_name):
16
setattr(self, attr_name, func(self))
17
return getattr(self, attr_name)
18
19
return _lazy_property
Copied!
Comparing with the lazy, it more like cache the result, maybe can also simply replaced by lru_cache. We have to ensure the return value won't change while using it.

Example of Async wait

1
import asyncio
2
from asyncio import Future
3
from typing import Dict
4
5
futures: Dict[str, Future] = {}
6
7
8
@app.get("/test/lock/{task_id}")
9
async def test_lock(task_id: str):
10
result = asyncio.get_event_loop().create_future()
11
# Put the task in Queue
12
futures[task_id] = result
13
return {
14
"result": await result
15
}
16
17
18
@app.get("/test/release/{task_id}")
19
async def test_release(task_id: str):
20
if task_id not in futures:
21
raise Exception("Task not found")
22
# Set result while the task finished
23
futures[task_id].set_result(f"{task_id} is done")
24
return {
25
"task": task_id
26
}
Copied!

FFMPEG

Convert MP4 File to MP3 File

1
ffmpeg -i ${MP4_FILE} -vn \
2
-acodec libmp3lame -ac 2 -ab 320k -ar 48000 \
3
${MP3_FILE}
Copied!
Replace -ab {bitrate} to -ac 2 -qscale:a 4 to use VBR

Convert to H264 mp4 file

1
ffmpeg -vcodec h264 -acodec copy -movflags +faststart -pix_fmt yuv420p -crf 23 -i input.mp4 output.mp4
Copied!

树莓派

RPI3 Ubuntu 摄像头

  1. 1.
    只能使用32位的系统
  2. 2.
    /boot/firmware/config.txt里增加start_x=1
  3. 3.
    执行 sudo snap install picamera-streaming-demo以后重启系统
  4. 4.
    打开 http://:8000/ 即可查看
参考资料:

RPI3 Wifi

  1. 1.
    Edit /etc/netplan/xxxxxx.yaml:
1
# This file is generated from information provided by
2
# the datasource. Changes to it will not persist across an instance.
3
# To disable cloud-init's network configuration capabilities, write a file
4
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
5
# network: {config: disabled}
6
network:
7
version: 2
8
ethernets:
9
eth0:
10
optional: true
11
dhcp4: true
12
# add wifi setup information here ...
13
wifis:
14
wlan0:
15
optional: true
16
access-points:
17
"YOUR-SSID-NAME":
18
password: "YOUR-NETWORK-PASSWORD"
19
dhcp4: true
Copied!
1
sudo netplan --debug try
2
# (continue even if there are errors)
3
sudo netplan --debug generate
4
# (provides more details in case of issues with the previous command)
5
sudo netplan --debug apply
6
# (if no issues during the previous commands)
Copied!

Encryption

SAML2 key/pem generator

1
openssl req -x509 -sha256 -nodes -days 3650 -newkey rsa:2048 -keyout encryption.key -out encryption.pem
2
openssl req -x509 -sha256 -nodes -days 3650 -newkey rsa:2048 -keyout signing.key -out signing.pem
Copied!

K8S

创建一个管理员账户的配置文件

首先创建一个Service Account
1
apiVersion: v1
2
kind: ServiceAccount
3
metadata:
4
name: <Account Name>
5
namespace: default
6
---
7
apiVersion: rbac.authorization.k8s.io/v1
8
kind: ClusterRoleBinding
9
metadata:
10
name: admin-user
11
roleRef:
12
apiGroup: rbac.authorization.k8s.io
13
kind: ClusterRole
14
name: cluster-admin
15
subjects:
16
- kind: ServiceAccount
17
name: <Account Name>
18
namespace: default
Copied!
获取账户详情 kubectl describe serviceaccount <Account Name>
随后获取secret的名字,格式一般是 <Account Name>-token-<随机字符串>
最后获取Token:
1
kubectl describe secret <Token Secret Name>
Copied!
1
apiVersion: v1
2
kind: Config
3
users:
4
- name: <Account Name>
5
user:
6
token: "<TOKEN>"
7
clusters:
8
- name: <Cluster Name>
9
cluster:
10
api-version: v1
11
server: <API ENDPOINT>
12
contexts:
13
- name: <Cluster Name>
14
context:
15
cluster: <Cluster Name>
16
user: <Account Name>
17
current-context: <Cluster Name>
Copied!

Java/Scala/Kotlin

Maven Settings

Mainly ~/.m2/settings.xml.
1
<?xml version="1.0" encoding="UTF-8"?>
2
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0"
3
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
5
6
<servers>
7
<server>
8
<id>nexus-snapshots</id>
9
<username>{username}</username>
10
<password>{password}</password>
11
</server>
12
<server>
13
<id>nexus-releases</id>
14
<username>{username}</username>
15
<password>{password}</password>
16
</server>
17
</servers>
18
19
<mirrors>
20
<mirror>
21
<id>central</id>
22
<name>central</name>
23
<url>https://nexus.xxxx.com/repository/maven/</url>
24
<mirrorOf>*</mirrorOf>
25
</mirror>
26
</mirrors>
27
28
</settings>
Copied!