| name | init-mysql-mac |
| description | Install, configure, and manage MySQL server on macOS using Homebrew. Use when user mentions installing MySQL on Mac, setting up MySQL database, creating MySQL user/database, securing MySQL, or managing MySQL service on macOS. Handles both interactive and automated setup. |
| allowed-tools | Bash, Read, Write, Glob, AskUserQuestion |
init-mysql-mac - MySQL Setup for macOS
Complete MySQL server installation, configuration, and management tool for macOS using Homebrew. Handles everything from fresh install to database/user creation with intelligent defaults.
When to Use This Skill
- User wants to install MySQL on macOS
- User mentions "setup MySQL on Mac" or "install MySQL Homebrew"
- User needs to create a MySQL database and user
- User wants to secure MySQL installation
- User asks about MySQL configuration on macOS
- User needs to manage MySQL service (start/stop/restart)
Core Capabilities
Installation
- Install MySQL via Homebrew
- Check existing installation
- Handle reinstallation scenarios
- Verify Homebrew availability
Security Configuration
- Secure MySQL installation
- Set root password
- Remove anonymous users
- Configure access restrictions
- Generate secure passwords
Database & User Setup
- Create database with proper charset
- Create user with privileges
- Configure connection settings
- Generate configuration files
Service Management
- Start/stop/restart MySQL service
- Check service status
- Auto-start on boot
- Troubleshoot service issues
Configuration Generation
- Create .env files
- Generate database.yaml
- Save connection strings
- Document credentials securely
Installation Modes
Interactive Mode (Default)
- Prompts for all configuration
- User sets passwords manually
- Runs mysql_secure_installation
- Full control over settings
Auto Mode (--auto)
- No user prompts
- Auto-generates secure passwords
- Uses sensible defaults
- Ideal for quick setup or scripting
Instructions
1. Pre-Installation Checks
Before starting:
Check Homebrew:
command -v brew || echo "Homebrew not installed"If not installed, guide user:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Check existing MySQL:
brew list mysql 2>/dev/null mysql --version 2>/dev/nullAsk user about mode:
- Interactive: Full control, set passwords manually
- Auto: Quick setup, auto-generated passwords
2. Install MySQL
For fresh installation:
Install via Homebrew:
brew install mysqlStart MySQL service:
brew services start mysqlWait for initialization (5 seconds):
sleep 5Verify service is running:
brew services list | grep mysql | grep started
For existing installation:
Ask user:
- Reinstall? (Stop service, uninstall, reinstall)
- Skip to configuration?
- Update existing installation?
If reinstalling:
brew services stop mysql brew uninstall mysql brew install mysql
3. Secure MySQL Installation
Interactive Mode:
Run mysql_secure_installation:
mysql_secure_installationGuide user with recommended answers:
- Set root password: YES (strong password)
- Remove anonymous users: YES
- Disallow root login remotely: YES
- Remove test database: YES
- Reload privilege tables: YES
Collect information:
- Root password (user-provided)
- Database name (default: textsql_db)
- Database user (default: textsql_user)
- User password (user-provided)
- Host (default: localhost)
- Port (default: 3306)
Auto Mode:
Generate secure passwords:
# 16-character random password ROOT_PASSWORD=$(LC_ALL=C tr -dc 'A-Za-z0-9!@#$%^&*' < /dev/urandom | head -c 16) DB_PASSWORD=$(LC_ALL=C tr -dc 'A-Za-z0-9!@#$%^&*' < /dev/urandom | head -c 16)Set root password (fresh install):
mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '${ROOT_PASSWORD}';" 2>/dev/nullUse defaults:
- Database:
textsql_db - User:
textsql_user - Host:
localhost - Port:
3306
- Database:
4. Create Database and User
Execute SQL commands:
-- Create database with UTF8MB4 charset
CREATE DATABASE IF NOT EXISTS ${DB_NAME}
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
-- Create user (only from localhost for security)
CREATE USER IF NOT EXISTS '${DB_USER}'@'localhost'
IDENTIFIED BY '${DB_PASSWORD}';
-- Grant all privileges on the specific database
GRANT ALL PRIVILEGES ON ${DB_NAME}.*
TO '${DB_USER}'@'localhost';
-- Apply changes
FLUSH PRIVILEGES;
-- Verify database exists
SHOW DATABASES;
Run via bash:
mysql -u root -p"${ROOT_PASSWORD}" <<EOF
CREATE DATABASE IF NOT EXISTS ${DB_NAME} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER IF NOT EXISTS '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PASSWORD}';
GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'localhost';
FLUSH PRIVILEGES;
SHOW DATABASES;
EOF
Verify success:
- Check exit code:
$? -eq 0 - Test connection:
mysql -u ${DB_USER} -p"${DB_PASSWORD}" ${DB_NAME} -e "SELECT 1"
5. Generate Configuration Files
Create database.yaml:
Location: ./config/database.yaml
Content:
# MySQL Database Configuration
# Auto-generated by Claude Code init-mysql-mac skill
database:
type: mysql
version: "8.0"
host: ${DB_HOST}
port: ${DB_PORT}
user: ${DB_USER}
password: ${DB_PASSWORD}
database: ${DB_NAME}
charset: utf8mb4
# Connection pool settings (optional)
pool_size: 5
max_overflow: 10
pool_timeout: 30
pool_recycle: 3600
Create/Update .env:
Location: ./.env
Before creating:
- Check if .env exists
- If exists, backup:
.env.backup.YYYYMMDD_HHMMSS - Create new .env
Content:
# =============================================================================
# Database Configuration
# =============================================================================
# Auto-generated by Claude Code init-mysql-mac skill on $(date)
DATABASE_URI=mysql+pymysql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}
DATABASE_VERSION=MySQL 8.0
# Individual connection parameters
DB_HOST=${DB_HOST}
DB_PORT=${DB_PORT}
DB_USER=${DB_USER}
DB_PASSWORD=${DB_PASSWORD}
DB_NAME=${DB_NAME}
# =============================================================================
# Add your application-specific settings below
# =============================================================================
Create my.cnf (optional):
Location: ~/.my.cnf (for client convenience)
Content:
[client]
user=${DB_USER}
password=${DB_PASSWORD}
host=${DB_HOST}
port=${DB_PORT}
database=${DB_NAME}
Set permissions:
chmod 600 ~/.my.cnf
6. Post-Installation Summary
Display to user:
Installation summary:
- Database Name
- Database User
- Host & Port
- Service status
Auto mode: Show credentials:
- ⚠️ Display generated passwords
- Warn to save securely
- Indicate where passwords are saved
Configuration files created:
- config/database.yaml
- .env
- ~/.my.cnf (optional)
Useful commands:
# Service management brew services start mysql brew services stop mysql brew services restart mysql brew services list | grep mysql # Connect to MySQL mysql -u ${DB_USER} -p ${DB_NAME} mysql -u root -p # Check MySQL status mysqladmin -u root -p status mysqladmin -u root -p variablesNext steps:
- Test connection
- Import schema/data
- Configure application
- Set up backups
Common Scenarios
Scenario 1: Fresh MySQL Installation
User: "Install MySQL on my Mac"
Actions:
- Check Homebrew installed
- Install MySQL:
brew install mysql - Start service:
brew services start mysql - Ask: Interactive or auto mode?
- If interactive: Run mysql_secure_installation
- If auto: Generate passwords, secure automatically
- Create database and user
- Generate config files
- Display summary with credentials
Scenario 2: Create New Database on Existing MySQL
User: "Create a new MySQL database called myapp"
Actions:
- Check MySQL is running
- Ask for root password (or detect from config)
- Ask for new user details
- Create database and user
- Update .env with new database info
- Test connection
- Display connection string
Scenario 3: Reset MySQL Installation
User: "Reinstall MySQL, I forgot the root password"
Actions:
- Stop MySQL:
brew services stop mysql - Uninstall:
brew uninstall mysql - Remove data directory (ask user):
rm -rf /usr/local/var/mysql # or for Apple Silicon rm -rf /opt/homebrew/var/mysql - Fresh install
- Secure installation
- Create database and user
- Generate new configs
Scenario 4: Automated Setup for CI/CD
User: "Set up MySQL automatically for testing"
Actions:
- Run in auto mode
- Use test database name
- Generate secure passwords
- Save credentials to CI environment
- Create minimal config
- Verify connection
- Return connection string
Scenario 5: Multiple Databases
User: "Create databases for dev, staging, and production"
Actions:
- Check MySQL running
- For each environment:
- Create database:
${app}_dev,${app}_staging,${app}_prod - Create user with privileges
- Generate separate .env files
- Create database:
- Create .env.dev, .env.staging, .env.prod
- Document which to use when
Service Management
Start MySQL:
brew services start mysql
# Verify started
brew services list | grep mysql
# Should show: started
Stop MySQL:
brew services stop mysql
Restart MySQL:
brew services restart mysql
Check Status:
# Service status
brew services list | grep mysql
# MySQL process
ps aux | grep mysql
# MySQL port
lsof -i :3306
Auto-start on boot:
# Enable (default with brew services start)
brew services start mysql
# Disable auto-start but keep running
brew services run mysql
Troubleshooting
MySQL Won't Start
Check logs:
# Homebrew logs
tail -f /usr/local/var/mysql/$(hostname).err
# or for Apple Silicon
tail -f /opt/homebrew/var/mysql/$(hostname).err
Common fixes:
Remove lock files:
rm -f /usr/local/var/mysql/*.pid rm -f /tmp/mysql.sock*Check permissions:
sudo chown -R $(whoami) /usr/local/var/mysql # or for Apple Silicon sudo chown -R $(whoami) /opt/homebrew/var/mysqlReinstall:
brew services stop mysql brew uninstall mysql brew install mysql brew services start mysql
Can't Connect to MySQL
Error: ERROR 2002 (HY000): Can't connect to local MySQL server through socket
Fix:
Check MySQL is running:
brew services list | grep mysqlIf not running, start:
brew services start mysql sleep 5Check socket file exists:
ls -la /tmp/mysql.sock
Access Denied
Error: ERROR 1045 (28000): Access denied for user 'user'@'localhost'
Fix:
Verify username/password
Reset user password:
mysql -u root -p ALTER USER 'username'@'localhost' IDENTIFIED BY 'new_password'; FLUSH PRIVILEGES;If root password forgotten:
- Stop MySQL
- Start in safe mode
- Reset password
- Restart normally
Port Already in Use
Error: Port 3306 already in use
Fix:
Check what's using port:
lsof -i :3306Kill process or change MySQL port:
# Edit config nano /usr/local/etc/my.cnf # Add: port = 3307 # Restart brew services restart mysql
Homebrew Not Found
Error: brew: command not found
Fix:
# Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# For Apple Silicon, add to PATH
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
Security Best Practices
Strong Passwords:
- Minimum 16 characters
- Mix of letters, numbers, symbols
- Use password generator
Limit Access:
- Users only from localhost (unless remote needed)
- Specific database privileges only
- No GRANT ALL unless necessary
Remove Defaults:
- Delete test database
- Remove anonymous users
- Disable remote root login
Secure Files:
- Protect .env:
chmod 600 .env - Protect my.cnf:
chmod 600 ~/.my.cnf - Don't commit passwords to git
- Protect .env:
Regular Updates:
brew update brew upgrade mysqlBackup Regularly:
mysqldump -u root -p ${DB_NAME} > backup_$(date +%Y%m%d).sql
Advanced Configuration
Custom my.cnf
Location: /usr/local/etc/my.cnf (Intel) or /opt/homebrew/etc/my.cnf (Apple Silicon)
Example:
[mysqld]
port = 3306
bind-address = 127.0.0.1
max_connections = 200
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
# Performance
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
# Logging
log_error = /usr/local/var/mysql/error.log
slow_query_log = 1
slow_query_log_file = /usr/local/var/mysql/slow.log
long_query_time = 2
[client]
port = 3306
default-character-set = utf8mb4
Apply changes:
brew services restart mysql
Enable Remote Access
Warning: Only enable if needed, security risk!
Edit my.cnf:
[mysqld] bind-address = 0.0.0.0Create remote user:
CREATE USER 'user'@'%' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON database.* TO 'user'@'%'; FLUSH PRIVILEGES;Firewall (if needed):
# macOS firewall allows by default # Check: System Settings > Network > Firewall
SSL/TLS Configuration
Generate certificates:
mysql_ssl_rsa_setup --datadir=/usr/local/var/mysql
Configure my.cnf:
[mysqld]
require_secure_transport = ON
ssl-ca = /path/to/ca.pem
ssl-cert = /path/to/server-cert.pem
ssl-key = /path/to/server-key.pem
Integration Examples
Python (pymysql/SQLAlchemy):
from sqlalchemy import create_engine
# From .env
DATABASE_URI = "mysql+pymysql://user:pass@localhost:3306/dbname"
engine = create_engine(DATABASE_URI)
Node.js (mysql2):
const mysql = require('mysql2');
const connection = mysql.createConnection({
host: 'localhost',
user: 'user',
password: 'password',
database: 'dbname'
});
Go (go-sql-driver):
import "database/sql"
import _ "github.com/go-sql-driver/mysql"
db, err := sql.Open("mysql", "user:password@tcp(localhost:3306)/dbname")
Output Format
Success output:
==========================================
MySQL Server Setup for macOS
==========================================
✓ Homebrew is installed
✓ MySQL installed successfully
✓ MySQL service is running
✓ MySQL secured in AUTO mode
✓ Database and user created successfully
✓ Created config/database.yaml
✓ Created .env with database configuration
==========================================
Installation Complete!
==========================================
MySQL Server Setup Summary:
Database Name: myapp_db
Database User: myapp_user
Host: localhost
Port: 3306
Auto-generated Credentials (SAVE THESE!):
MySQL Root Password: Xy9#mK2$pL4@nQ8v
Database User Password: Bw7!tR5&hN3%jM9c
⚠ IMPORTANT: Save these credentials in a secure location!
Configuration Files:
Database config: config/database.yaml
Environment: .env
Useful Commands:
Start MySQL: brew services start mysql
Stop MySQL: brew services stop mysql
Restart MySQL: brew services restart mysql
MySQL CLI: mysql -u myapp_user -p myapp_db
Check status: brew services list | grep mysql
Next Steps:
1. Test the connection
2. Import your database schema/data
3. Configure your application to use the database
Setup complete! Happy developing! 🚀
Usage with Claude Code
Natural commands that trigger this skill:
- "Install MySQL on my Mac"
- "Set up MySQL database on macOS"
- "Create a new MySQL database called myapp"
- "Help me configure MySQL on Mac"
- "Reset my MySQL installation"
- "Generate MySQL config files"
- "Secure my MySQL installation"
Claude will handle all the details automatically!