When setting up MySQL server in a VirtualBox VM, developers often encounter the frustrating error message:
Host '10.0.2.2' is not allowed to connect to this MySQL server
This occurs despite having proper port forwarding configured in VirtualBox and MySQL's bind-address
set to 0.0.0.0
.
VirtualBox uses specific IP addresses by default:
- 10.0.2.2 - Special address for the host machine (your physical computer)
- 10.0.2.15 - Typical address for the guest VM
When your host machine tries to connect to the MySQL server in the VM, it's coming from 10.0.2.2, which MySQL doesn't automatically trust.
Here's how to properly configure access:
-- First, log into MySQL from within the VM
mysql -u root -p
-- Create or update a user that can connect from any host
CREATE USER 'username'@'%' IDENTIFIED BY 'password';
-- Grant all privileges (or specific ones as needed)
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' WITH GRANT OPTION;
-- Alternative for root access if needed
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'yourpassword';
-- Don't forget to flush privileges
FLUSH PRIVILEGES;
Ensure your my.cnf
contains:
[mysqld]
skip-networking = 0
skip-external-locking
bind-address = 0.0.0.0
Set up port forwarding in VirtualBox:
VBoxManage modifyvm "VM Name" --natpf1 "mysql,tcp,,3306,,3306"
Or use the VirtualBox GUI:
- Go to VM Settings > Network
- Select NAT and click Port Forwarding
- Add rule: Name=MySQL, Protocol=TCP, Host IP=blank, Host Port=3306, Guest IP=blank, Guest Port=3306
If you're still having issues:
-- Check active MySQL users and their hosts
SELECT user, host FROM mysql.user;
-- Verify connection privileges
SHOW GRANTS FOR 'username'@'%';
-- Check if MySQL is actually listening
netstat -tulnp | grep mysql
While %
wildcard host is convenient for development, consider:
- Restricting to specific IPs in production
- Using SSH tunneling instead of direct MySQL connections
- Setting up proper firewalls on both host and guest
When setting up MySQL server inside a VirtualBox VM with the host machine as client, developers often hit this roadblock:
Host '10.0.2.2' is not allowed to connect to this MySQL server
Before diving into solutions, verify these essentials first:
# In my.cnf/my.ini
skip-external-locking
bind-address = 0.0.0.0
Port forwarding should be properly configured in VirtualBox:
VBoxManage modifyvm "VM_NAME" --natpf1 "mysql,tcp,,3306,,3306"
The key solution lies in MySQL user permissions. Here's how to grant remote access:
-- Inside MySQL shell
CREATE USER 'remote_user'@'%' IDENTIFIED BY 'secure_password';
GRANT ALL PRIVILEGES ON *.* TO 'remote_user'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
If the issue persists, check these additional configurations:
# Verify MySQL listening ports
netstat -tulnp | grep mysql
# Check firewall rules (iptables/ufw)
sudo iptables -L -n | grep 3306
# Test connectivity from host
telnet 10.0.2.2 3306
For NAT network configurations, consider these alternatives:
1. Bridged Adapter mode
2. Host-Only Network + NAT
3. Port forwarding with specific IP:
VBoxManage modifyvm "VM_NAME" --natpf1 "mysql,tcp,127.0.0.1,3306,,3306"
While opening access, maintain security:
-- Restrict access to specific IP
GRANT ALL ON *.* TO 'user'@'192.168.1.%';
-- Or use SSH tunneling for secure access
ssh -L 3306:localhost:3306 user@vm_ip