Pháttri ển ứng dụng mãngu ồn mở

Bài1: H ệ quảntr ị Cơ sở dữ liệu mãngu ồn mở GV: ĐOÀN THIỆN NGÂN dngan2003@gmail.com – ngan@ueh.edu.vn http://sites.google.com/site/ptudnmk37

ĐoànThi ệnNgân Bài1 -1/40

Mụctiêu • Giớithi ệucác h ệ quảntr ị cơ sở dữ liệu mãngu ồn mở, đặcbi ệtchútr ọng đến MySQL vàPostGreSQL.

• Cài đặtvà s ử dụng hệ quảntr ị cơ sở

dữ liệuMySQL.

• Cài đặtvà s ử dụngph ần mềmgiaoti ếp vàqu ảntr ị MySQL: MySqlWorkbench.

• Cài đặtvà s ử dụng hệ quảntr ị cơ sở

dữ liệuPostGreSQL.

• Cài đặtvà s ử dụngph ần mềmgiaoti ếp vàqu ảntr ị PostGreSQL: pgAdmin3.

ĐoànThi ệnNgân Bài1 -2/40

Nộidung

• Tổngquan. • Hệ quảntr ị cơ sở dữ liệu

MySQL.

• Phần mềmgiaoti ếpvàqu ảntr ị

MySQL Workbench.

• Hệ quảntr ị cơ sở dữ liệu

PostGreSQL.

• Phần mềmgiaoti ếpvàqu ảntr ị

PostGreSQL –pgadminIII.

ĐoànThi ệnNgân Bài1 -3/40

Tổngquan–OSS DBMS

• Hệ quảntr ị cơ sở dữ liệu

(Database Management System -DBMS): – MySQL – PostGreSQL – SqLite, Firebird, mSql, … – Sybase – IBM DB2 Express C – MS SQL Server Express – Oracle Express – …

ĐoànThi ệnNgân Bài1 -4/40

Hệ quảntr ị Cơ sở dữ liệu MySQL

ĐoànThi ệnNgân Bài1 -5/40

Tài liệu tham khảo

1.MySQL Reference Manual. http://dev.mysql.com/doc

2.Beginning PHP and MySQL 5: From Novice to Professional, 4th Edition; W. Jason Gilmore; Apress; 2010. 3.Xem cú pháp, cách s ử dụng MySQL

Manual trước khi bắt đầu.

ĐoànThi ệnNgân Bài1 -6/40

MySQL

• Open source software DBMS • http://www.mysql.com • http://dev.mysql.com • http://dev.mysql.com/doc • Môi trường cài đặt:

– MS Windows – Linux – Unix – Mac OS – …

ĐoànThi ệnNgân Bài1 -7/40

MySQL –MS Windows

• Cài đặtMySQL trênMS Windows (nên

download MySQL Installer)

http://dev.mysql.com/downloads/mysql/ • MySQL Community Server chứa đầy đủ:

– Server – Client – Workbench – …

• Xem MySQL Manual: cú pháp, cách sử

dụng, …

ĐoànThi ệnNgân Bài1 -8/40

Cài đặt MySQL –Linux –FC20

• Download MySQL Yum Repository http://dev.mysql.com/downloads/repo/yum/ • Cài đặt MySQL Yum Repository trên Fedora 20 # yum localinstall

