I created two VM, one is a php application and one is a mariaDB server. How to connect to the db server from application server?
To connect your PHP application running on one VM to a MariaDB server running on another VM, you need to follow these steps:
Check IP Addresses: Make sure you know the IP address of the MariaDB server VM. You can usually find this in your cloud provider's dashboard or by running ifconfig
or ip a
on the MariaDB server.
Ping the Database Server: From the PHP application VM, try to ping the MariaDB server to ensure that the two VMs can communicate:
ping <MariaDB_Server_IP>
Edit the MariaDB Configuration: Open the MariaDB configuration file, usually located at /etc/mysql/mariadb.conf.d/50-server.cnf
or /etc/my.cnf
, and look for the line that starts with bind-address
. Change it to:
bind-address = 0.0.0.0
This allows MariaDB to accept connections from any IP address.
Restart MariaDB: After making changes, restart the MariaDB service:
sudo systemctl restart mariadb
Log into MariaDB: Access the MariaDB server using the command line:
mysql -u root -p
Create a User: Create a user that can connect from the PHP application server's IP address. Replace <username>
, <password>
, and <PHP_App_Server_IP>
with your desired username, password, and the IP address of the PHP application server:
CREATE USER '<username>'@'<PHP_App_Server_IP>' IDENTIFIED BY '<password>';
Grant Privileges: Grant the necessary privileges to the user for the database you want to access. Replace <database_name>
with the name of your database:
GRANT ALL PRIVILEGES ON <database_name>.* TO '<username>'@'<PHP_App_Server_IP>';
Flush Privileges: Make sure to flush the privileges to apply the changes:
FLUSH PRIVILEGES;
Exit MariaDB:
EXIT;
Install PHP MySQL Extension: Ensure that the PHP MySQL extension is installed on your PHP application server. You can install it using:
sudo apt-get install php-mysql
or for other distributions, use the appropriate package manager.
Create a Connection Script: In your PHP application, create a script to connect to the MariaDB server. Here’s a simple example:
<?php
$servername = "<MariaDB_Server_IP>";
$username = "<username>";
$password = "<password>";
$dbname = "<database_name>";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Test the Connection: Run the PHP script in your web browser or command line to see if it connects successfully.
If you have a firewall enabled on either VM, ensure that the port used by MariaDB (default is 3306) is open for incoming connections from the PHP application server's IP address.
For example, if you are using ufw
on the MariaDB server, you can allow traffic on port 3306 with:
sudo ufw allow from <PHP_App_Server_IP> to any port 3306
After following these steps, your PHP application should be able to connect to the MariaDB server. Make sure to handle any errors and secure your database connections appropriately in a production