mysql
Administer MySQL/MariaDB databases. Configure replication and optimize performance. Use when managing MySQL deployments.
What this skill does
# MySQL / MariaDB
Administer, optimize, and secure MySQL and MariaDB databases in development and production environments.
## When to Use
- You need a mature, widely supported relational database.
- Your stack depends on MySQL-specific features or compatibility (WordPress, Magento, many PHP frameworks).
- You are setting up source-replica replication for read scaling.
- You want to tune InnoDB for high-throughput transactional workloads.
## Prerequisites
- Linux server (Debian/Ubuntu or RHEL-based) or Docker.
- Root or sudo access for package installation.
- Familiarity with SQL fundamentals.
## Installation and Setup
```bash
# Debian / Ubuntu — MySQL 8
sudo apt update
sudo apt install -y mysql-server
# RHEL / Amazon Linux
sudo dnf install -y mysql-server
sudo systemctl enable --now mysqld
# Run the secure installation wizard
sudo mysql_secure_installation
# Prompts: set root password, remove anonymous users, disable remote root, remove test db
# Verify
mysql --version
sudo systemctl status mysql
```
## Initial User and Database Setup
```bash
sudo mysql -u root -p
```
```sql
-- Create a database
CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- Create an application user with strong auth
CREATE USER 'myapp'@'%' IDENTIFIED BY 'strong_password_here';
GRANT SELECT, INSERT, UPDATE, DELETE ON mydb.* TO 'myapp'@'%';
FLUSH PRIVILEGES;
-- Verify
SHOW GRANTS FOR 'myapp'@'%';
```
## mysql CLI Reference
```bash
# Connect
mysql -u myapp -p -h 127.0.0.1 mydb
# Execute a single statement
mysql -u myapp -p -e "SELECT COUNT(*) FROM orders;" mydb
# Import a SQL file
mysql -u myapp -p mydb < schema.sql
# Export query results to CSV
mysql -u myapp -p -e "SELECT * FROM users" mydb \
| tr '\t' ',' > users.csv
```
```
-- Inside the mysql shell
SHOW DATABASES;
USE mydb;
SHOW TABLES;
DESCRIBE users;
SHOW CREATE TABLE users\G
SHOW PROCESSLIST;
SHOW ENGINE INNODB STATUS\G
```
## Configuration Tuning
Edit `/etc/mysql/mysql.conf.d/mysqld.cnf` (or `/etc/my.cnf` on RHEL).
```ini
[mysqld]
# -- Networking --
bind-address = 0.0.0.0
max_connections = 300
wait_timeout = 600
interactive_timeout = 600
# -- InnoDB (most impactful settings) --
innodb_buffer_pool_size = 4G # ~70% of RAM on a dedicated server
innodb_buffer_pool_instances = 4 # 1 per GB of pool (up to 64)
innodb_log_file_size = 1G
innodb_flush_log_at_trx_commit = 1 # 1 = ACID; 2 = faster, slight risk
innodb_flush_method = O_DIRECT # avoids double buffering on Linux
innodb_io_capacity = 2000 # raise for SSD
innodb_io_capacity_max = 4000
# -- Query cache (disabled in MySQL 8, use ProxySQL or app cache) --
# query_cache_type = 0
# -- Logging --
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1
log_error = /var/log/mysql/error.log
# -- Binary log (required for replication) --
server-id = 1
log_bin = /var/log/mysql/mysql-bin
binlog_expire_logs_seconds = 604800 # 7 days
sync_binlog = 1
# -- Character set --
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
```
```bash
# Apply changes
sudo systemctl restart mysql
# Verify a setting at runtime
mysql -u root -p -e "SHOW VARIABLES LIKE 'innodb_buffer_pool_size';"
```
## Backup and Restore
### Logical Backups with mysqldump
```bash
# Single database
mysqldump -u root -p --single-transaction --routines --triggers \
mydb > /backups/mydb_$(date +%F).sql
# All databases
mysqldump -u root -p --all-databases --single-transaction \
> /backups/all_$(date +%F).sql
# Compressed backup
mysqldump -u root -p --single-transaction mydb \
| gzip > /backups/mydb_$(date +%F).sql.gz
# Restore
mysql -u root -p mydb < /backups/mydb_2025-01-15.sql
# Restore compressed
gunzip < /backups/mydb_2025-01-15.sql.gz | mysql -u root -p mydb
```
### Physical Backups with Percona XtraBackup
```bash
# Install
sudo apt install -y percona-xtrabackup-80
# Full backup
xtrabackup --backup --user=root --password=secret \
--target-dir=/backups/full_$(date +%F)
# Prepare the backup (apply redo logs)
xtrabackup --prepare --target-dir=/backups/full_2025-01-15
# Restore (stop MySQL first)
sudo systemctl stop mysql
sudo rm -rf /var/lib/mysql/*
xtrabackup --move-back --target-dir=/backups/full_2025-01-15
sudo chown -R mysql:mysql /var/lib/mysql
sudo systemctl start mysql
```
### Incremental Backup with XtraBackup
```bash
# Incremental based on the full backup
xtrabackup --backup --user=root --password=secret \
--target-dir=/backups/inc_$(date +%F) \
--incremental-basedir=/backups/full_2025-01-15
# Prepare: apply full, then incremental
xtrabackup --prepare --apply-log-only --target-dir=/backups/full_2025-01-15
xtrabackup --prepare --target-dir=/backups/full_2025-01-15 \
--incremental-dir=/backups/inc_2025-01-16
```
## Source-Replica Replication
### Source (Primary)
```ini
# /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
server-id = 1
log_bin = /var/log/mysql/mysql-bin
binlog_format = ROW
```
```sql
-- Create replication user
CREATE USER 'replicator'@'10.0.0.%' IDENTIFIED BY 'repl_secret';
GRANT REPLICATION SLAVE ON *.* TO 'replicator'@'10.0.0.%';
FLUSH PRIVILEGES;
-- Get current binary log position
SHOW MASTER STATUS\G
```
### Replica
```ini
# /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
server-id = 2
relay_log = /var/log/mysql/relay-bin
read_only = ON
```
```sql
-- Point replica to source (use SHOW MASTER STATUS values)
CHANGE REPLICATION SOURCE TO
SOURCE_HOST = '10.0.0.1',
SOURCE_USER = 'replicator',
SOURCE_PASSWORD = 'repl_secret',
SOURCE_LOG_FILE = 'mysql-bin.000003',
SOURCE_LOG_POS = 154;
START REPLICA;
-- Verify
SHOW REPLICA STATUS\G
-- Check: Replica_IO_Running = Yes, Replica_SQL_Running = Yes, Seconds_Behind_Source = 0
```
## Monitoring Queries
```sql
-- Connection statistics
SHOW STATUS LIKE 'Threads_connected';
SHOW STATUS LIKE 'Max_used_connections';
-- InnoDB buffer pool hit ratio (should be > 99%)
SELECT
ROUND(100 - (
(SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME = 'Innodb_buffer_pool_reads') /
(SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME = 'Innodb_buffer_pool_read_requests')
) * 100, 2) AS buffer_pool_hit_pct;
-- Top 10 slow queries (requires performance_schema)
SELECT DIGEST_TEXT, COUNT_STAR, AVG_TIMER_WAIT / 1e12 AS avg_sec
FROM performance_schema.events_statements_summary_by_digest
ORDER BY AVG_TIMER_WAIT DESC
LIMIT 10;
-- Table sizes
SELECT table_name,
ROUND(data_length / 1024 / 1024, 2) AS data_mb,
ROUND(index_length / 1024 / 1024, 2) AS index_mb,
table_rows
FROM information_schema.tables
WHERE table_schema = 'mydb'
ORDER BY data_length DESC;
-- Check replication lag
SHOW REPLICA STATUS\G
-- Look at Seconds_Behind_Source
```
## Docker Compose Setup
```yaml
# docker-compose.yml
version: "3.9"
services:
mysql:
image: mysql:8.0
restart: unless-stopped
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: mydb
MYSQL_USER: myapp
MYSQL_PASSWORD: secret
volumes:
- mysql_data:/var/lib/mysql
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
command: >
--innodb-buffer-pool-size=512M
--max-connections=200
--slow-query-log=ON
--long-query-time=1
--character-set-server=utf8mb4
--collation-server=utf8mb4_unicode_ci
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-prootpass"]
interval: 10s
timeout: 5s
retries: 5
phpmyadmin:
image: phpmyadmin:latest
restart: unless-stopped
ports:
- "8080:80"
environment:
PMA_HOST: mysql
PMA_USER: root
PMA_PASSWORD: rootpass
depends_on:
mysql:
condition: service_healthy
volumes:
mysql_datRelated in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.