I have two VM, one is a database mariaDB server(10.0.0.1). How can I allow one VM(10.0.0.2) to connect to this DB?
To allow a VM (10.0.0.2) to connect to a MariaDB server running on another VM (10.0.0.1), you need to follow these steps:
Edit the MariaDB Configuration File:
/etc/mysql/my.cnf
or /etc/my.cnf
.bind-address
. By default, it may be set to 127.0.0.1
, which restricts connections to localhost.0.0.0.0
to allow connections from any IP address, or specify the IP address of the VM (10.0.0.1) if you want to restrict it further.[mysqld]
bind-address = 0.0.0.0
Restart the MariaDB Service:
sudo systemctl restart mariadb
Log in to the MariaDB Server:
mysql -u root -p
Create a User for Remote Access:
%
).CREATE USER 'username'@'10.0.0.2' IDENTIFIED BY 'password';
CREATE USER 'username'@'%' IDENTIFIED BY 'password';
Grant Privileges:
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'10.0.0.2';
%
, replace 10.0.0.2
with %
in the command.Flush Privileges:
FLUSH PRIVILEGES;
Exit MariaDB:
EXIT;
If you have a firewall running on the MariaDB server (10.0.0.1), you need to allow incoming connections on the default MariaDB port (3306).
Using UFW (Uncomplicated Firewall):
sudo ufw allow from 10.0.0.2 to any port 3306
Using iptables:
sudo iptables -A INPUT -p tcp -s 10.0.0.2 --dport 3306 -j ACCEPT
Using firewalld:
sudo firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="10.0.0.2" port protocol="tcp" port="3306" accept'
sudo firewall-cmd --runtime-to-permanent
Now, you can connect to the MariaDB server from the client VM (10.0.0.2) using the following command:
mysql -u username -p -h 10.0.0.1
You will be prompted to enter the password for the user you created.
By following these steps, you should be able to successfully connect from one VM to the MariaDB server running on