mysql-community-release-fc20-5.noarch.rpm (hay # rpm –Uvh mysql-community-release-fc20-5.noarch.rpm) • Cài đặt MySQL Community Server trên Fedora

20 (yum tự động cài MySql Client và các thư viện kèm theo)

# yum install mysql-community-server • Cấu hình để khởi động MySQL lúc boot hệ thống

# chkconfig --levels 235 mysqld on

ĐoànThi ệnNgân Bài1 -9/40

MySQL –Linux

• Khởi độngMySQL

# service mysqldstart

hay dùngtrình system-config-service • Thử lệnh

$ mysql mysql> show databases; mysql> \h mysql> \q

• Để an toàntr ướckhi s ử dụngMySQL, ch ạy

mysql_secure_installation hay đơngi ản đặt mật khẩuchoroot qua l ệnhsql

$ mysql–u root mysql> set paswordfor

root@localhost=pasword('secret') ;

ĐoànThi ệnNgân Bài1 -10/40

MySQL Admin Tools

• MySQL Workbench

http://wb.mysql.com

• phpMyAdmin

http://www.phpmyadmin.net/

• Webmin

http://webmin.net/

• SQL Maestro

http://www.sqlmaestro.com/

• Navicat

http://www.navicat.com/

ĐoànThi ệnNgân Bài1 -11/40

MySQL Workbench

• Cài đặtMySQL Workbench # yum install mysql-workbench-community • GọiMySQL Workbench

$ mysql-workbench &

hay gọimenu Applications/ MySQL Workbench • ĐọcHelp c ủaMySQL Workbench h ọccách

sử dụng

• MySQL Workbench tương tự Microsoft

SqlServerData Management Studio, nó r ất cầnthi ếtkhi s ử dụngMySQL. ĐoànThi ệnNgân

Bài1 -12/40

MySQL Workbench

ĐoànThi ệnNgân Bài1 -13/40

MySQL Workbench

ĐoànThi ệnNgân Bài1 -14/40

MySQL Workbench

ĐoànThi ệnNgân Bài1 -15/40

MySQL Workbench • MySQL Workbench is a visual database design tool

that is developed by MySQL.

• It is the successor application of the DBDesigner4

project.

• MySQL Workbench is available in three editions.

– The Community Edition is available free of charge (open

source edition).

– The Standard Edition provides additional Enterprise

features, such as database documentation generation, at low cost (paying customers).

– The Enterprise Edition (Commercial)

• MySQL Workbench provides three main areas of

functionality: – SQL Development – Data Modeling – Server Administration

ĐoànThi ệnNgân Bài1 -16/40

MySQL Workbench • SQL Development: create and manage connections

to database servers. As well as allowing you configure connection parameters, MySQL Workbench provides the capability to execute SQL queries on the database connections using the built- in SQL Editor. (Query Browser)

• Data Modeling: create models of your database

schema graphically, reverse and forward engineer between a schema and a live database, and edit all aspects of your database using the comprehensive Table Editor. The Table Editor provides easy-to-use facilities for editing Tables, Columns, Indexes, Triggers, Partitioning, Options, Inserts and Privileges, Routines and Views.

• Server Administration: create and administer server

instances (MySQL Administrator).

ĐoànThi ệnNgân Bài1 -17/40

Cơ sở dữ liệu mẫu sakila

• Download cơ sở dữ liệu mẫu từ MySQL • http://dev.mysql.com/doc/index-other.html • Chọn cơ sở dữ liệusakila, download. • Mở nén, có2 scripts và1 môhình • DùngMySQL Workbench, s ử dụng

Execute SQL file chạy2 scripts – sakila-schema.sql (tạo csdl) – sakila-data.sql(chèn d ữ liệu vào csdl)

ĐoànThi ệnNgân Bài1 -18/40

???

• MySQL làgì?

– Cài đặt – Khởi động

• Phần mềmqu ảntr ị MySQL? • Cách sử dụngMySQL Workbench?

– Tạodatabase – Tạotable m ới – TạoView, …

ĐoànThi ệnNgân Bài1 -19/40

Hệ quảntr ị Cơ sở dữ liệu PostGreSQL

ĐoànThi ệnNgân Bài1 -20/40

Tài liệu tham khảo

1.PostGreSQLManual.

http://www.postgresql.org/docs/

2.PHP and PostgreSQL 8, W. Jason Gilmore

and Robert H. Treat, Apress, 2006

3. Đọc kỹ hướng dẫn cài đặt và sử dụng

trong PostGreSQL Manual

ĐoànThi ệnNgân Bài1 -21/40

General Concepts

• PostgreSQLis an object-relational database management system (ORDBMS) based on POSTGRES, Version 4.21 -University of California at Berkeley Computer Science Department.

• POSTGRES pioneered many concepts that only became available in some commercial database systems much later.

• PostgreSQLis an open-source descendant of this

original Berkeley code. It supports a large part of the SQL standard and offers many modern features: – complex queries – foreign keys – triggers – views – transactional integrity – multiversionconcurrency control

ĐoànThi ệnNgân Bài1 -22/40

PostGreSQL

• http://www.postgresql.org/ • http://www.postgresql.org/docs/ • http://www.postgresql.org/developer/ • Môitr ườngcài đặt:

– MS Windows – Linux – Unix – Mac OS – …

ĐoànThi ệnNgân Bài1 -23/40

PostGreSQL trên MS Windows

• Download www.enterprisedb.com/products-services-training/pgdownload#windows postgresql-9.3.4-4-windows-x64.exe

• Cài đặt PostgreSQL thì mặc định

PgAdminIII được cài theo.

• Chú ý vai trò người quản trị postgres của

Hqtcsdl PostgreSQL

• Chú ý mật khẩu postgres trong quá trình

cài đặt.

ĐoànThi ệnNgân Bài1 -24/40

PostGreSQL • Cài đặtPostGreSQLtrênFedora 20

# yum install postgresql-server postgresql • Khởi tạo nơich ứa cơ sở dữ liệu với user postgres

# su–postgres -bash-4.2 $ initdb–D /var/lib/pgsql/data/

• Khởi độngpostgresql

# service postgresqlstart hay dùngtrình system-config-service

• Thử lệnh gọipostgresql v ớiuser postgres

$ psql–U postgres postgres=# help

• Khởi độngPostGreSQLkhi h ệ thốngboot # chkconfig--levels 235 postgresqlon

ĐoànThi ệnNgân Bài1 -25/40

DBA user postgres

• User postgres: database system

administrator ( ~ satrongMS SQL Server) • Nên tạopassword chopostgres để dễ làm

việc vớivaitròpostgres # passwdpostgres …

• Làmvi ệc vớitínhcáchuser postgres

$ su–postgres

• Gọipostgresqlclient (cpmmand line) v ới vai

trò quản trị postgres $ psql–U postgres

ĐoànThi ệnNgân Bài1 -26/40

postgres& psqlscripting

LệnhdùngchoDBA postgres • createuser

-bash-4.2-postgres $ createusermyname

• dropuser • createdb

$ createdbmyname $ psql-l $ psqlmyname

• dropdb

ĐoànThi ệnNgân Bài1 -27/40

PostGreSQL Admin Tools

• PostGreSQLpgAdmin3

http://www.pgadmin.org/

• phpPgAdmin

http://sourceforge.net/projects/phppgadmin/

• Webmin

http://webmin.net/

• SQL Maestro

http://www.sqlmaestro.com/

• Navicat

http://www.navicat.com/

• Tổngquátwiki http://wiki.postgresql.org/wiki/Community_Guide_to_ PostgreSQL_GUI_Tools

ĐoànThi ệnNgân Bài1 -28/40

PgAdmin3

• Cài đặtpgAdmin 3

# yum install pgadmin3

• GọiPG Admin III $ pgadmin3 &

hay gọimenu Applications/pgAdminIII • ĐọcHelp c ủapgAdmin để dễ sử dụng

ĐoànThi ệnNgân Bài1 -29/40

PgAdmin III

ĐoànThi ệnNgân Bài1 -30/40

PgAdmin III Help

ĐoànThi ệnNgân Bài1 -31/40

Tạo Server

• Đặt pwd user postgres # passwd postgres

• Name • Host • Port (mặc định) • Maintenanca DB:

postgres

• Username: postgres • Password

ĐoànThi ệnNgân Bài1 -32/40

Password

• Cảnh

báo khi lưu mật khẩu

ĐoànThi ệnNgân Bài1 -33/40

Cơ sở dữ liệu mặc định postgres

ĐoànThi ệnNgân Bài1 -34/40

Tạo cơ sở dữ liệuPagila

• Lấy cơ sở dữ liệu mẫu http://pgfoundry.org/projects/dbsamples • Chọn cơ sở dữ liệu pagila • DùngpgAdminIII, t ạo cơ sở dữ liệu mới

pagila(utf-8) – Di chuyển vào csdl pagila – Gọi Query Tool:

pgAdminIII/ Tools/ Qyerytool – Chạyscript pagila-schema.sql – Chạyscript pagila-insert-data.sql

ĐoànThi ệnNgân Bài1 -35/40

Script pagila-schema.sql

ĐoànThi ệnNgân Bài1 -36/40

Tables

ĐoànThi ệnNgân Bài1 -37/40

• Kiểm tra dữ liệu • với

select

ĐoànThi ệnNgân Bài1 -38/40

• Kiểm tra tất cả cơ sở dữ liệu

ĐoànThi ệnNgân Bài1 -39/40

???

• PostGreSQLlàgì?

– Cài đặt – Khởi động – User postgresvàDBA postgres – psql

• Phần mềmqu ảntr ị PostGreSQL? • Cách sử dụngpgAdmin3?

– Tạodatabase – Tạotable m ới – TạoView, … – Sử dụngQuery Tool

ĐoànThi ệnNgân Bài1 -40/40