Presentation is loading. Please wait.

Presentation is loading. Please wait.

Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Intro to MySQL.

Similar presentations


Presentation on theme: "Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Intro to MySQL."— Presentation transcript:

1 Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Intro to MySQL

2 Open Source Server Side Scripting 2 ECA 236 test MySQL  to test from the command line on Windows platform, change the directory: c:\mysql\bin  or change the PATH  start the MySQL server mysqld  or use the Windows utility, winmysqladmin, to start the MySQL server

3 Open Source Server Side Scripting 3 ECA 236 test MySQL cont …  to test the installation, at the command line type: mysqlshow # returns a list of databases mysqlshow mysql # returns the tables in the database named mysql mysqladmin version status proc # returns current status / information re: MySQL

4 Open Source Server Side Scripting 4 ECA 236 MySQL server  through administration of the MySQL server we can:  create databases  drop databases  create users  set permissions  etc

5 Open Source Server Side Scripting 5 ECA 236 MySQL privileges  MySQL uses a system of privileges to allow users access to do certain things  we do not want all users to have the same level of privileges  regular users should not have the power to create, delete, or change the structure of a database, or even use any other database on the server

6 Open Source Server Side Scripting 6 ECA 236 MySQL configuration  the default configuration of MySQL is not ideal  to make MySQL more secure we will  set a password for the root user  delete the anonymous user

7 Open Source Server Side Scripting 7 ECA 236 root user  MySQL comes with a superuser account named root, which has all the possible privileges to administer the server  by default, root does not have an associated password  without a password, virtually anyone could log on to the server as root and use administrative privileges

8 Open Source Server Side Scripting 8 ECA 236 root user cont …  when signing in at the command line as a particular user we use a flag ( -u ) followed by the name of the user  without a password, anyone can log on as root  remember, the mysql monitor is a way to interact with the MySQL server through a command line interface mysql -u root

9 Open Source Server Side Scripting 9 ECA 236 setting the root password  at the mysql prompt type:  remember, all commands must end with a semicolon  SQL commands, which we will look at shortly, are generally written in upper case to differentiate them from database, table, and column names use mysql; UPDATE user SET password=PASSWORD(‘welcome’) WHERE user=‘root’; FLUSH PRIVILEGES;

10 Open Source Server Side Scripting 10 ECA 236 setting the root password cont …  new versions of MySQL (4.1 and up) have changed the passwords from 16 to 32 bits  incompatible with older versions of PHP  older versions of PHP use 16 bit passwords  use MySQL OLD_PASSWORD( ) function use mysql; UPDATE user SET password=OLD_PASSWORD(‘welcome’) WHERE user=‘root’; FLUSH PRIVILEGES;

11 Open Source Server Side Scripting 11 ECA 236 setting the root password cont …  new versions of MySQL (4.1 and up) have changed the passwords from 16 to 32 bits  use OLD_PASSWORD if you get an error similar to: Client does not support authentication protocol requested by server; consider upgrading MySQL client …

12 Open Source Server Side Scripting 12 ECA 236 setting the root password cont …  the use keyword tells MySQL which database we will be working with  at the time of installation, two databases are created  mysql  contains grant tables, used to store information about privileges  test  a simple test database use mysql;

13 Open Source Server Side Scripting 13 ECA 236 setting the root password cont …  passwords should not be saved as unencrypted text  UPDATE is a SQL command which allows us to modify data in a table  user is a table in the mysql database which stores information about users, including associated passwords UPDATE user SET password=PASSWORD(‘welcome’) WHERE user=‘root’;

14 Open Source Server Side Scripting 14 ECA 236 setting the root password cont …  PASSWORD( ) is a function to encrypt the actual string password  encrypted string is always 16 characters long  PASSWORD( ) uses a one-way encryption algorithm, meaning there is no way to unencrypt it  output of PASSWORD( ) is not arbitrary – the same word will result in the same encrypted string each time  EG, “welcome” will be encrypted as 4326ef3a2d0d0116 UPDATE user SET password=PASSWORD(‘welcome’) WHERE user=‘root’;

