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.
כלי פקודתי מיוחד אחד שאני משתמש בו לעתים קרובות הוא ה-mariadb
של SQL לקוח (או mysql
בעולם ה-MySQL) – תוכנה לביצוע פקודות בשפת SQL המשמשת לחיבור למסדות הנתונים מתאימות ל-MariaDB. באמצעותה, אפשר לשלוח שאילתות SQL ופקודות אחרות לשרת המסד נתונים.
לקוח SQL מבוסס פקודת CLI של MariaDB
ללקוח SQL mariadb
יש מספר אפשרויות תצורה, כולל אפשרות לקבוע דפדפן מסוף. אם אתה מכיר ב-Linux, יש לך כנראה שמעת או השתמשת בדפדפנים more
ו-less
. אפשר לקבוע דפדפן דרך משתנה הסביבה PAGER
ו-mariadb
ישתמש בו אוטומטית. או שאפשר לקבוע דפדפן רק עבור הסשן הנוכחי באמצעות הפקודה mariadb
. לדוגמה, כדי להשתמש בדפדפן less
רוץ את הפקודה הבאה ברגע שאתה מחובר למסד הנתונים:
pager less
בפעם הבאה שתפעיל שאילתת SQL, תוכל לנווט בין קבצי התוצאה באמצעות מקשי החצים במקלדת שלך.
קביעת דפדפן באמצעות לקוח SQL mariadb
הדפדפן less
שימושי אך אינו הטוב ביותר לפלטי SQL שמוצגים כטבלאות. קיים כלי פתוח המקור בשם pspg
(ראה את התיעוד ואת קוד המקור ב-GitHub), שפותח במקור ל-PostgreSQL אך מאוחר יותר הוספה תמיכה במספר מסדי נתונים אחרים, כולל MariaDB. מאחר שללקוח SQL של mariadb
יכול להתחבר למסדי נתוני MariaDB Xpand, ניסיתי את זה וזה עבד בצורה מושלמת. המשך לקרוא כדי לגלות כיצד לנסות את זה.
הדרך הקלה ביותר להפעלת מסד נתוני Xpand היא על ידי יצירת שירות ב-SkySQL (זה חינם). עם זאת, אפשר גם להריץ מפרט מקומי באמצעות Docker. הנה הקטע של הקוד שאתה צריך:
docker run --name xpand \
-d \
-p 3306:3306 \
--ulimit memlock=-1 \
mariadb/xpand-single
מסדי נתונים יותר כיף כשיש בהם נתונים. מסד נתונים דוגמא פשוט אך מעניין זמין באתר זה. במערכות ההפעלה דמויות לינוקס, הפעל את הפקודות הבאות (שנה את כתובת ה-IP בפקודה האחרונה אם מסד הנתונים של Xpand פועל במקום אחר):
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
התחבר למסד הנתונים באמצעות לקוח SQL mariadb
עם פקודה מותאמת אישית ומגניבה יותר שמראה "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.
דפדפן pspg
מציג נתונים מ MariaDB Xpand
אפשר לחפש שורה, לסדר, לייצא ל-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