One‑Click Deployment of MySQL, Redis, and PostgreSQL on WSL
This guide shows how to install, configure, and manage MySQL/MariaDB, Redis, and PostgreSQL inside Windows Subsystem for Linux, including remote access setup, common command‑line operations, GUI client recommendations, and scripts for one‑click start/stop and backup.
Why run databases in WSL?
Traditional solutions problems:
❌ Install MySQL on Windows → path/permissions/service issues
❌ Docker for Windows (old) → poor performance, slow startup
❌ Cloud development databases → network latency, cost, data security
WSL solution advantages:
✅ Native Linux performance, matches production servers
✅ localhost direct connection, zero latency
✅ systemd management, start on boot
✅ Data persistence, no loss on reboot
✅ Multiple versions coexist, switch anytime1. MySQL / MariaDB
Install MariaDB (MySQL fully open‑source alternative)
# Install
sudo apt update
sudo apt install -y mariadb-server
# Start service
sudo systemctl start mariadb
sudo systemctl enable mariadb # start on boot
# Secure initialization
sudo mysql_secure_installation
# Prompts:
# 1. Set root password? Y → enter password
# 2. Remove anonymous users? Y
# 3. Disallow root login remotely? N (needed for development)
# 4. Remove test database? Y
# 5. Reload privilege tables? Y
# Verify status
sudo systemctl status mariadb # should show: active (running) ✅Create user and database
# Login to MariaDB
sudo mysql -u root -p
-- Create database
CREATE DATABASE myapp
DEFAULT CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
-- Create user and grant privileges ("%" allows any host)
CREATE USER 'devuser'@'%' IDENTIFIED BY 'dev_password_123';
GRANT ALL PRIVILEGES ON myapp.* TO 'devuser'@'%';
FLUSH PRIVILEGES;
-- Verify connection
SHOW DATABASES;
SELECT User, Host FROM mysql.user;
-- Exit
EXIT;Allow remote connections
# Edit configuration file
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
# Change the following lines:
# bind-address = 0.0.0.0
# Ensure port = 3306 (default)
# Restart to apply
sudo systemctl restart mariadb
# Test local connection
mysql -u devuser -p -h 127.0.0.1 myapp # should connect ✅Common operation cheat sheet
-- List all databases
SHOW DATABASES;
-- List tables in current database
SHOW TABLES;
-- Describe table structure
DESCRIBE tablename;
-- or
SHOW COLUMNS FROM tablename;
-- Import SQL file
source /path/to/dump.sql # or: mysql -u user -p dbname < dump.sql
-- Export database
mysqldump -u devuser -p myapp > backup_$(date +%Y%m%d).sql2. Redis – In‑memory database king
Install and start
# Install
sudo apt install -y redis-server
# Start
sudo systemctl start redis-server
sudo systemctl enable redis-server # start on boot
# Verify (should return PONG)
redis-cli ping # PONGCommon operations
# Connect to Redis
redis-cli
# Basic operations
SET mykey "Hello WSL"
GET mykey # "Hello WSL"
DEL mykey
# Data type examples
## Hash (object storage)
HSET user:1001 name "Zhang San" age 28 city "Beijing"
HGETALL user:1001 # 1) name 2) "Zhang San" 3) age 4) "28"
## List (queue)
LPUSH tasks "deploy code"
LPUSH tasks "write tests"
LPUSH tasks "fix bug"
LRANGE tasks 0 -1 # 1) "fix bug" 2) "write tests" 3) "deploy code"
## Set (deduplication / tags)
SADD tags python linux wsl
SMEMBERS tags
## Sorted Set (leaderboard)
ZADD leaderboard 2500 "Alice"
ZADD leaderboard 3000 "Bob"
ZADD leaderboard 2800 "Charlie"
ZREVRANGE leaderboard 0 -1 WITHSCORES
# Useful commands
KEYS * # list all keys (use with caution in production)
DBSIZE # number of keys
INFO server # server information
FLUSHALL # clear all data ⚠️
EXITRedis configuration optimization
# Backup original config
sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.bak
# Edit config (development‑friendly settings)
sudo nano /etc/redis/redis.confKey configuration items:
# Bind address (allow remote access)
bind 0.0.0.0
# Set password (required in production, optional for dev)
requirepass your_dev_password
# Persistence: RDB snapshots
save 900 1 # at least 1 write within 900 s
save 300 10 # at least 10 writes within 300 s
save 60 10000 # at least 10000 writes within 60 s
dbfilename dump.rdb
dir /var/lib/redis
# Max memory limit (adjust to your system)
maxmemory 512mb
maxmemory-policy allkeys-lru # Restart to apply new config
sudo systemctl restart redis-server
# Verify new config
redis-cli CONFIG GET maxmemory
redis-cli INFO server | grep redis_version3. PostgreSQL – Most feature‑rich open‑source database
Install
# Ubuntu 24.04 can install directly
sudo apt install -y postgresql postgresql-contrib
# Start
sudo systemctl start postgresql
sudo systemctl enable postgresql
# Verify
sudo systemctl status postgresql # active (running) ✅
# Check version
psql --version # psql (PostgreSQL) 16.xCreate user and database
PostgreSQL uses the system user postgres for administration.
# Switch to postgres user
sudo -u postgres psql
-- Create user with password
CREATE USER devuser WITH PASSWORD 'dev_pass_123';
-- Create database owned by the new user
CREATE DATABASE myapp OWNER devuser;
-- Grant privileges
GRANT ALL PRIVILEGES ON DATABASE myapp TO devuser;
-- Exit psql
\qConnection test
# Connect with the new user
psql -h localhost -U devuser -d myapp # enter password to connect
-- Test table creation
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
INSERT INTO users (name, email) VALUES ('Test', '[email protected]');
SELECT * FROM users;
\qRemote connection configuration
# Edit pg_hba.conf (client authentication)
sudo nano /etc/postgresql/16/main/pg_hba.conf
# Add the following line under "IPv4 local connections":
host all all 0.0.0.0/0 scram-sha-256
# Edit postgresql.conf (listen addresses)
sudo nano /etc/postgresql/16/main/postgresql.conf
# Change:
# listen_addresses = 'localhost'
listen_addresses = '*'
# Restart PostgreSQL to apply
sudo systemctl restart postgresql
# Confirm port is listening
ss -tlnp | grep 5432 # LISTEN 0 128 0.0.0.0:54324. GUI client tool recommendations
DBeaver – Free, full‑featured, supports all major databases (Win/Linux/Mac) – Free
TablePlus – Attractive UI, lightweight, fast (Win/Mac/Linux) – Partially paid
DataGrip – Powerful, intelligent SQL (JetBrains family) – Paid (free tier available)
Beekeeper Studio – Open‑source, free, modern UI (cross‑platform) – Free
phpMyAdmin – Web‑based MySQL client, browser access (Web) – Free
DBeaver configuration steps (recommended)
1. Install DBeaver on Windows (exe installer)
2. Open DBeaver → New Database Connection
3. Choose database type (MySQL / PostgreSQL / Redis, etc.)
MySQL connection settings:
├── Host: localhost
├── Port: 3306
├── Database: myapp
├── Username: devuser
└── Password: dev_password_123
PostgreSQL connection settings:
├── Host: localhost
├── Port: 5432
├── Database: myapp
├── Username: devuser
└── Password: dev_pass_123
Redis connection (requires Redis plugin):
├── Host: localhost
├── Port: 6379
└── Password: (if set)
4. Click "Test Connection" → Save on successGUI clients run on Windows and connect to the WSL databases via localhost; the mirrored network mode or automatic port forwarding makes the experience identical to a local database.
5. One‑click management script
Create a convenient management script:
#!/bin/bash
# Database management script
case "$1" in
start)
echo "🚀 Starting all databases..."
sudo systemctl start mariadb
sudo systemctl start redis-server
sudo systemctl start postgresql
echo "✅ All started"
;;
stop)
echo "⏹️ Stopping all databases..."
sudo systemctl stop mariadb
sudo systemctl stop redis-server
sudo systemctl stop postgresql
echo "✅ All stopped"
;;
status)
echo "=== MariaDB ==="
systemctl is-active mariadb
echo "=== Redis ==="
systemctl is-active redis-server
echo "=== PostgreSQL ==="
systemctl is-active postgresql
;;
ports)
echo "=== Database ports in use ==="
ss -tlnp | grep -E ':(3306|5432|6379)\s'
;;
*)
echo "Usage: $0 {start|stop|status|ports}"
;;
esac
SCRIPT
chmod +x ~/db-manager.sh
# Usage examples:
# ~/db-manager.sh start # start all
# ~/db-manager.sh status # check status
# ~/db-manager.sh ports # view ports
# ~/db-manager.sh stop # stop all6. Data backup strategy
#!/bin/bash
# Save as ~/backup-db.sh
BACKUP_DIR="/mnt/d/db-backups/$(date +%Y%m%d)"
mkdir -p "$BACKUP_DIR"
echo "📦 Starting backup to $BACKUP_DIR"
# Backup MySQL/MariaDB
mysqldump -u devuser -p'dev_password_123' myapp > "$BACKUP_DIR/mysqldb_$(date +%H%M%S).sql"
# Backup PostgreSQL
pg_dump -U devuser -h localhost myapp > "$BACKUP_DIR/pgdb_$(date +%H%M%S).sql"
# Backup Redis
redis-cli BGSAVE
cp /var/lib/redis/dump.rdb "$BACKUP_DIR/redis_$(date +%H%M%S).rdb"
echo "✅ Backup completed! Files located in $BACKUP_DIR"
ls -lh "$BACKUP_DIR"
SCRIPTNext issue preview
Upcoming article: Advanced Optimization – "WSL 2 Performance Truth: Why It Can Be Slower Than Windows?"
📊 File I/O benchmark comparisons
🔍 Deep analysis of three major performance bottlenecks
⏱️ Performance tests for different scenarios
🛠️ Bottleneck identification and solutions
💡 Best practices for performance tuning
What database do you mainly use? Share your experience in the comments! UbuntuNews — Focused on Ubuntu/Linux tech sharing
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ubuntu
Focused on Ubuntu/Linux tech sharing, offering the latest news, practical tools, beginner tutorials, and problem solutions. Connecting open-source enthusiasts to build a Linux learning community. Join our QQ group or channel for discussion!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
