posted by 귀염둥이채원 2018. 12. 28. 11:03

도커 컨테이너로 Ubuntu, CentOS 베이스 이미지를 사용할 경우,

기본적인 패키지를 설치가 필요해서 정리한다.


# Ubuntu 기본 패키지 설치

$ apt -y update

$ apt -y install \

net-tools \

vim \

htop \

iftop \

openssh-server \

lrzsz \

tree


# Centos 기본 패키지 설치

$ yum -y update

$ yum -y install \

net-tools \

vim \

htop \

iftop \

openssh-server \

which \

sudo \

tree \

initscripts



posted by 귀염둥이채원 2018. 12. 28. 10:37

pgAdmin, DBeaver는 개발자 및 데이터베이스 관리자를위한 무료 오픈 소스 범용 데이터베이스 도구입니다.


pgAdmin은 Linux, Unix, Mac OS X 및 Windows에서 PostgreSQL 9.2 이상을 관리하는 데 사용할 수 있다.

PgAdmin 다운로드: https://www.pgadmin.org/download/




DBeaver 자바로 작성된 데스크톱 애플리케이션이며 이클립스 플랫폼에 기반을 둔다.

DBeaver는 크로스 플랫폼 도구로서 마이크로소프트 윈도우, 리눅스, macOSX, 솔라리스에서도 지원된다.


DBeaver는 Community Edition과 Enterprise Edition이 존재한다.

MySQL, PostgreSQL, MariaDB, SQLite, Oracle, DB2, SQL Server, Sybase, MS Access, Teradata, Firebird, Derby 등 모든 인기있는 데이터베이스를 지원한다.

참고로 Enterprise Edition에서는 NoSQL도 지원한다고 한다.

DBeaver 다운로드: https://dbeaver.io/download/



Slant 사이트는 무엇인가 기술을 선택해야될 경우,

기술에 대해 비교하고 의견을 공유할수 있는 유용한 사이트인데요.


Slant 커뮤니티에서는 DBeaver, pgAdmin4를 비교할 때 대부분의 사람들이 DBeaver를 권장하네요^^

참고: https://www.slant.co/versus/198/208/~dbeaver_vs_pgadmin-4


'DB > postgres' 카테고리의 다른 글

PostgreSQL 백업 및 복원 하는 방법  (0) 2018.12.27
[PostgreSQL] psql 명령어 사용법 정리  (0) 2018.12.27
CentOS에 postgres 설치하기  (0) 2018.12.27
posted by 귀염둥이채원 2018. 12. 27. 17:12

PostgreSQL에서 SQL 덤프를 통한 백업으로 pg_dump 명령어를 제공합니다.

pg_dump 명령어를 통해 백업을 하면 텍스트 파일로 생성됩니다.


# =================================

# 샘플 테이블 스키마

# =================================

CREATE TABLE test_table (

  id SERIAL PRIMARY KEY,

  title TEXT

);


postgres=# INSERT INTO test_table values(1, 'test');


# =================================

# 특정 DB만 백업 및 복원하는 방법

# =================================

# 백업 : pg_dump -U {소유주} {DB명} > {백업파일명}

$ pg_dump -U postgres test_db > db_backup.sql


# test_db2 데이터베이스 생성

# CREATE DATABASE test_db2 OWNER test;


# 복원 : psql -U {소유주} {DB명} < {백업파일명}

$ psql -U postgres test_db2 < db_backup.sql


# =================================

# 특정 테이블만 백업 및 복원 하는 방법

# =================================

# 백업 : pg_dump -U {소유주} {DB명} -t {테이블명} > {백업파일명}

$ pg_dump -U postgres test_db -t test_table > db_backup.sql


# 복원 : psql -U {소유주}  -f {백업파일명} {DB명}

$ psql -U postgres -f db_backup.sql test_db2 

'DB > postgres' 카테고리의 다른 글

