While MySQL may not run on the server you connect over SSH into, (depending on your application's backend architecture) you can use Drush to either connect to MySQL's CLI directly, or, print out the MySQL connection string.
In order to connect to a site's database, SSH into your application and run drush sql-cli
(or drush sqlc
for short) providing the --uri
argument if needed:
example@instance-123456:/var/www/html/example.prod/docroot$ drush sqlc --uri=www.example.com
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3783831
Server version: 5.6.32-78.1-log Percona Server (GPL), Release 78.1, Revision 4c779b7
Copyright (c) 2009-2016 Percona LLC and/or its affiliates
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Tip: You can use drush sql-cli --extra=-A
to skip the initial reading of table information
If for some reason you need to to print out the MySQL connection string, you can use drush sql-connect
(but be careful as, by default, that will also print out the database user's password)
example@instance-123456:/var/www/html/example.prod/docroot$ drush sql-connect
mysql --user=s123456 --password=xxxxxxxxxxxx --database=examplrzafzs3cco --host=instance-234567 --port=3306 -A
Tip: You can use drush sql-cli --extra=-A
to skip the initial reading of table information
Example use: You can pipe the command to import a backup or series of queries/statements:
`drush sql-connect` < example.sql