DEV Community

Cover image for Setting up MySQL on Amazon EC2 using Homebrew
Rikin Patel
Rikin Patel

Posted on • Updated on

Setting up MySQL on Amazon EC2 using Homebrew

In the dynamic realm of cloud computing, Amazon EC2 provides a versatile environment for hosting applications and databases. This guide walks you through the process of setting up MySQL on an Amazon EC2 instance using Homebrew, a popular package manager for macOS and Linux.

Introduction

In this tutorial, we'll explore the steps to install and configure MySQL on Amazon EC2 using Homebrew. By the end, you'll have a fully functional MySQL infrastructure ready to support your projects.

Installation

1. Install Git

Begin by installing Git to fetch Homebrew installation bash scripts. Execute the following command:

sudo dnf install git
Enter fullscreen mode Exit fullscreen mode

2. Install Homebrew

Proceed to install Homebrew with the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Enter fullscreen mode Exit fullscreen mode

3. Add Homebrew to Your PATH

Add Homebrew to your PATH and bash shell rcfile. If you're using bash, modify ~/.bashrc as follows:

test -d ~/.linuxbrew && eval "$(~/.linuxbrew/bin/brew shellenv)"

test -d /home/linuxbrew/.linuxbrew && eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

echo "eval \"\$($(brew --prefix)/bin/brew shellenv)\"" >> ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

MySQL Installation

1. Install MySQL

Use Homebrew to install MySQL:

brew install mysql
Enter fullscreen mode Exit fullscreen mode

2. Start MySQL Service

Initiate the MySQL service:

brew services start mysql
Enter fullscreen mode Exit fullscreen mode

3. Secure MySQL Installation

Secure your MySQL installation with:

mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

Follow the prompts to set a root password, remove anonymous users, disallow root login remotely, and remove the test database.

4. Set up users from root user

Create MySQL users from the root user. For example:

mysql -u root -p
Enter fullscreen mode Exit fullscreen mode
CREATE USER 'user_name'@'localhost' IDENTIFIED WITH mysql_native_password BY 'user_password';
GRANT ALL PRIVILEGES ON *.* TO 'user_name'@'localhost';
Enter fullscreen mode Exit fullscreen mode

5. Connection to remote client: Example: MySQL Workbench

To connect remotely, use MySQL Workbench. Here's an example of connection credentials:

Image description

Safe Practices

When setting up MySQL on Amazon EC2, it's essential to follow these safe practices:

  • Secure Installation: Set a strong root password during the installation process to secure your MySQL installation.
  • Regular Updates: Keep both your system and MySQL software up-to-date to patch potential vulnerabilities.
  • Principle of Least Privilege: When creating MySQL users, grant only the necessary permissions for specific databases and tasks.
  • Restrict Remote Access: Enhance overall security by restricting remote access to MySQL to trusted IP addresses.
  • Encryption: Implement encryption for data in transit and at rest to safeguard sensitive information.
  • Built-in Security Features: Enable MySQL's built-in security features, such as firewalls and authentication mechanisms, for an extra layer of protection.
  • Regular Backups: Maintain a regular backup schedule to ensure data recoverability in case of accidental deletion or system failures.
  • Monitoring: Monitor MySQL logs and performance metrics to identify and address suspicious activities promptly.
  • Network Security: Implement network security best practices, including using Virtual Private Clouds (VPCs) and Network Access Control Lists (NACLs), to control traffic flow.
  • Education and Audits: Educate your team on security best practices and conduct regular security audits to identify and mitigate potential risks.

Conclusion

Congratulations! You've successfully set up a MySQL infrastructure on Amazon EC2 using Homebrew. Your environment is now primed for creating databases to fuel your projects.

Top comments (0)