MySQL CLI Cheat Sheet
Use the MySQL command line to connect, inspect databases, run queries, export backups, import SQL files and check common WordPress settings.
Connect to MySQL
Connect as user
mysql -u username -pConnect to database
mysql -u username -p database_nameConnect to host
mysql -h hostname -u username -pRun one query
mysql -u username -p -e "SHOW DATABASES;"Inspect databases and tables
| Task | SQL |
|---|---|
| Show databases | SHOW DATABASES; |
| Select database | USE database_name; |
| Show tables | SHOW TABLES; |
| Describe table | DESCRIBE wp_options; |
| Show table size | SHOW TABLE STATUS LIKE 'wp_options'\G |
Useful query examples
Count rows
SELECT COUNT(*) FROM wp_posts;Show latest posts
SELECT ID, post_title, post_date FROM wp_posts ORDER BY post_date DESC LIMIT 10;Find large options
SELECT option_name, LENGTH(option_value) AS size FROM wp_options ORDER BY size DESC LIMIT 10;Check users
SELECT ID, user_login, user_email FROM wp_users;Export databases with mysqldump
Export database
mysqldump -u username -p database_name > backup.sqlCompressed export
mysqldump -u username -p database_name | gzip > backup.sql.gzExport one table
mysqldump -u username -p database_name wp_options > wp_options.sqlExport schema only
mysqldump -u username -p --no-data database_name > schema.sqlImport SQL files
Import SQL file
mysql -u username -p database_name < backup.sqlImport compressed dump
gunzip < backup.sql.gz | mysql -u username -p database_nameCheck file first
head -50 backup.sqlShow dump size
du -sh backup.sqlWordPress database checks
Check site URLs
SELECT option_name, option_value FROM wp_options WHERE option_name IN ('siteurl','home');Update siteurl
UPDATE wp_options SET option_value='https://example.com' WHERE option_name='siteurl';Update home
UPDATE wp_options SET option_value='https://example.com' WHERE option_name='home';Check active plugins
SELECT option_value FROM wp_options WHERE option_name='active_plugins'\GFor safer WordPress URL changes, WP-CLI is usually better because it handles serialized data. See the WP-CLI Search Replace Guide.
Safety tips
- Export a backup before running UPDATE or DELETE queries.
- Use SELECT first to confirm the rows you will change.
- Be careful with WordPress serialized data. Prefer WP-CLI search-replace where possible.
- Do not paste database passwords into shared terminals or screenshots.
MySQL CLI workflows for WordPress and hosting
Connect
mysql -u dbuser -p database_nameList tables
SHOW TABLES;Check WordPress URLs
SELECT option_name, option_value FROM wp_options WHERE option_name IN ('siteurl','home');Export database
mysqldump -u dbuser -p database_name > backup.sqlFrequently Asked Questions
How do I connect to MySQL from the command line?
Use mysql -u USER -p DATABASE_NAME.
How do I list MySQL tables?
Run SHOW TABLES; after selecting a database.
How do I export a database?
Use mysqldump -u USER -p DATABASE_NAME > backup.sql.
Should I update WordPress URLs directly in MySQL?
Prefer WP-CLI search-replace for URL changes because it handles serialized data.