Accessing Your Vagrant VM's MySQL Database From the Host Computer
I love how convent PuPHPet makes setting up a Vagrant VM. One of the things that I don't like about it is the fact that you can't access your VMs databases using native tools like MySQL Workbench and Sequel Pro. There are two reasons for this, the first is that by default MySQL is setup to only accept connections from the localhost and the second is that the user created by Puppet can only access the site though local connections or remote connections (it's a slight flaw in the MySQL defaults) but not both.
To fix the problem where MySQL will only accept connections from localhost we need to find the following line in our default.pp
:
class { 'mysql::server': config_hash => { 'root_password' => 'zendpass' } }
And change it to this (whitespace is optional but I added it for readability:
class { 'mysql::server':
config_hash => {
'root_password' => 'zendpass',
'bind_address' => '192.168.56.101',
}
}
This will change the bind_address parameter inside my.ini to the external IP address of the VM.
Next you need to find a section that looks like this:
mysql::db { 'database':
grant => [
'ALL'
],
user => 'username',
password => 'password',
host => 'localhost',
charset => 'utf8',
require => Class['mysql::server'],
}
We're going to add the following after this:
database_user { 'username@%':
password_hash => mysql_password('password')
}
database_grant { 'usernamer@%/database':
privileges => ['all'] ,
}
This will add an additional permission to the username user so they have access to the database from all hosts.
Now a quick vagrant reload
will cause these permissions to be loaded.
Finally, when you're ready to connect you just connect using the IP address of the VM (192.168.56.101 if your using our setup guide to PuPHPet) and the username and password specified.
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows