Lab 7

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

IS222 Lab 7

Logging into the MySql Server and Using the MySQL Database

Objectives:

1. To learn how to log into the SCIMS-based MySql server.

2. To access your student MySQL database.


3. To understand PHPMyAdmin.

Pre‐Lab Preparation:

You should have completed all previous labs.

Special Note:

If there are any changes to the details of database server used for this lab or the initial setup of your
MySQL and Linux Server accounts, these will be indicated under this lab’s link. Check those first before
you proceed with this lab.

What you will be doing?

For this class, we will learn how to use the MySQL database server. We have created database
accounts for all students on the internal network on a genesis-based server. You will be required
to login to your database using putty, which is one of the software used for remote
connection/access, and execute SQL commands. You will also learn to use PHPMyAdmin, a web‐
based GUI tool to help you manage your database as an alternative to working with shell
environment via Putty.

To access the MySQL database you will have to login two times. The first login is on to the given
genesis-based Server. After you have logged in to the server, you can then login to use your
MySQL database (For example, in the labs at USP, you first login using your student ID and email
password into the lab computer, and then you can access your MS Access database). Similarly,
we first need to remotely login to the given genesis server via putty and then access your MySQL
student database.

Page 1
A Note on Your Username and Password

You will require username and password at different places in this manual. Your username will
always be your Student ID number with the letter ‘S’ in uppercase. Your password is the same
as your email password, that is, the password that you use to check your student emails. The
only exception is the MySQL database password, which is by default your surname with the first
letter of your surname in uppercase, e.g. Nusair and not nusair.

Task 1: Logging into the given SCIMS server:

Task 1: Getting putty

In this lab, you will use the software, named putty, to connect to your MySQL database.

Please download the software and save it in your personal share, or flash drive, etc. You don’t want to
be downloading the software every time from Moodle!

For more information about putty, see http://www.putty.nl/. Locate the Download tab on the link and
download Putty for Windows on Intel x86.

A copy of Putty is also available on Moodle for this lab where the link for this lab is. You may use that as
well.

Task 2: Starting putty

1. Open putty (double‐click on the software).


2. Enter the host name in the Host Name (or IP Address) given for the genesis-based server in the
textbox.

Note:

a) In the screenshot, we are using the Host Name as genesis.usp.ac.fj. If a different server is
used, this will be specified in Moodle in the description just below this lab’s link. Check on
Moodle before specifying the server address in Putty.

b) In the screenshot example below, you can also click on the earlier saved SCIMS session from
the Saved Sessions, and click on Load. This can only be done if the session has been saved
earlier. It is unlikely that this will be the case for you.

3. For the Port, specify 22 and for the protocol, SSH.

Page 2
4. Click Open

5. This will open a SSH connection to the SCIMS server. See below:

6. If you see a security alert, click Yes.

7. You will now see a window where you can enter your username. Follow the instructions in Task 3
regarding this.
Page 3
Task 3: Connecting to the given Server

1. You will now see a window where you can enter your username. Your username is your student
id, e.g. Sxxxxxxxx where X represents your number. Please ensure that the letter ‘S’ is in
uppercase.
2. Type in your ID number and Press Enter
3. Type in your password. Your password is your email password. (Same as the password you use
to check your Student emails). If this is otherwise, it will be specified on Moodle just beneath this
lab’s link.

Note: You will not see anything when you are typing your password! Just type it and press
enter.

4. If your login is successful, you should see a screen similar to the one below with your ID instead.

5. Now go to the next task.

Task 4: Accessing MySQL Database

Note:
Each student has a database. The name of the database is your student ID number. You cannot
create another database, but you can create tables within your database and execute certain
queries.

1. To connect and access your database type in this command and press Enter:
mysql –u Sxxxxxxxxx –p password

Replace Sxxxxxxxx with your Student ID (your username) using a capital S in your id. Your
password is your surname with the first letter in capital. For e.g., if your surname is nusair, the
first letter is a capital N.

Important Note: If you specify a space between –p and password, it will request the password
again, which won’t be visible when you type just like the previous login.

Page 4
2. If your command was correctly typed in and the password was correct, you will see the mysql
prompt. (see the sample screenshot below) You can now issue SQL commands to make table, insert
data into those table and so on. We will practice a few in the next section.

Task 5: Basic SQL Commands

1. To change database to your own database type in: mysql> use database_name;

For example, if I wanted to use my database I would type: use nusair_s;

Your database name is your Student ID with letter ‘S’ in


uppercase.

2. To see all the Tables in your database, type mysql> show tables;

You will see Empty set (0.00 sec) because there are no tables and it took 0.00 seconds to execute the
query.

3. See the MySQL server version and the current date: mysql> select version (), CURRENT_DATE;

Note: Keywords may be entered in any letter case. However it’s


