I’m not an anti-GUI person. In fact, I wrote three books about web GUI development with Java. However, I also like the command-line interface (CLI), especially text-based UIs. After a year of exploring MariaDB and the DevOps world, I got to discover and play with many text-based CLI tools that I didn’t know even existed. These tools are especially useful when connecting to remote servers that don’t have a GUI.
제가 자주 사용하는 특별한 CLI 도구 중 하나는 mariadb
SQL 클라이언트 (또는 MySQL 세계에서 mysql
)입니다. 이는 MariaDB 호환 데이터베이스에 연결하는 데 사용되는 CLI 프로그램입니다. 이를 통해 데이터베이스 서버로 SQL 쿼리 및 기타 명령을 보낼 수 있습니다.
MariaDB CLI 기반 SQL 클라이언트
mariadb
SQL 클라이언트에는 여러 구성 옵션이 있는데, 그중 하나는 터미널 페이저를 설정할 수 있는 것입니다. Linux에 익숙한 경우 more
및 less
페이저를 들어보거나 사용한 적이 있을 것입니다. 환경 변수 PAGER
를 통해 페이저를 설정하거나 mariadb
가 자동으로 사용하도록 할 수 있습니다. 또는 mariadb
프롬프트를 사용하여 현재 세션에만 페이저를 설정할 수 있습니다. 예를 들어 데이터베이스에 연결된 후 less
페이저를 사용하려면 다음 명령을 실행하십시오.
pager less
다음에 SQL 쿼리를 실행할 때 키보드의 화살표 키를 사용하여 결과 집합을 탐색할 수 있습니다.
mariadb
SQL 클라이언트를 사용하여 페이저 설정
less
페이저는 유용하지만 테이블로 표시되는 SQL 결과 세트에 가장 적합한 것은 아닙니다. pspg
라는 오픈 소스 도구가 있습니다(문서와 소스 코드는 GitHub에서 확인하세요). 처음에는 PostgreSQL을 위해 개발되었지만 나중에 MariaDB를 포함한 여러 다른 데이터베이스를 지원하도록 확장되었습니다. mariadb
SQL 클라이언트가 MariaDB Xpand 데이터베이스에 연결할 수 있기 때문에 시도해 보았고, 완벽하게 작동했습니다. 어떻게 시도해 볼 수 있는지 계속 읽어보세요.
Xpand 데이터베이스를 실행하는 가장 쉬운 방법은 SkySQL에서 서비스를 생성하는 것입니다(무료임). 그러나 Docker를 사용하여 로컬 인스턴스를 실행할 수도 있습니다. 필요한 스니펫은 다음과 같습니다:
docker run --name xpand \
-d \
-p 3306:3306 \
--ulimit memlock=-1 \
mariadb/xpand-single
데이터가 있을 때 데이터베이스는 더 재미있습니다. 간단하지만 흥미로운 데모 데이터베이스는 이 웹사이트에서 사용할 수 있습니다. 리눅스 계열 운영 체제에서는 다음 명령어를 실행하세요(Xpand 데이터베이스가 다른 곳에서 실행 중이라면 마지막 명령에서 IP 주소를 변경하세요):
sudo apt install curl -y
curl https://www.mariadbtutorial.com/wp-content/uploads/2019/10/nation.zip --output nation.zip
unzip nation.zip
mariadb -h 127.0.0.1 -u xpand < nation.sql
rm nation.zip nation.sql
다음을 설치해야 합니다.pspg
:
apt install pspg -y
mariadb
SQL 클라이언트를 사용하여 “Xpand”을 표시하는 사용자 정의 더 멋진 프롬프트로 데이터베이스에 연결하십시오.
mariadb -h 127.0.0.1 -u xpand --prompt="Xpand [\d]> " nation
I learned this tip from my colleague Patrick Bossman (Product Manager at MariaDB) during a webinar on MariaDB Xpand + Docker. I recommend watching it if you want to learn more.
MariaDB Xpand에 사용자 지정 프롬프트를 사용하여 연결하기
현재 세션에 대한 pspg
페이저 설정:
pager pspg -s 14 -X --force-uniborder --quit-if-one-screen
A nice feature in pspg
is that it shows the fancy text-based UI only when it makes sense (--quit-if-one-screen
). So if your query returns only a few rows that fit in the screen, it will just show them right there on the screen as usual. For example, try running the following query:
select * from continents;
여기서 볼 건 별로 없습니다.
몇 개의 행만 표시되는 경우 pspg
페이저가 활성화되지 않습니다
다음을 시도해보세요:
select * from countries;
A navigable text-based interface allows you to explore the data more efficiently.
MariaDB Xpand에서 데이터를 렌더링하는 pspg
페이저
행을 검색하고, 정렬하고, CSV로 내보내고, 컬럼을 고정하고, 행을 표시하고, 도구와 상호작용하기 위해 마우스를 사용할 수 있등 다양한 작업을 수행할 수 있습니다.
pspg
의 일부 메뉴 옵션
I hope this tool helps you the next time you have to interact with a database via SSH and the command line. You can find more information about how to install pspg
on your operating system, configuration options, and documentation on the GitHub repository for the project. If you want to learn more about distributed SQL and the MariaDB Xpand database, watch this short video, take a look at this datasheet, and explore some of the blog posts and documentation.
Source:
https://dzone.com/articles/using-the-postgressql-pager-with-mariadb-xpand