I have two VM, one is a database mariaDB server(10.0.0.1). How can I allow one VM(10.0.0.2) or multiple address to connect to this DB?
To allow a VM (10.0.0.2) or multiple addresses to connect to your MariaDB server (10.0.0.1), you need to follow these steps:
Edit the MariaDB Configuration File:
Open the MariaDB configuration file, usually located at /etc/mysql/my.cnf
or /etc/my.cnf
. You may also find it in /etc/mysql/mariadb.conf.d/50-server.cnf
depending on your installation.
sudo nano /etc/mysql/my.cnf
Bind Address:
Look for the line that starts with bind-address
. By default, it may be set to 127.0.0.1
, which restricts connections to localhost. Change it to 0.0.0.0
to allow connections from any IP address, or specify the IP address of the VM you want to allow (10.0.0.2).
bind-address = 0.0.0.0
Save and Exit: Save the changes and exit the text editor.
Log in to MariaDB: Log in to your MariaDB server using the command line.
mysql -u root -p
Create a User:
Create a user that can connect from the specific IP address (10.0.0.2) or from any IP address (%
).
To allow access from a specific IP:
CREATE USER 'username'@'10.0.0.2' IDENTIFIED BY 'password';
To allow access from any IP:
CREATE USER 'username'@'%' IDENTIFIED BY 'password';
Grant Privileges: Grant the necessary privileges to the user for the database you want them to access.
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'10.0.0.2';
Or, if you used %
:
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'%';
Flush Privileges: After creating the user and granting privileges, run the following command to ensure that the changes take effect:
FLUSH PRIVILEGES;
Exit MariaDB:
EXIT;
If you have a firewall running on your MariaDB server, you need to allow traffic on the default MariaDB port (3306).
For UFW (Uncomplicated Firewall):
sudo ufw allow from 10.0.0.2 to any port 3306
Or to allow from any IP:
sudo ufw allow 3306
For Firewalld:
sudo firewall-cmd --zone=public --add-port=3306/tcp --permanent
sudo firewall-cmd --reload
After making these changes, restart the MariaDB service to apply the configuration.
sudo systemctl restart mariadb
From the VM (10.0.0.2), try to connect to the MariaDB server using the credentials you created.
mysql -u username -p -h 10.0.0.1
If everything is set up correctly, you should be able to connect to the MariaDB server from the specified VM.
%
) for broader access.