pgAdmin, DBeaver 무엇을 사용할까?  (0) 2018.12.28
[PostgreSQL] psql 명령어 사용법 정리  (0) 2018.12.27
CentOS에 postgres 설치하기  (0) 2018.12.27
posted by 귀염둥이채원 2018. 12. 27. 16:49

psql 명령어 사용법을 정리해 보겠습니다.


# ================================================

# psql 역슬래시 명령어

# ================================================

# psql 실행

=# help      // help 조회

=# \q       // psql 종료 (Ctrl+d)

=# \h       // DDL, DML 조회

=# \?       // help


 명령

설명 

 \l

 database 조회

 \d

 테이블, 인덱스, 시퀀스, 뷰 목록

 \d {table_name}

 특정 테이블 정보 보기

 \du

 롤(사용자) 목록

 \dn

 db schema 리스트 조회

 \dS

 시스템테이블 목록

 \dv

 뷰 목록

 \ds

 시퀀스 목록

 \dl 

 DB 목록

 \dn

 스키마 목록

 \db 

 테이블스페이스 목록 

 \c {db_name}

 다른 DB에 접속

 \c {db_name} {user_name}

 다른 DB에 지정된 사용자로 접속

 \x

 vertical print

 \!

 \!만 입력하면 Shell로 빠짐 (exit로 다시 원복)


# ================================================

# 계정 및 DB 생성

# ================================================

계정(test), 비밀번호(test), DB(test_db) 생성하기

postgres-# CREATE ROLE test LOGIN NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;

postgres=# ALTER ROLE test WITH PASSWORD 'test';

postgres=# CREATE DATABASE "test_db" WITH OWNER = test ENCODING = 'UTF8' template = template0;


# ================================================

# 샘플 테이블 스키마

# ================================================

CREATE TABLE test_table (

  id SERIAL PRIMARY KEY,

  title TEXT

);


postgres=# INSERT INTO test_table values(1, 'test');

INSERT 0 1


postgres=# \d

                List of relations

 Schema |       Name        |   Type   |  Owner

--------+-------------------+----------+----------

 public | test_table        | table    | postgres

 public | test_table_id_seq | sequence | postgres

(2 rows)


postgres=# select * from test_table;

 id | title

----+-------

  1 | test


# ================================================

# psql 유용한 명령어

# ================================================

# 버전 표시

=# SELECT VERSION();


# postgres DB에 접속

=# psql -U username -d database


# 지정한 비밀번호로 접속 

ex) PGPASSWORD={PASSWORD} psql -U {USER}

=# PGPASSWORD=postgres psql -U postgres


# psql에서 query 실행

=# psql -U username -d database -c 'select * from test'


# psql에서 .sql 파일에 저장된 쿼리 실행

=# psql -U username -d database -a -f test.sql

=# psql -U username -d database < test.sql


# 원격 서버의 postgres 접속

=# psql -h 10.10.10.10 -U username -d database


# 참고 사이트

http://kb.globalsoft.co.kr/web/web_view.php?notice_no=245

http://blog.naver.com/PostView.nhn?blogId=alice_k106&logNo=220847310053

'DB > postgres' 카테고리의 다른 글

pgAdmin, DBeaver 무엇을 사용할까?  (0) 2018.12.28
PostgreSQL 백업 및 복원 하는 방법  (0) 2018.12.27
CentOS에 postgres 설치하기  (0) 2018.12.27
posted by 귀염둥이채원 2018. 12. 27. 15:53

도커 컨테이너 CentOS에서 postgres 10.6을 설치하는 방법입니다.


# ==========================================

# 컨테이너 기동

# ==========================================

-p 옵션을 사용하여 호스트와 컨테이너의 포트를 연결해준다.

(SQL 클라이언트이자 데이터베이스 관리 도구인 pgadmin, dbeaver를 통해 접근이 가능하다.)


$ docker run -dit \

--name centos7.4_postgres \

--privileged \

-e container=docker \

-v /sys/fs/cgroup:/sys/fs/cgroup:ro \

-p 5432:5432 \

centos:latest \

/usr/sbin/init


# ==========================================

# Postgres 설치

# ========================================== 

# CentOS 배포판 확인

아래 명령으로 postgresql 버전을 확인한다.

$ yum list | grep ^postgresql


# postgresql yum 저장소 업데이트

http://yum.postgresql.org/에 접속해서 버전 확인

https://download.postgresql.org/pub/repos/yum/에 접속해서 원하는 버전의 주소를 찾는다.

$ yum update

$ rpm -Uvh https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7.4-x86_64/pgdg-centos10-10-2.noarch.rpm


# postgresql 설치

$ yum install -y postgresql10.x86_64 postgresql10-server.x86_64


# 설치 패키지 확인

아래 명령을 실행해서 설치되었는지 확인한다.

$ rpm -qa | grep post


# 계정 확인

아래 명령을 통해 postgres 계정이 생성되었는지 확인한다.

$ cat /etc/passwd


# ==========================================

# 초기화, 기동, 접속, 종료

# ==========================================

# 초기화

아래 며령을 수행해서 postgresql을 초기화한다.

$ /usr/pgsql-10/bin/postgresql-10-setup initdb


# postgresql 계정 패스워드 설정

$ passwd postgres


# postgresql 기동

$ su - postgres

$ /usr/pgsql-10/bin/pg_ctl start


# postgresql 접속

$ -bash-4.2$ psql          ---> password 필요 없음

psql (10.6)

Type "help" for help.

postgres=# select version();

 PostgreSQL 10.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-28), 64-bit

(1 row)


# postgresql 종료

$ su - postgres

$ /usr/pgsql-10/bin/pg_ctl stop


# ==========================================

# 관리자 비밀번호 설정

# ==========================================

postgres=# \password postgres

Enter new password:

Enter it again:


# ==========================================

# 로컬 서버에서 암호를 이용한 접속으로 변경

# ==========================================

$ vi /var/lib/pgsql/10/data/pg_hba.conf

#local   all             all                                     peer  --> 암호 X

local   all             all                                     md5    --> 암호 O


# ==========================================

# Remote에서 접근 가능하도록 설정

# ==========================================

$ vi /var/lib/pgsql/10/data/pg_hba.conf

# IPv4 local connections:

host    all             all             0.0.0.0/0            trust        <-- 추가 (암호 X)

host    all             all             0.0.0.0/0            md5        <-- 추가 (암호 O)


$ vi /var/lib/pgsql/10/data/postgresql.conf

#listen_addresses = 'localhost'         # what IP address(es) to listen on;

listen_addresses = '*'          # what IP address(es) to listen on;

'DB > postgres' 카테고리의 다른 글

pgAdmin, DBeaver 무엇을 사용할까?  (0) 2018.12.28
PostgreSQL 백업 및 복원 하는 방법  (0) 2018.12.27
[PostgreSQL] psql 명령어 사용법 정리  (0) 2018.12.27
posted by 귀염둥이채원 2018. 12. 27. 14:54

컨테이너에 postgresql을 설치하던 중 아래와 같은 에러가 발생했다.

$ /usr/pgsql-10/bin/postgresql-10-setup initdb

Failed to get D-Bus connection: Operation not permitted

failed to find PGDATA setting in postgresql-10.service


문제는 권한이 없는 (non-privileged) 컨테이너를 실행중이라고 한다.

systemd 의 경우 CAP_SYS_ADMIN capability가 필요하다.

도커는 보안을 위해 권한이 없는 컨테이너의 경우 capability 를 활성화 시키지 않는다.

또한 systemd는 컨테이너의 cgroup 파일 시스템에 RO(Read Only) 접근을 필요로 한다.

해결을 위해서는 "–v /sys/fs/cgroup:/sys/fs/cgroup:ro" 구문을 추가해야 한다.


# 문제

