Accessing MySql on an Ubuntu Virtual Machine Using VMWare Fusion
In my previous post, I mentioned that I was creating our schema.rb on a Linux version of MySql.
I originally tried to have my local version of MySql run with its data directory located on a Mac Disk image that was case sensitive. However, I found that to be too brittle. If my system needed to be forced powered-off for any reason, the image would break, and I would have to set a new one up from scratch. With VMWare's Fusion package, I've only had that happen once when my disk had become corrupted.
However there are a couple of things that have to be configured in order to make this work effectively.
First make sure that your VMWare ubuntu image is using NAT (Share with my Mac) in order to access the network to download MySql.
Get your ubuntu IP address then ensure that you can ping it from your Mac. In an ubuntu window run:
And look for the IP address for the eth0 interface. While you have the window up, note the HWAddr of the same interface. We'll need it later.
Then in your Mac Terminal, ping that IP address:
First use the package manager to download and install MySql.
sudo apt-get mysql-server
You also have to enable the ubuntu version of MySql to accept external connections. Otherwise you can not connect to it from your Mac.
Edit your `/etc/mysql/my.cnf`` and comment out one line:
Run the mysql client from inside an Ubuntu terminal and create a user to have access from an IP address other than localhost:
mysql -u root -p <Enter your root password> mysql> GRANT ALL ON test_development.* TO 'rails_user'@'%' IDENTIFIED BY 'some_password'; mysql> FLUSH PRIVILEGES;
Verify that you can access the MySql server from your Mac Terminal:
mysql test_development -h 172.16.23.131 -u rails_user -p <Enter the rails_user password> mysql>
Just one more thing to do! This works as is, but quickly becomes VERY annoying because the VM will periodically refresh its IP address. Out of the box, a VMWare image uses DHCP to dynamically grab an available IP Address. However if this is going to be our development database, we would really prefer to use a FIXED IP address.
Setting a Fixed IP Address for Your Ubuntu Image
VMWare's site isn't very forthcoming for the Mac configuration, but I found a very useful post from Andrew Elkins.
Remember that HWAddr from running ifconfig inside an Ubuntu terminal? We are now going to use that here.
sudo vim /Library/Preferences/VMware\ Fusion/vmnet8/dhcpd.conf
Locate the following line:
####### VMNET DHCP Configuration. End of “DO NOT MODIFY SECTION” #######
In my file, it was at the very bottom. Note that inside the DO NOT MODIFY block that the subnet has a range defined. This is the range of IP addresses that DHCP draws from for dynamic IP assignment.
Select an IP address inside the subdomain, but outside the specified range. For example, 172.16.23.55. Below the DO NOT MODIFY LINE define a host using your HWAddr and your chosen IP Address.
host ubuntu64 { hardware ethernet 00:0c:29:78:56:05; fixed-address 172.16.23.55; }
Finally restart your Mac. When you open your Ubuntu Terminal and use ifconfig you should see the following:
Edit your config/database.yml file and set the following:
development: adapter: mysql2 reconnect: false pool: 5 host: 172.16.23.55 username: rails_user password: some_password database: test_development test: adapter: mysql2 reconnect: false pool: 5 host: 172.16.23.55 username: rails_user password: some_password database: test_test