IT

How to create your own Database locally on your laptop?

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.

 

How to create your own Database locally on your laptop?

Step-by-Step Guide: Vagrant Database Setup

Follow these steps to establish your Vagrant database environment:

1.Install Vagrant and VirtualBox:

Download and install both Vagrant and VirtualBox from their respective websites.

Please refer this article on how to install vagrant on your laptop: Article

 

2.Set Up the Vagrantfile:

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.

3.Provision the VM:

In your terminal, run vagrant up to start the VM, apply the script, and install MySQL.

4.Access the Database:

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”.

5.Manage the VM:

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.

6. Then at last when the work is done and you no longer need the VM, just destroy the VM.

vagrant halt

vagrant destroy

Conclusion

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.

techswankk.com

View Comments

Recent Posts

The Dark Side of Smartphones: 7 Hidden Dangers Experts Don’t Talk About

You glance at your smartphone about 58 times daily - but did you know this…

4 weeks ago

Do you know these details about Lava Agni 3? Wow powerful factor with two Screens

Get ready, smartphone lovers! Lava International is about to launch its new smartphone, the Lava…

2 months ago

How To Find Out Why Server Is SIow? Top 8 Reasons

You often ask yourself, “Why is the server so slow today?”  Whether it's your application…

3 months ago

Can you Guess the Pixel 9 Pro Fold Price?

The Google Pixel 9 Pro Fold is the latest foldable phone in the Google Pixel…

4 months ago

2024 CrowdStrike outage-Why George Kurtz, CEO of CrowdStrike, Looked Visibly Nervous During a TV Interview

In the high-stakes world of cybersecurity, leaders are often seen as pillars of calm and…

4 months ago

2024 Latest – Know Everything About Qilin Ransomware As A Service (RAAS)

As technology is growing, the number of threats and cyber crimes are also gaining momentum.…

4 months ago