$ docker run -d --name centos7.4_postgres centos:7.4.1708 /bin/bash -c "while true; do echo hello world; sleep 1; done"


# 해결

$ docker run -d --name centos7.4_postgres --privileged -it -e container=docker -v /sys/fs/cgroup:/sys/fs/cgroup:ro centos:latest /usr/sbin/init


# 참고 사이트

https://serverfault.com/questions/824975/failed-to-get-d-bus-connection-operation-not-permitted

http://webkebi.zany.kr:9003/board/bView.asp?bCode=13&aCode=14170

posted by 귀염둥이채원 2018. 12. 27. 11:40

Base Image가 CentOs인 컨테이너를 데몬 형태로 기동하는 방법을 정리한다.

Image 이름에 CentOs 버전을 입력하면 원하는 도커 이미지를 받아서 컨테이너를 생성할 수 있다.


# CentOS Docker Image tag 목록

latest, centos7, 7 (docker/Dockerfile)

centos6, 6 (docker/Dockerfile)

centos7.6.1810, 7.6.1810 (docker/Dockerfile)

centos7.5.1804, 7.5.1804 (docker/Dockerfile)

centos7.4.1708, 7.4.1708 (docker/Dockerfile)  --> centos7.4.1708 

centos7.3.1611, 7.3.1611 (docker/Dockerfile)

centos7.2.1511, 7.2.1511 (docker/Dockerfile)

centos7.1.1503, 7.1.1503 (docker/Dockerfile)

centos7.0.1406, 7.0.1406 (docker/Dockerfile)

centos6.10, 6.10 (docker/Dockerfile)

centos6.9, 6.9 (docker/Dockerfile)

centos6.8, 6.8 (docker/Dockerfile)

centos6.7, 6.7 (docker/Dockerfile)

centos6.6, 6.6 (docker/Dockerfile)


# Docker command example

docker run -d --name {Container Name} centos:{Version} /bin/bash -c "while true; do echo hello world; sleep 1; done"


# CentOs6.6 컨테이너 생성

$ docker run -d --name c6.6 centos:6.6 /bin/bash -c "while true; do echo hello world; sleep 1; done"

5956c5d1e710c7bfa42311f2900419207a4eb368c733b5ba5ae736bc536edd6b


# 컨테이너 접속

$ docker exec -it c6.6 /bin/bash


# OS 버전 확인

[root@5956c5d1e710 /]# cat /etc/*rel*

CentOS release 6.6 (Final)

cpe:/o:centos:linux:6:GA


# CentOs7.4 컨테이너 생성

$ docker run -d --name c7.4 centos:7.4.1708 /bin/bash -c "while true; do echo hello world; sleep 1; done"

Unable to find image 'centos:7.4.1708' locally

7.4.1708: Pulling from library/centos

18b8eb7e7f01: Pull complete

Digest: sha256:814ef512b757f3b515e81f60bed29909fd71bc2d3b28ec8cf025eac749712ed9

Status: Downloaded newer image for centos:7.4.1708

b61d9645aafa60560ba1abd6066079082f3fa02fbc0a39179ca0266cf3e75d51


# 컨테이너 접속

$ docker exec -it c7.4 /bin/bash


# OS 버전 확인

[root@b61d9645aafa /]# cat /etc/*rel*

CentOS Linux release 7.4.1708 (Core)

Derived from Red Hat Enterprise Linux 7.4 (Source)

NAME="CentOS Linux"

VERSION="7 (Core)"

ID="centos"

ID_LIKE="rhel fedora"

VERSION_ID="7"

PRETTY_NAME="CentOS Linux 7 (Core)"

ANSI_COLOR="0;31"

CPE_NAME="cpe:/o:centos:centos:7"

HOME_URL="https://www.centos.org/"

BUG_REPORT_URL="https://bugs.centos.org/"


CENTOS_MANTISBT_PROJECT="CentOS-7"

