By the end of this tutorial, you should be comfortable with accessing and installing MySQL on a remote VM. Furthermore, the VM is interchangeable with any remote server, which means after you SSH in, all of the remaining steps are identical.
- A 32-bit or 64-bit computer with at least 8GB of RAM. 16GB or more is recommended.
- At least 50GB of free disk space.
Now, that we have our Vagrant VM running, we can SSH into it like any other remote server we have SSH access to.
# Print the VM's IP address to the terminal vagrant ssh -c "ip address show eth1 | grep 'inet ' | sed -e 's/^.*inet //' -e 's/\/.*$//'" # SSH in (password is "vagrant") ssh vagrant@MY.VM.IP.ADDRESS
An alternative way to SSH in to the Vagrant VM would be the
vagrant ssh command. But I recommend using the first method because it's closer to how you would log into a remote server in the cloud.
Now we can perform the MySQL installation. Follow any onscreen prompts and enter a new MySQL root password. You will be using this same password to access the DB, later on.
sudo apt-get install mysql-server
You'll want to comment out the
skip-external-locking lines for now. This is only to make testing easier.
# Update the my.cnf and save your settings sudo nano /etc/mysql/my.cnf # Restart mysql sudo service mysql restart
Before deploying to production, you'll want to uncomment the
skip-external-locking lines and add in the IP addresses any authorized servers that need to access the DB.
We're now going to create a new non-root user that will be able to connect to the MySQL database remotely.
# Connect to MySQL and enter your root password mysql -uroot -p # Run MySQL commands CREATE USER 'myuser'@'%' IDENTIFIED BY 'myp4ssw0rd'; GRANT ALL ON *.* TO 'myuser' IDENTIFIED BY 'myp4ssw0rd'; exit
First exit the VM
mysql-client on the host.
sudo apt-get install mysql-client
# Install homebrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" # Install MySQL brew install mysql
And access MySQL.
mysql -hMY.VM.IP.ADDRESS -umyuser -p
If everything went well, you should now be able to connect to your virtual machine's MySQL database from the host machine. If at any point, you want to start over from scratch, run
vagrant destroy from the same directory as the
Vagrantfile, and the virtual machine will be deleted.
- High Performance MySQL: Optimization, Backups, and Replication
- SQL in 10 Minutes, Sams Teach Yourself
- SQL Queries for Mere Mortals: A Hands-On Guide to Data Manipulation in SQL
- Database Design for Mere Mortals: A Hands-On Guide to Relational Database Design (3rd Edition)