Skip to content

Access Vagrant mySQL database from PhpStorm

Laravel is a great PHP based software development framework to develop Web based software applications. PHPStorm is a Lightning-Smart PHP IDE that can really fuel your PHP software development process and combined with Laravel it really helps developers to be suoer productive.

For the most part there is not really that much set up required make PHPStorm to work with a Laravel project and if you have installed and configured Laravel on your Ubuntu Desktop you are pretty much good to go. All you need to do open your project directory in in PHP storm and you can start coding!

The Laravel Homestead is an official, pre-packaged Vagrant box that provides you a wonderful development environment without requiring you to install PHP, a web server, and any other server software on your local machine. No more worrying about messing up your operating system! Vagrant boxes are completely disposable. If something goes wrong, you can destroy and re-create the box in minutes!

It works great, and for the most part developers never really need to concern themselves with the minutae of working with the various components involved.

Working with Databases on Homestead

Laravel comes prepackaged powerful REPL CLI , called Tinker, powered by the PsySH console by Justin Hileman under the hood. The tinker console allows you to interact with your Laravel application from the command line in an interactive shell.

 Using Tinker we can interact with the database through command-line and can even test and use Tinker to add dummy data to your database table and you don’t have access to the database. In such cases, you can insert data through Tinker. We can even update, delete table records in the database.

This is great tool, but occassionally seasoned developers would like to connect directly to their databases to write complex SQL queries or even just to check schema changes or maybe even to optimise the databases etc.

PHPStorm comes with great features to enable to connect to a number of different databases directly within the IDE. This is a great feature which helps developers to troubleshoot and debug database issues directly within there development environment.

It is possible to connect to your mySQL database in your Homestead environment using PHPStorm all that is required is some additional configuration.

Open the Database window

To open your Database window in PHPStorm simply click on VIew --> Tool Windows --> Database and the Database Sidebar will open

PHPStorm Database Window

Configure SSH Tunnel

When creating a Homestead Environment all components and databases etc utilised by your Laravel application are contained within your Vagrant Machine. The only way you can access these utilities are by using SSH. Fortunately, PHPStorm enables you to connect to database making use of SSH Tunnels.

In order to do so we will need to get the SSH Configuration details, which fortunately Laravel makes super easy for us with a simple Terminal Command we can use. So by accessing the PHPStorm integrated terminal window and ensuring we are in the project root directory we can use the following command

Shell

which will provide a result similar too

vagrant ssh-config

We are now going to use these details in order to connect to our database in the Homestead environment

Using the Database window click on the Plus icon --> DataSource --> mySQL

PHPStorm Database connect

When the Data Sources and Drivers window opens select SSH/SSL tab and ensure Use SSH Tunnel is selected

We can now enter the data we retrieved from our ssh-configuration as follows:

Once all the data has been completed. Switch back to the General tab and then add in your database credentials. For the sake of this article we're going to use the default homestead credentials.

We can then click Test Connection and get a confirmation that our connection was successful.

Summary

In this post we covered how to connect PHPstorm to your Laravel Homestead Database.

Gary Woodfine
Latest posts by Gary Woodfine (see all)