Need to initiate a command line MySQL connection? We've got your covered! In this article, we'll show you how to do it either by using WP-CLI or directly using the mysql command.
WP-CLI is installed on all VPS/Enterprise hosting accounts and is a great tool to simplify WordPress-related actions, including connecting to your database.
For more information, take a look at our guide on connecting to your database with WP-CLI.
If you'd prefer not to use WP-CLI to initiate my MySQL connection, you can always do so manually using the mysql command.
Note: Due to security procedures, you won't be able to connect directly to your MySQL database from a remote location. If you need initialize a MySQL connection, you'll need to either do so from an SSH session on the server or through an SSH tunnel.
Getting Your Connection Details
To get your MySQL connection details such as your username, password, and database host, perform the following:
- Connect to your site via SSH.
- Next, run the following command, replacing example.com with your site's primary domain:
cat ~/sites/example.com/wp-config-hosting.php | grep define
- After running the previous command, you'll be shown several definitions that exist inside the wp-config-hosting.php file. The information that you'll need is the following:
- DB_NAME: Your database name.
- DB_USER: The username associated with your database.
- DB_PASSWORD: Your database user's password.
- DB_HOST: The host where your database is located.
Connecting To Your MySQL Database
Once you have all of your database connection details, use the following command to connect to your MySQL database:
mysql -u DB_USER -p DB_PASSWORD -h DB_HOST DB_PASSWORD
After initializing the connection, you should now see a MySQL command prompt.