2024 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

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.

1 thought on “2024 How to create your own Database locally on your laptop?”

Leave a comment