-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnote
120 lines (102 loc) · 4.95 KB
/
note
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
1.sql to find deadlock v1
SELECT
a.application_name,
l.relation::regclass,
l.transactionid,
l.mode,
l.locktype,
l.GRANTED,
a.usename,
a.query,
a.pid
FROM
pg_stat_activity a
JOIN pg_locks l ON
l.pid = a.pid
WHERE
a.application_name = 'psql'
ORDER BY
a.pid;
2.sql to find deadlock v2
SELECT blocked_locks.pid AS blocked_pid,
blocked_activity.usename AS blocked_user,
blocking_locks.pid AS blocking_pid,
blocking_activity.usename AS blocking_user,
blocked_activity.query AS blocked_statement,
blocking_activity.query AS current_statement_in_blocking_process
FROM pg_catalog.pg_locks blocked_locks
JOIN pg_catalog.pg_stat_activity blocked_activity ON blocked_activity.pid = blocked_locks.pid
JOIN pg_catalog.pg_locks blocking_locks
ON blocking_locks.locktype = blocked_locks.locktype
AND blocking_locks.database IS NOT DISTINCT FROM blocked_locks.database
AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation
AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page
AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple
AND blocking_locks.virtualxid IS NOT DISTINCT FROM blocked_locks.virtualxid
AND blocking_locks.transactionid IS NOT DISTINCT FROM blocked_locks.transactionid
AND blocking_locks.classid IS NOT DISTINCT FROM blocked_locks.classid
AND blocking_locks.objid IS NOT DISTINCT FROM blocked_locks.objid
AND blocking_locks.objsubid IS NOT DISTINCT FROM blocked_locks.objsubid
AND blocking_locks.pid != blocked_locks.pid
JOIN pg_catalog.pg_stat_activity blocking_activity ON blocking_activity.pid = blocking_locks.pid
WHERE NOT blocked_locks.granted;
3. Overide config with env
e.q. SERVER_ADDRESS=0.0.0.0:8081 make server -> run server on port 8081
4. Generate mock for db.Store interface
$ mockgen -destination db/mock/store.go github.com/edwillam1007/simplebank/db/sqlc Store
5. Create migration schema
$ migrate create -ext sql -dir db/migration -seq add_users
6. Build docker file
$ docker build -t simplebank:latest .
7. Run container
$ docker run --name simplebank -p 8080:8080 simplebank:latest
8. Run container with GIN in default mode
$ docker run --name simplebank -p 8080:8080 -e GIN_MODE=release simplebank:latest
9. Run container with assing postgres address
$ docker run --name simplebank -p 8080:8080 -e GIN_MODE=release -e DB_SOURCE="postgresql://root:[email protected]:5432/simple_bank?sslmode=disable" simplebank:latest
10. Create our own network
$ docker network create bank-network
10.1 link container to our network
$ docker network connect bank-network postgres12
10.2 using container name to connect postgres
$ docker run --name simplebank --network bank-network -p 8080:8080 -e GIN_MODE=release -e DB_SOURCE="postgresql://root:secret@postgres12:5432/simple_bank?sslmode=disable" simplebank:latest
11. start docker compose
$ docker-compose up
12. stop docker compose
$ docker-compose down
13. AWS
bbLW1qsCJH9n6AJyFr2I
simple-bank.c423peqak5tm.ap-northeast-2.rds.amazonaws.com
14. Create random key
$ openssl rand -hex 64 | head -c 32
15. aws cli
15.1 install
https://docs.aws.amazon.com/zh_tw/cli/latest/userguide/install-cliv2-linux.html
15.2 get secrets
$ aws secretsmanager get-secret-value --secret-id simple_bank --query SecretString
15.3 convert by jq
$ aws secretsmanager get-secret-value --secret-id simple_bank --query SecretString --output text | jq -r 'to_entries|map("\(.key)=\(.value)")|.[]' > app.env
16. pull ECR image
16.1 login
aws ecr get-login-password | docker login --username AWS --password-stdin <ECR URI> (without repository name)
e.q. $ aws ecr get-login-password | docker login --username AWS --password-stdin 087555703757.dkr.ecr.ap-northeast-2.amazonaws.com
17. **** How to run
17.1 Use the docker image on ECR and connect to AWS Postgres rds
17.1.1 login AWS ECR
aws ecr get-login-password | docker login --username AWS --password-stdin <ECR URI> (without repository name)
e.q. $ aws ecr get-login-password | docker login --username AWS --password-stdin 087555703757.dkr.ecr.ap-northeast-2.amazonaws.com
17.1.2 pull ECR latest docker image
docker pull <image URI>
e.q. $docker pull 087555703757.dkr.ecr.ap-northeast-2.amazonaws.com/simplebank:9673de31132ed1ee6375740af289d318ffa63f2c
17.1.3 Run docker container
docker run -p 8080:8080 <Image ID>
e.q. docker run -p 8080:8080 629fea85b145
17.1.4 User Postman to connect to simple_bank server
PS: AWS settings refer to Part 26 to Part 29
17.2 Use docker compose to run db and server containers
17.2.1 Mark "source /app/app.env" in start.sch
17.2.2 run all containers
$ docker-compose up
17.2.3 User Postman to connect to simple_bank server
17.2.4 stop all containers
$ docker-compose up