Skip to main content

Accessing the Database CLI

The MySQL/MariaDB command-line client (mysql) is the fastest way to inspect a WordPress database, validate credentials, and run targeted administrative queries. Use it for read-only checks first (status, table lists, simple SELECTs), then progress to changes only when you have a backup and you are certain you're on the right server and database.

Quick Summary

Use mysql -u wpuser -p -D wordpress to connect to the WordPress database. Use mysql -e "SQL" for one-off commands in scripts.

Mental Model

Prerequisites

  • MySQL or MariaDB server installed and running.
  • The mysql client installed (usually installed with the server package).
  • A username + password with appropriate privileges.

Verify the client is available:

verify-mysql-client.sh
mysql --version

Core Syntax

mysql-cli-syntax.sh
mysql [OPTIONS] [DATABASE]

Common connection flags:

  • -u <user> username
  • -p prompt for password
  • -h <host> host (use 127.0.0.1 for TCP to local host)
  • -P <port> port (default 3306)
  • -S <socket> Unix socket path
  • -D <db> default database to use

Key Options

OptionWhat it doesExampleWordPress / VPS use case
-u <user>Set usernamemysql -u wpuser -pUse least-privilege DB user
-pPrompt for passwordmysql -u wpuser -pAvoid putting passwords in command history
-D <db>Select databasemysql -u wpuser -p -D wordpressConnect directly to the WP DB
-e "SQL"Execute one commandmysql -u wpuser -p -D wordpress -e "SHOW TABLES;"Automation / quick checks
-h <host>Connect to a hostmysql -h 127.0.0.1 ...Force TCP instead of socket
-S <path>Use Unix socketmysql -S /var/run/mysqld/mysqld.sock ...Fix socket-path issues

Examples (Commands + Expected Output)

Output varies

Prompts and output formatting vary by version. Use the patterns and flags.

Check the client version

mysql-client-version.sh
mysql --version

Expected output:

example-output-mysql-version.txt
mysql Ver 8.0.xx for Linux on x86_64 (MySQL Community Server)

Use case: Confirm the client exists and record the version.

Connect locally as root (interactive)

mysql-connect-root.sh
mysql -u root -p

Expected output:

example-output-mysql-connect.txt
Enter password:
Welcome to the MySQL monitor.
mysql>

Use case: Admin access for troubleshooting (use with care).

Connect directly to the WordPress database

mysql-connect-wordpress-db.sh
mysql -u wpuser -p -D wordpress

Expected output:

example-output-mysql-wp-db.txt
Enter password:
mysql>

Use case: Run queries against the right database without extra USE commands.

Run a one-off query (non-interactive)

mysql-show-tables.sh
mysql -u wpuser -p -D wordpress -e "SHOW TABLES;"

Expected output:

example-output-mysql-show-tables.txt
Tables_in_wordpress
wp_options
wp_posts
wp_postmeta
...

Use case: Quick validation that credentials and database selection are correct.

Run a query and print a single value

mysql-count-posts.sh
mysql -u wpuser -p -D wordpress -e "SELECT COUNT(*) AS posts FROM wp_posts;"

Expected output:

example-output-mysql-count.txt
posts
1234

Use case: Sanity-check database size/content during migrations.

Connect via a specific socket

mysql-connect-socket.sh
mysql -u root -p -S /var/run/mysqld/mysqld.sock

Expected output:

example-output-mysql-socket.txt
Enter password:
mysql>

Use case: Fix ERROR 2002 when the client is using the wrong socket path.

warning

Avoid exposing MySQL/MariaDB to the public internet. If you must connect remotely, restrict by firewall/VPN and use TLS.

WordPress VPS Use Cases

TaskCommand patternNotes
Verify credentialsmysql -u wpuser -p -D wordpress -e "SHOW TABLES;"Fast smoke test
Confirm DB host volumedf -h /var/lib/mysqlCapacity planning
Quick read-only checksSELECT ...Do read-only first

Troubleshooting

ErrorLikely causeFix
Access denied for userWrong password or privilegesVerify user and grants; check wp-config settings
ERROR 2002 (HY000)Cannot connect (socket or service down)Check service status and socket path; try -h 127.0.0.1
Unknown databaseDB name mismatchList databases with SHOW DATABASES;

Best Practices

  • Use a dedicated least-privilege DB user for WordPress.
  • Keep passwords out of shell history (use -p prompt).
  • Take a backup before making changes.
Cheat Sheet
mysql-cli-cheat-sheet.sh
mysql --version
mysql -u root -p
mysql -u wpuser -p -D wordpress
mysql -u wpuser -p -D wordpress -e "SHOW TABLES;"
mysql -u root -p -S /var/run/mysqld/mysqld.sock

What's Next