A lightweight, high-performance CLI tool written in Go to export MySQL/MariaDB tables directly to CSV files. Designed for speed, portability, and ease of use in Linux/WSL environments.
- Zero Dependencies: Compiles to a single static binary. No need for Go, Python, or PHP runtimes on the target server.
- Efficient Memory Usage: Streams data row-by-row, allowing for the export of massive datasets without exhausting RAM.
- Smart Type Handling: Automatically handles MySQL types, including converting
[]byte(blobs/text) to strings and managingNULLvalues. - Flexible Filtering: Supports raw SQL
WHEREclauses via a--filterflag. - Secure: Supports environment variables and URL-encoded credentials for safe database connections.
- Ensure you have Go installed.
- Clone the repository:
git clone https://github.com/demoniodojo/db2csv.git cd db2csv - Build the binary:
go build -o db2csv main.go
- (Optional) Move to your path:
sudo mv db2csv /usr/local/bin/
Basic command structure:
db2csv [flags] <database.table> <output.csv>If output.csv is omitted then table.csv is used.
Standard Export:
db2csv --user root --password mysecret my_database.users users_backup.csvUsing a Filter:
db2csv --user admin --password pass --filter "status='active' AND created_at > '2023-01-01'" web_db.orders active_orders.csvRemote Connection:
db2csv --host 192.168.1.50 --port 3307 --user remote_user mydb.products products.csv--user: Database username (required).--password: Database password (required).--host: Database hostname (default:127.0.0.1).--port: Database port (default:3306).--filter: Optional SQL snippet for theWHEREclause.--no-header: Optional, if used then no column names are included in the csv.
- Language: Go (Golang)
- Database Driver:
github.com/go-sql-driver/mysql - Architecture: - Uses
sql.RawBytesandinterface{}slices for dynamic row scanning, enabling compatibility with any table schema without pre-definition.- Implements
Type Assertionto safely convert raw database bytes into UTF-8 strings. - Utilizes
deferstacks for guaranteed resource cleanup (database handles and file buffers).
- Implements
MIT
Chris Carias