15 Open Source Server Side Scripting 15 ECA 236 setting the root password cont …  WHERE limits which records to return  conditional operators such as the equal sign are used to create a condition  here we are testing to make sure the user’s name is root  other conditional operators include > < BETWEEN NOT BETWEEN etc UPDATE user SET password=PASSWORD(‘welcome’) WHERE user=‘root’;

16 Open Source Server Side Scripting 16 ECA 236 setting the root password cont …  after you make certain changes to the grant tables using UPDATE, the changes do not automatically take effect  FLUSH PRIVILEGES lets the server know that changes have taken place  it resets the list of acceptable users and their privileges FLUSH PRIVILEGES;

17 Open Source Server Side Scripting 17 ECA 236 setting the root password cont …  from this point on, tasks that previously did not require the use of a password will not work without one  administrative tasks with mysqladmin  signing on to mysql monitor  include the -u flag followed by the username  include the -p flag to indicate a password is being used mysql -u root -p

18 Open Source Server Side Scripting 18 ECA 236 setting the root password cont …  do not type the actual password after the -p flag, although doing so is possible  MySQL will prompt you for a password, which will be overwritten by asterisks bin>mysql -u root -p Enter password: *******

19 Open Source Server Side Scripting 19 ECA 236 deleting anonymous user  when MySQL is installed, an anonymous user is created  the anonymous user can access the system without supplying a username or password mysql>DELETE FROM user WHERE user=‘’; DELETE command two single quotes user is equal to anonymous FROM user table

20 Open Source Server Side Scripting 20 ECA 236 planning your database  plan your database  what tables will the database contain?  what field will each table contain?  what type of information will each field contain?  we will create a database named sitename which contains:  user ID  last name  first name  email address  password  registration date

21 Open Source Server Side Scripting 21 ECA 236 MySQL naming rules  when naming fields the following rules apply:  you must use names containing  alphanumeric characters  underscore  no spaces  correct:  bob_inventory  incorrect  bob’s inventory

22 Open Source Server Side Scripting 22 ECA 236 data types  when creating the database, MySQL requires that you define a data type for each field  MySQL’s three main categories of data:  Text  Numbers  Date & Times  choosing data type determines what kind of data can be stored, and overall performance of database

23 Open Source Server Side Scripting 23 ECA 236 text data types TypeDescription CHAR (length)0 – 255 characters long VARCHAR (length)0 – 255 characters long TINYTEXTmax length of 255 characters TEXTmax length of 65535 characters MEDIUMTEXTmax length of 16+ million characters LONGTEXTmax length of 4+ billion characters CHAR and VARCHAR can take a (length) attribute, limiting their total size – length is optional for CHAR, required for VARCHAR

24 Open Source Server Side Scripting 24 ECA 236 CHAR vs VARCHAR  both store can be set to a maximum length  CHAR will always be stored as a string the length of your maximum length. Spaces are used to pad any string that is less than the max  VARCHAR will be stored as a string which is only as long as the string itself

25 Open Source Server Side Scripting 25 ECA 236 number data types TypeDescription TINYINT [length]-128 to 127, or 0 to 255 unsigned SMALLINT [length]-32768 to 32767, or 0 to 65535 unsigned MEDIUMINT [length]-8388608 to 8388607, or 0 to 16777215 unsigned INT [length]-2+ billion to 2+ billion, or 0 to 4+ billion unsigned BIGINT [length]very large FLOATa small number with a floating decimal point DOUBLE [length] [decimals]a large number with a floating decimal point DECIMAL [length] [decimals]a DOUBLE stored as a string

26 Open Source Server Side Scripting 26 ECA 236 number data types cont …  many number data types take optional [length]  SIGNED  able to take negative values  UNSIGNED  only positive values and zero  ZEROFILL  extra spaces padded with zeros, automatically unsigned

27 Open Source Server Side Scripting 27 ECA 236 date data types TypeDescription DATEYYYY-MM-DD DATETIMEYYYY-MM-DD HH:MM::SS TIMESTAMPYYYYMMDDHHMMSS ( ends in the year 2037 ) TIMEHH:MM:SS for more information about date data types, visit the MySQL manual, www.mysql.com