CENTOS_MANTISBT_PROJECT_VERSION="7"

REDHAT_SUPPORT_PRODUCT="centos"

REDHAT_SUPPORT_PRODUCT_VERSION="7"


cat: /etc/prelink.conf.d: Is a directory

CentOS Linux release 7.4.1708 (Core)

CentOS Linux release 7.4.1708 (Core)


# 참고 사이트

https://hub.docker.com/_/centos/

posted by 귀염둥이채원 2018. 12. 27. 11:39

Docker 컨테이너는 단지 명령만 실행하고 그 결과만 보여주는 기능을 수행한다.

즉, 아래와 같은 명령은 "Hello World"를 출력한 후 즉시 종료된다.

$ docker run ubuntu /bin/echo 'Hello world'


만약 컨테이너를 데몬 형태로 실행하기 위해서는 계속 실행될 수 있는 명령이 필요하다.

$ docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"


아래와 같이 실행해도 컨테이너를 데몬 형태로 실행할 수 있다.

$ docker run -d -it --name my_ubuntu ubuntu

* -d 옵션: Docker의 컨테이너를 백그라운드 프로세스로 실행하는 옵션

* -it 옵션: interactive 모드


※ 참고 사이트 ※

https://www.popit.kr/%EA%B0%9C%EB%B0%9C%EC%9E%90%EA%B0%80-%EC%B2%98%EC%9D%8C-docker-%EC%A0%91%ED%95%A0%EB%95%8C-%EC%98%A4%EB%8A%94-%EB%A9%98%EB%B6%95-%EB%AA%87%EA%B0%80%EC%A7%80/

https://blog.pavelsklenar.com/10-useful-docker-commands-tip-tricks/

posted by 귀염둥이채원 2018. 12. 27. 10:52

docker 명령을 반복해서 타이핑하려면 귀찮다.

bashrc 파일에  아래 내용을 복사해서 사용하면 편하다.


# Print List container

alias dps='docker ps --format "table {{.ID}} \t{{.Image}} \t{{.Command}} \t{{.Names}} \t{{.Status}} \t{{.Ports}}"'


# Stop all containers

dstop() { docker stop $(docker ps -a -q); }


# Remove all containers

drm() { docker rm $(docker ps -a -q); }


# Stop and Remove all containers

alias drmf='docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q)'


# Remove all images

dri() { docker rmi $(docker images -q); }


# Show all alias related docker

dalias() { alias | grep 'docker' | sed "s/^\([^=]*\)=\(.*\)/\1 => \2/"| sed "s/['|\']//g" | sort; }


# Bash into running container

dbash() { docker exec -it $(docker ps -aqf "name=$1") bash; }


참고 사이트

- https://github.com/tcnksm/docker-alias/blob/master/zshrc

- https://windsock.io/customising-docker-cli-output/

posted by 귀염둥이채원 2018. 11. 21. 01:25

# 특정 문자열을 찾아 변경하는 방법

find -name "*.conf" | xargs sed s/찾을단어/원하는단어/g -i

위의 명령어는 *.conf 파일을 찾아서 내용중에 찾을단어를 원하는 단어로 변경.


vi 에서는

:%s/기존문자열/새문자열

 

# find 명령어 사용법 예시


파일명 찾기

find ./ -name 찾을파일명


# 파일의 특정 소유자 찾기. ex) nobody

find ./ -user 찾을파일명


10kb~100kb 인 파일찾기

find ./ -size +10k -size 100k


확장자가 conf 인 파일 지우기

find ./ -name "*.conf" -exec rm {} \;


퍼미션 777 파일 찾기

find ./ -type f -perm 0777


하위디렉터리를 제외한 현재 디렉토리에서 tar.gz 파일 

find ./ -maxdepth 1 -name "*.tar.gz"                                                               


찾아서 크기가 큰순으로 정렬

find ./ -maxdepth 2 -name "*.tar.gz" | xargs ls -l -SSr -h