Setting Up MySQL on an Azure VM
Requirements
The Nautilus DevOps team is tasked with integrating a PHP application hosted on an Azure VM with a MySQL database hosted on another Azure VM. This will validate the application's ability to connect to the database in the cloud.
- Create the MySQL VM:
- Create a VM named
nautilus-mysql-vmusing the MySQL Jetware image from the Azure Marketplace. - Configure the VM in the East US region.
- Use
Passwordas the authentication type. - Set the username as
nautilus_adminand the password asNamin@123456. - Allow inbound traffic on port
3306to enable MySQL access.
- Setup the MySQL Database:
- SSH into the
nautilus-mysql-vm. - Use the
sudo /jet/enter mysqlcommand to access the MySQL shell. - Create a database named
nautilus_db. - Create a MySQL user named
nautilus_userwith passwordpassword123. - Grant all privileges on the
nautilus_dbdatabase to this user.
- PHP VM Setup:
- A VM named
nautilus-php-vmalready exists in the East US region. - This VM is hosting a PHP application and contains a pre-existing
db_test.phpfile in the/var/www/html/directory.
- Database Connection Configuration:
- Retrieve the public IP address of the
nautilus-mysql-vm. - Update the database connection settings in the
db_test.phpfile to use the MySQL credentials and public IP address of thenautilus-mysql-vm.
- Validation:
- Access the
db_test.phpfile from thenautilus-php-vmusing its public IP address. - Ensure the file displays the message
Connected successfully, confirming the connection between the PHP application and the MySQL database.
Note
Use one database/user naming set consistently so validation and troubleshooting are straightforward.
Prerequisites
- Azure CLI installed and logged in.
- Existing resource group and
nautilus-php-vm. - SSH access from client host to both VMs.
Steps
RG=$(az group list --query "[?contains(name, 'kml')].name" --output tsv)
MYSQL_VM="nautilus-mysql-vm"
PHP_VM="nautilus-php-vm"
LOCATION="eastus"
ADMIN_USER="nautilus_admin"
ADMIN_PASS='Namin@123456'
IMAGE='jetware-srl:mysql:mysql57-ubuntu-1604:1.0.170503'
DB_NAME='nautilus_db'
DB_USER='nautilus_user'
DB_PASS='password123'
az vm create \
--resource-group "$RG" \
--name "$MYSQL_VM" \
--image "$IMAGE" \
--admin-username "$ADMIN_USER" \
--admin-password "$ADMIN_PASS" \
--authentication-type password \
--location "$LOCATION"
az vm open-port --resource-group "$RG" --name "$MYSQL_VM" --port 3306
MYSQL_IP=$(az vm show --show-details --resource-group "$RG" --name "$MYSQL_VM" --query publicIps -o tsv)
PHP_IP=$(az vm show --show-details --resource-group "$RG" --name "$PHP_VM" --query publicIps -o tsv)
echo "SSH to MySQL VM: ssh ${ADMIN_USER}@${MYSQL_IP}"
echo "Then run: sudo /jet/enter mysql"
echo "Create DB/user and grants exactly as required."
echo "SSH to PHP VM: ssh ${ADMIN_USER}@${PHP_IP}"
echo "Update /var/www/html/db_test.php with MYSQL_IP=${MYSQL_IP}, DB=${DB_NAME}, USER=${DB_USER}."
Verification
- Verify MySQL VM is running and has public IP:
az vm show -d --resource-group "$RG" --name "$MYSQL_VM" --query "{powerState:powerState,publicIps:publicIps}"
- Verify port 3306 rule exists:
az network nsg rule list --resource-group "$RG" --query "[?contains(name, '3306')]"
- Verify app connectivity by opening:
http://<php-vm-public-ip>/db_test.php
- Expected output:
Connected successfully