28 Open Source Server Side Scripting 28 ECA 236 two special types TypeDescription ENUMa string object that can only have one value, chosen from a list of several thousand values SETallows for several values, up to 64

29 Open Source Server Side Scripting 29 ECA 236 NULL and NOT NULL  NULL means the field has no value, not even an empty string  NULL  no value is required, field can be empty  NOT NULL  value is required, field cannot be empty  default values  use default followed by the default value

30 Open Source Server Side Scripting 30 ECA 236 designing users for sitename  the database sitename will contain one table named users, which contains the following fields users Table Column nameExample user_id13 first_nameJosie last_namePackard emailjosie@packardsmill.com passwordbob registration_date2003-12-30 13:47:29

31 Open Source Server Side Scripting 31 ECA 236 designing users for sitename cont…  identify whether those fields should be a text, number, or date data type users Table Column nameExample user_idnumber first_nametext last_nametext emailtext passwordtext registration_datedate

32 Open Source Server Side Scripting 32 ECA 236 designing users for sitename cont…  choose a subtype for each field users Table Column nameExample user_idMEDIUMINT first_nameVARCHAR last_nameVARCHAR emailVARCHAR passwordCHAR registration_dateDATETIME

33 Open Source Server Side Scripting 33 ECA 236 designing users for sitename cont…  set maximum length and other attributes for each column users Table Column nameExample user_idMEDIUMINT UNSIGNED NOT NULL first_nameVARCHAR(15) NOT NULL last_nameVARCHAR(30) NOT NULL emailVARCHAR(40) passwordCHAR(16) NOT NULL registration_dateDATETIME NOT NULL

34 Open Source Server Side Scripting 34 ECA 236 primary key  to be a PRIMARY KEY a column must meet three conditions:  it must always contain a value ( NOT NULL )  the value must never change  the value must be unique for each record  the PRIMARY KEY for the users table will be the field user_id

35 Open Source Server Side Scripting 35 ECA 236 auto increment  to insure that each record in user_id is unique, use the AUTO_INCREMENT statement  every time a record is added, the value in user_id will automatically be incremented

36 Open Source Server Side Scripting 36 ECA 236 create sitename  start MySQL  sign onto the mysql monitor as the root user  create the sitename database  use the sitename database CREATE DATABASE sitename; USE sitename;

37 Open Source Server Side Scripting 37 ECA 236 creating a table  the CREATE statement is also used to create a table, once we indicate which database to use  syntax to create a table:  after naming the table, name and define each column in order, within parentheses  separate columns/descriptions with a comma CREATE TABLE tablename (column1name description, column2name description, …);

38 Open Source Server Side Scripting 38 ECA 236 create table users  syntax: CREATE TABLE users ( user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, first_name VARCHAR(15) NOT NULL, last_name VARCHAR(30) NOT NULL, email VARCHAR(40), password CHAR(16) NOT NULL, registration_date DATETIME NOT NULL, PRIMARY KEY (user_id) );

39 Open Source Server Side Scripting 39 ECA 236 confirm table creation  confirm the existence of the table:  confirm the creation of the columns: SHOW TABLES; SHOW COLUMNS FROM users; ~~~ or ~~~ DESCRIBE users;

40 Open Source Server Side Scripting 40 ECA 236 backing up a database  mysqldump  will back up tables and their structure  run directly from command line  dump file to the screen: mysqldump -u root -p sitename

41 Open Source Server Side Scripting 41 ECA 236 backing up a database cont …  mysqldump  create an output file  contains SQL commands to create the table  contains data to populate the table mysqldump -u root -p sitename > c:/data/mydump.sql

42 Open Source Server Side Scripting 42 ECA 236 backing up a database cont …  mysqldump  read the file back into MySQL using the syntax:  review MySQL Manual for a long list of options to use with mysqldump mysql -u root -p sitename < c:/data/mydump.sql


Download ppt "Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Intro to MySQL."

Similar presentations


Ads by Google