Week8 - Selesfia/ComputerNetwork GitHub Wiki

AI Tools

  • Perplexity AI -> If you want ask a question and you want to get the answer directly. URL

  • XAnswer -> URL

Create SQL Instance

  1. Go to GCP console and go to SQL
  2. Create new sql instance
  3. Choose SQL edition "Enterprise plus" -> Edition preset "Development" -> Choose database version "8.0"
  4. Give a name "mydb" -> Set password "12345678"
  5. Choose region "asia-east1 (Taiwan)" -> zonal availability "Single zone"
  6. Click create

Change to Private IP Address

  1. Edit your SQL database -> Connection
  2. Disable the public IP and enable the private IP
  3. Save the changes that you made

Connect to the DB Using Cloud Shell

  1. Connect to cloud shell
  2. Go to Cloud SQL Admin API and enable the API
  3. Connect to cloud shell -> mysql -h “10.0.80.3” -u root -p, change the ip to your db private ip -> enter password

Create Simple DataBase and Display it Using PHP

  1. Connect to your db using the above step

  1. Run the following code step by step
show databases;   
create database testdb;   
use testdb;  
create table addrbook(name varchar(50) not null, phone char(10));
insert into addrbook(name, phone) values ("jessline", "(tel:0937711339)");
insert into addrbook(name, phone) values ("celine", "(tel:0971660435)");
select name,phone from addrbook;
  1. Connect to cloud shell
  2. Run the following code
$ sudo apt install apache2 php libapache2-mod-php php-mysql
$ sudo systemctl restart apache2
$ cd /var/www/html
$ sudo vim testdb.php
  1. Paste the following text (Change to your db IP addr, esc :wq to exit and save vim)
<?php
$servername="10.0.80.3";
$username="root";    
$password="12345678";
$dbname="testdb";

$conn = new mysqli($servername, $username, $password, $dbname);

if($conn->connect_error){
    die("connection failed: " . $conn->connect_error);
}
else{
    echo "connect OK!" . "<br>";
}

$sql="select name,phone from addrbook";
$result=$conn->query($sql);

if($result->num_rows>0){, 
    while($row=$result->fetch_assoc()){
        echo "name: " . $row["name"] . "\tphone: " . $row["phone"] . "<br>";
    }
} else {
    echo "0 record";
}
?>
  1. Open the web using external IP Addr and add "/testdb.php"

Create Unmanaged Instance Group

  1. Create 2 VM instance "ww1", "ww2". When creating both vm paste the below text at "Automation"
#! /bin/bash
 apt update
 apt -y install apache2
 cat <<EOF > /var/www/html/index.html
 <html><body><p>Linux startup script added directly. $(hostname -f) </p></body></html>
  1. Go to Instance Group and create new

Create Load Balancing

  1. Go to load balancing and create new
  2. Type of load balancer "Application Load Balancer (HTTP/HTTPS)" -> Public facing or internal "Public facing (external)" -> Global or single region deployment "Best for regional workloads" -> Configure
  3. Give name "mylb" -> choose region "asia-east1 (Taiwan)" -> Network "default"
  4. Frontend configuration -> give a name "myfrontend" -> protocol "HTTP" -> port "80" -> ip addr "ephemeral" -> click done
  5. Backend configuration -> create backend service -> give a name "mybackend" -> Instance group "www-group" -> port "80" -> create new health check "myhealthcheck" -> click create
  6. Check your load balancer ip addr and try to open in a 2 different web

PS : Remember to delete the resources that you created if you don't use it anymore.

29/10/2024

⚠️ **GitHub.com Fallback** ⚠️