HHA504 / Assignment 5 / Flask and DBs - MySQL
- Deploy a VM using GCP
- Install MySQL onto a GCP VM
- Configure the VM to allow inbound connections
- Upload public dataset via python to a dummy database within MySQL to allow someone to remotely connect and query this database
- 2 vCPU | 4 GB GAM | 10 GB | Ubuntu 18.04 LTS
- Allow traffic for: HTTP. HTTPS. SSH, MySQL
- Create a new firewall rule to enable MySQL traffic (Open port 3306)
- Name: mysql-allow
- Target Tags: All instances in the network
- Source IP ranges: 0.0.0.0/0
- Protocols and ports: Check [TCP]: 3306
- Go to config file:
-
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
-
- Change bind address and mysqlx-bind-address from 127.0.0.1 to 0.0.0.0/0
- Restart mysql server to activate changes:
-
sudo /etc/init.d/mysql restart
-
- [For Azure VM, add: Inbound security rule Service: MySQL, which auto-adds port 3306, Name it: mysql-custom-allow]
-
sudo apt-get update
-
sudo apt install mysql-client mysql-server # provides many dbs
-
sudo mysql # connecting to server
-
show databases \G; Show what databases exist within the server
-
\q to leave the mysql server
- Create user: (@ = wildcard // % = where DBA can be connected from: anhywhere)
-
Create user 'username'@'%' identified by 'ahi2022';
-
- To get a list of users:
-
select * from mysql.user
-
- To get NEAT list of users:
-
select * from mysql.user \G;
-
- Query to get a list of usernames only:
-
select user, host, password_expired, authentication_string, password_last_changed from mysql.user \G;
-
- Exit mysql server:
-
\q
-
- LOGGING INTO MYSQL SERVER
-
mysql -u alice -p
-
-
enter [password]
- ENTER QUERY:
-
select * from mysql.users;
-
- Fix user permissions error:
-
grant all privileges on *.* TO 'alice'@'%' with grant option;
-
- Confirm privileges are opened to your user:
-
show grants for alice \G;
-
- Log back into mysql server using:
-
mysql -u dba_username -p
-
-
CREATE DATABASE db1;
- Verify db was created by show all databases using:
-
show databases \G;
-
- Change into new db directory to create a table:
-
Use db1
-
- Create a new table within the new db1
-
Create table table_name(column1 datatype, column2 datatype, column3 datatype, ...));
-
Create table salarytable(title varchar(255), salary int, year int);
-
- Confirm table was created
-
show tables;
-
-
print(engine.table_names())
-
Refer to connectDB.py script
- Refer to connectDB.py Data section to push .csv data into a new table (ds_salary_table) within db1
- To review data, enter a query into mysql server on GCP:
-
select * from db1.ds_salary_table limit 5;
-
select * from ds_salary_table where employee_residence = 'US';
-
- Data Analyst Salaries Dataset Retreived from Kaggle: https://www.kaggle.com/code/ericktilieri/data-analyst-salaries-eda/data