Looking to develop locally with a database? Vagrant offers a powerful solution for creating isolated development environments. This guide walks you through on How to create your own Database locally?
Vagrant simplifies the process of creating and managing virtual machines (VMs) for development. This allows you to set up a dedicated environment with your chosen database, complete with sample data, for isolated testing and development.
Follow these steps to establish your Vagrant database environment:
Download and install both Vagrant and VirtualBox from their respective websites.
Please refer this article on how to install vagrant on your laptop: Article
Create a project directory using your terminal (mkdir myVM).
Initialize a Vagrant project using vagrant init <boxname>
A Vagrant file will be created automatically.
Edit the Vagrantfile to configure the VM and database installation.
Here’s an example Vagrantfile for a MySQL database on Ubuntu:
Vagrant.configure(“2”) do |config|
# Use the Ubuntu 20.04 box
config.vm.box = “ubuntu/focal64”
# Configure the VM’s network
config.vm.network “private_network”, type: “dhcp”
# Provision the VM
config.vm.provision “shell”, inline: <<-SHELL
# Update package list and install MySQL
sudo apt-get update
sudo apt-get install -y mysql-server
# Configure MySQL (change ‘yourpassword’ to a secure password)
sudo mysql -e “ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘yourpassword’;”
sudo mysql -e “CREATE DATABASE mydatabase;”
sudo mysql -e “USE mydatabase; CREATE TABLE mytable (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100));”
sudo mysql -e “USE mydatabase; INSERT INTO mytable (name) VALUES (‘Sample Data’);”
SHELL
# Forward MySQL port
config.vm.network “forwarded_port”, guest: 3306, host: 3306
end
Note: To find the box name of the VM, just google it like “ubuntu vagrant box” and the get the box name.
In your terminal, run vagrant up to start the VM, apply the script, and install MySQL.
SSH into the VM using vagrant ssh.
Access MySQL with mysql -u root -p and enter your password (set in the Vagrantfile).
Verify the data using USE mydatabase; SELECT * FROM mytable;. You should see “Sample Data”.
Use vagrant halt to stop the VM and vagrant destroy to remove it (if not needed).
To re-provision after Vagrantfile changes, run vagrant reload –provision.
vagrant halt
vagrant destroy
By following these steps, you’ll have a Vagrant VM with MySQL set up and pre-populated with sample data. Remember to modify the provisioning script in the Vagrantfile to match your specific database and data needs.
You glance at your smartphone about 58 times daily - but did you know this…
Get ready, smartphone lovers! Lava International is about to launch its new smartphone, the Lava…
You often ask yourself, “Why is the server so slow today?” Whether it's your application…
The Google Pixel 9 Pro Fold is the latest foldable phone in the Google Pixel…
In the high-stakes world of cybersecurity, leaders are often seen as pillars of calm and…
As technology is growing, the number of threats and cyber crimes are also gaining momentum.…
View Comments