a good programing practice to use upper case .The following
queries are equivalent:
mysql> SELECT VERSION(), CURRENT_DATE;
mysql> select version(), current_date;
mysql> SeLeCt vErSiOn(), current_DATE;
4. To see which account the server authenticated you as, invoke the CURRENT_USER() function:
SELECT CURRENT_USER();

Page 5
Task 6: Changing MySQL Password

1. Your default password is your surname with the first letter of your surname in uppercase, e.g.
Nusair.
2. To change your password, type in the following command:

SET PASSWORD FOR 'SXXXXXXXX'@'localhost'=PASSWORD('????');


Note:
1. Replace SXXXXXXXX with your student ID

2. Note the use of use the correct apostrophe symbol (')

3. Replace???? With your new password. PLEASE REMEMBER YOUR NEW PASSWORD

Note:
1. You will use the new password to login into the MySQL database server.
2. You have only changed the password of the MySQL database server. To login into the genesis
server (your first login) use your email password.
3. You DON’T have to change the password every time. Do it only when you want to change your
password.

Task 7 DDL SQL Statements

1. We will now write the SQL statement to create a table called pet. Copy the query in the given listing
and paste at the MySQL prompt.

CREATE TABLE pet(


pet_id CHAR(3),
pet_name VARCHAR(20),
pet_owner VARCHAR(20),
pet_species VARCHAR(20),
pet_sex CHAR(1),
pet_birth DATE, pet_death
DATE, CONSTRAINT
pet_pk_petid PRIMARY
KEY(pet_id),
INDEX pet_index_petname
(pet_name)
)ENGINE=InnoDB;
Page 6
2. MySQL prompt doesn’t provide a really nice environment for writing SQL statements. When you move
to a new line while writing a SQL statement, you will not be able to go to the previous line for
editing. It is therefore recommended that you write all SQL statements in Notepad or Notepad++
first. From notepad, you will copy the SQL statement into the MySQL prompt.

Note: It is recommended to use Notepad


Typically in Linux, you copy the text from Windows and use right click in the shell to paste.

3. Select the CREATE TABLE definition given above.

4. To paste the above query in the MySQL prompt, right‐click at the putty prompt.
5. If the query was successfully executed, you will see a successful notification message. If the query
did note execute then press Enter once. A sample output is below

6. To verify that this table has been created, type SHOW TABLES;

7. To see the structure of the newly created table, type

DESCRIBE pet; OR DESC pet;

A sample output is given below.

Page 7
Task 8 DML SQL Statements

1. With the MySQL prompt still open, we will enter the insert SQL statement to add a row of data to
our pet table.
2. Copy the SQL statement from the given listing and paste in the MySQL prompt and execute it.

INSERT INTO pet VALUES ('P01', 'kitty', 'Ms Smith', 'Ocelot','f', '1999-01-31',NULL);

Note: Date values are entered in the format ‘yyyy‐mm‐dd’ and within apostrophes.

A sample output is given below.

3. To select all the data from pet table, issue the following select statement

SELECT * FROM pet;

A sample output is given below.

Page 8
Task 9: Exit MySQL Database and the given SCIMS Server

1. Type Exit and press enter to exit from the MySQL database
server.

exit;

A sample output is given below.

2. To exit the SCIMS server, type exit and press Enter again.

NOTE: This will close putty.


Summary

1. You have two accounts on genesis Server.


a. First account is used to login on to the given genesis server itself. Username is your student ID,
and password is your email password.
b. Second account is used to access your MySQL database. Username is your student ID, and
password is your surname (with first letter in uppercase). If you have changed your password in
the one of the earlier tasks, you will now use the new password.

2. Accessing the genesis Server and Database is in two steps:

a. You always login on to the given genesis Server first.


b. Then login into the MySQL database. You need to see the
MySQL prompt in order to execute the MySQL commands.

Page 9
3. You can use arrow keys on the keyboard to recall your previous commands

Task 10: Using PHPMyAdmin

At a minimum you should be able to log in to your MySql account using PhpMyAdmin while doing
this lab. However, you can look at all other features of PhPMyAdmin in your own time or in the next
lab.

To access phpMyadmin:

1. Type in the following URL in any browser and press Enter:


https://genesis.usp.ac.fj/phpmyadminscims/

2. Enter your MySQL database username and password. Your username is your Student ID, and default
password was your surname (with first letter in uppercase). These are the same as when logging
through the shell earlier.

NOTE: If you had changed your database password in one of the earlier tasks above, then use your
new database password. Otherwise, use your surname (with first letter in uppercase).

3. Click on GO.

4. Look up the Introduction to phpMyAdmin link on Moodle (under the link for this lab) to
examine all other tasks/features of PhPMyAdmin in your own time for your database.
Page 10

You might also like