Posts in category “Linux”

Simplify MySQL Connections with a Smarter Bash Function

Working with multiple MySQL environments is a common scenario for developers. Whether you're connecting to development, staging, or production databases, you often need different connection parameters for each environment. While MySQL's configuration files help manage these connections, switching between them can be cumbersome. Let me introduce a simple but powerful bash function that will streamline this process.

The Problem

If you work with MySQL regularly, you likely have multiple connection profiles defined in your ~/.my.cnf file using the [group] syntax. To use a specific configuration group, you need to use the --defaults-group-suffix option:

mysql --defaults-group-suffix=dev
mysql --defaults-group-suffix=prod

This works, but it's verbose and typo-prone.

The Solution: A Smarter Bash Function

Here's a bash function that makes working with MySQL much more convenient:

m() {
    if [ $# -eq 0 ]; then
        # If no arguments provided, run mysql directly
        mysql
    elif [[ "$1" == -* ]]; then
        # If first argument starts with -, pass all arguments directly to mysql
        mysql "$@"
    else
        # Otherwise, treat first argument as suffix
        local suffix=$1
        shift
        mysql --defaults-group-suffix=$suffix "$@"
    fi
}

How It Works

This function provides three different behaviors depending on how you call it:

  1. No arguments: Simply runs the MySQL client with default settings.

    m
    
  2. First argument starts with a dash: Passes all arguments directly to MySQL, just like you're using the mysql command.

    m -e "SHOW DATABASES"
    
  3. First argument doesn't start with a dash: Uses the first argument as the defaults-group-suffix and passes the remaining arguments to MySQL.

    m dev -e "SELECT * FROM users"
    

Practical Examples

Example 1: Connect to Different Environments

m dev    # Connect to development database
m prod   # Connect to production database
m stage  # Connect to staging database

Example 2: Run Commands Directly

m dev -e "SELECT COUNT(*) FROM orders"
m prod -N -e "SELECT id FROM customers LIMIT 5"  # -N removes column names

Example 3: Use MySQL Options Directly

m -N -e "SELECT VERSION()"  # Quick query without environment prefix
m -t -e "SHOW TABLES"  # Table format output

Setup Instructions

  1. Add the function to your .bashrc or .zshrc file.
  2. Set up your MySQL configuration groups in ~/.my.cnf:
[client]
# Default settings

[clientdev]
user=devuser
password=devpass
host=dev-db.example.com

[clientprod]
user=produser
password=prodpass
host=prod-db.example.com

[mysql]
database=YourDataBase
  1. Source your shell configuration file or restart your terminal.

Benefits

  • Brevity: No need to type connection parameters (-h, -u, -p) repeatedly, as they're stored in your config file.
  • Flexibility: Pass any MySQL options like -N (skip column names) or -e (execute) alongside environment selection.
  • Consistency: Maintain the standard MySQL command syntax for direct option usage.

This simple function has saved me countless keystrokes and made database work significantly smoother. It's especially handy for running quick queries with options like -e (execute) and -N (no headers) without having to type connection details every time. Give it a try in your workflow—small improvements like this add up to a much more efficient development experience!

Do you have any favorite shell functions that make your development life easier? Share them in the comments!

Quick Guide to Installing postmarketOS on Asus C100P

This guide helps you install PostmarketOS on your C100P's internal storage (emmc hard disk).

Step 1: Install pmbootstrap

First, install the latest version of pmbootstrap on Debian 12 via git:

git clone https://gitlab.postmarketos.org/postmarketOS/pmbootstrap.git
cd pmbootstrap
mkdir -p ~/.local/bin
ln -s "$PWD/pmbootstrap.py" ~/.local/bin/pmbootstrap
pmbootstrap --version

Make sure to add ~/.local/bin to your PATH environment variable.

Step 2: Initialize pmbootstrap

Run pmbootstrap --init, and if any dependencies are missing, install them using sudo apt install <package> as instructed. For the vendor, select google instead of asus, and for the codename, choose veyron.

Step 3: Prepare a USB Drive

Prepare a USB drive and plug it into your computer. Use sudo fdisk -l to check the device name of the USB drive (usually /dev/sdb or something else). Be very careful, as choosing the wrong device can lead to data loss. Then, use the following command to begin the installation:

pmbootstrap install --sdcard /dev/sdx

Remember to replace /dev/sdx with the correct device name of your USB drive.

Step 4: If You Encounter Mounting Errors

If you run into errors when trying to mount, try using a different USB drive. I had an issue with a 128GB Sandisk drive, but it worked fine with a 32GB SD card.

Step 5: Boot Into Asus C100P

Boot your Asus C100P using the prepared USB drive, and connect to a Wi-Fi network.

Step 6: Install pmbootstrap on C100P

On the C100P, install pmbootstrap using the same steps from Step 1, then run "sudo apk add openssl".

Step 7: Initialize pmbootstrap

Run pmbootstrap init on the C100P, following the same instructions from Step 2.

Step 8: Install to eMMC

Use the following command to install postmarketOS to the eMMC:

pmbootstrap install --sdcard /dev/mmcblk0 # on my c100p it is mmcblk0, please check yours before press enter!

Step 9: Done! Remove the USB Drive

Once the installation is complete, remove the USB drive. postmarketOS should now be installed on your Asus C100P!

继续折腾:在 Asus C100P 上安装 postmarketOS 简要记录

这篇blog教你如何将 postmarketOS 安装到 C100P 的内置emmc硬盘上。我装了最新的 edge 系统和 Mate桌面,还余 10G空间可用。

步骤 1: 安装 pmbootstrap

首先,在 Debian 12 上通过 git 安装最新版 pmbootstrap:

git clone https://gitlab.postmarketos.org/postmarketOS/pmbootstrap.git
cd pmbootstrap
mkdir -p ~/.local/bin
ln -s "$PWD/pmbootstrap.py" ~/.local/bin/pmbootstrap
pmbootstrap --version

确保把 ~/.local/bin 添加到你的 PATH 环境变量中。

步骤 2: 初始化 pmbootstrap

运行 pmbootstrap --init,如果有缺少的依赖包,按提示使用 sudo apt install <package> 安装。对于厂商,选择 google 而不是 asus,对于 codename,选择 veyron

步骤 3: 准备优盘

准备一个优盘并接入电脑,运行 sudo fdisk -l 来确认优盘的设备名(通常是 /dev/sdb 或其他)。一定要小心,不要选错设备名,否则可能会导致数据丢失。然后使用以下命令开始安装:

pmbootstrap install --sdcard /dev/sdx

注意,将 /dev/sdx 替换为你自己的优盘设备名。

步骤 4: 如果出现挂载错误

如果出现挂载错误,尝试换一块优盘。我遇到的问题是,128GB 的 Sandisk 优盘无法正常工作,但换成了一块 32GB 的 SD 卡后就顺利安装了。

步骤 5: 启动到 Asus C100P 上

用准备好的优盘启动 Asus C100P,连接 Wi-Fi 网络。

步骤 6: 安装 pmbootstrap 到 C100P

在 C100P 上安装 pmbootstrap,按照步骤 1 中的方式安装。

步骤 7: 初始化 pmbootstrap

在 C100P 上运行 pmbootstrap init,按照步骤 2 中的说明完成初始化。

步骤 8: 安装到 eMMC

运行以下命令将 postmarketOS 安装到 eMMC:

pmbootstrap install --sdcard /dev/mmcblk0

步骤 9: 完成并移除优盘

安装完成后,拔掉优盘,你的 Asus C100P 上就安装好了 postmarketOS!

AI Prompt for Git Commit Messages

Analyze the following code changes and generate a concise, meaningful git commit message that:

1. Summarizes the main purpose or impact of the changes
2. Is no longer than 100 characters (150 characters maximum if absolutely necessary)
3. Uses present tense and imperative mood (e.g., "Add feature" not "Added feature")
4. Focuses on the "what" and "why" rather than the "how"

Provide ONLY the commit message itself, without any additional text, explanations, or formatting.

Code changes:

I have used it in my bash script ci and it works very well! ...

How to Avoid SSH's "Are you sure you want to continue connecting?" Prompt

If you're tired of seeing the "Are you sure you want to continue connecting (yes/no/[fingerprint])?" prompt every time you SSH into a new server, you're not alone. This security feature, while important, can be a bit of a nuisance for system administrators and developers who frequently connect to new machines. This is especially true when they wish to run a remote command on a newly trusted machine. Let's explore how to streamline this process without completely compromising security.

Understanding the Prompt

First, it's important to understand why this prompt appears. It's a security measure designed to protect you from man-in-the-middle attacks by verifying the authenticity of the server you're connecting to. However, in controlled environments or for non-critical systems, you might want to bypass this prompt.

The Quick Fix: StrictHostKeyChecking

One simple way to avoid this prompt is by using the StrictHostKeyChecking option. You can add this to your SSH command like this:

ssh -o StrictHostKeyChecking=accept-new user@hostname

But what if you want to make this change permanent? You can add it to your SSH config file:

  1. Open or create your SSH config file:

    vim ~/.ssh/config
    
  2. Add the following line:

    StrictHostKeyChecking accept-new
    

This setting will automatically accept and save new host keys without prompting, while still warning you if a known host's key has changed.

Security Considerations

While this method is convenient, it's important to understand the security implications:

  • It automatically accepts keys from new, unknown hosts.
  • It still protects you from potential man-in-the-middle attacks on known hosts.
  • It assumes you trust your network and the new hosts you're connecting to.

When to Use This Method

This approach is best suited for:

  • Environments where you frequently connect to new, trusted hosts.

  • Controlled, secure networks.

  • Scenarios where the convenience outweighs the risk of not manually verifying each new host.