How to Install and Connect to MySQL on your Linux VM
A step-by-step guide to installing MySQL on a Linux VM, securing it, and setting up remote access using a database client.

Demola Malomo
Apr 14 2025
3 min read

At some point in your software development lifecycle (SDLC), you’ll need to work with a database, whether that’s in a development environment, a production environment, or both. Databases are a core part of most applications, and it’s important to host them close to your backend service to reduce latency and improve performance. It's also important to access them securely, both during development and after your application goes live.
In this guide, I’ll walk you through everything you need to get MySQL installed and secured on a Linux virtual machine (VM), and also set it up for remote access.
To get MySQL up and running on your Linux VM, follow the steps below:
1. Update VM and Install MySQL
Before anything else, the first thing to do is to update your VM to have the latest security fixes, patches, and features.
1sudo apt update 2sudo apt upgrade -y
Next, install the MySQL server:
1sudo apt install mysql-server -y
2. Secure the MySQL Database
Even on a virtual machine, where the attack surface is relatively smaller, you shouldn't leave your database exposed. It's important to secure your installation from day one and reduce potential vulnerabilities by tightening access and applying security measures. To secure your database, run the command below:
1sudo mysql_secure_installation
This script will prompt you to configure several security-related settings, like setting a root password, removing anonymous users, disallowing root login remotely, and dropping the test database.
3. Create a Database User
Since root login is disabled, you'll need to create a new user with the right permissions to access the database. Depending on your use case, the permissions can be limited to a specific database or granted at a super-user level with access to all available privileges. To do this, follow the steps below:
Step 1: Access MySQL Shell and Log In
1sudo mysql -u root -p
Step 2: Create a New User and Grant Permission
1CREATE USER 'remote_user'@'%' IDENTIFIED BY 'your_secure_password'; 2GRANT ALL PRIVILEGES ON *.* TO 'remote_user'@'%' WITH GRANT OPTION;
Note that the
%
wildcard allows connections from any host. For stronger security, you can replace%
with a specific IP address or a range. You can also adjust the privileges to apply only to a specific database or limit them to certain operations, depending on what the user needs access to.
Step 3: Apply the Changes and Exit
1FLUSH PRIVILEGES; 2EXIT;
4. Allow Remote Access to your Database
By default, MySQL only listens on the local network. That setup works fine if everything happens within the VM, but in most cases, you’ll be connecting through a workbench or a backend service. To allow remote access, follow the steps below:
Step 1: Locate and Open the MySQL Config File
1sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Step 2: Bind Address
Look for the line that says:
1bind-address = 127.0.0.1
Change it to:
1bind-address = 0.0.0.0
Use Ctrl O, hit Enter to save, and Ctrl X to exit.
5. Configure Firewall for MySQL Access
By default, your VM will only allow traffic through ports that you've explicitly opened. Since MySQL uses port 3306
, you'll need to open this port so remote clients can connect to your database over the network.
1sudo ufw allow 3306/tcp 2sudo ufw reload
6. Connect to MySQL Remotely
With your MySQL server configured and the firewall set up, you can now connect to it remotely using any database client of your choice. To do this, do the following:
- Open a database client like MySQL Workbench on your local machine.
- Create a New Connection:
- Connection Name: Give it a friendly name.
- Hostname: Use your Linux VM’s public IP address. You can get it by running the command
hostname -I
. - Port: Ensure the port is set to 3306.
- Username: Enter
remote_user
(or your chosen username). - Password: Click "Store in Vault" or similar, and input your secure password.
- Test the Connection: Click the “Test Connection” button to make sure everything is working.
Wrapping Up
This guide walks you through setting up a MySQL database on a Linux VM. Each step helps get your server up and running while keeping your data secure and accessible when you need it. Whether you're working on a simple project or deploying a production-ready database, following these best practices will help you use MySQL with confidence and efficiency.
If you're new to working with Linux VMs, you might find this beginner’s guide on setting up a Linux virtual machine helpful before diving deeper into server and database management.