introduction to mysql and phpmyadmin

52
Introduction to MySQL and phpMyAdmin Jian-Long Huang 2013.8.1

Upload: jian-long-huang

Post on 21-Jun-2015

445 views

Category:

Software


1 download

DESCRIPTION

c4Lab 2013 暑期訓練課程

TRANSCRIPT

Page 1: Introduction to MySQL and phpMyAdmin

Introduction to MySQL and phpMyAdmin

Jian-Long Huang2013.8.1

Page 2: Introduction to MySQL and phpMyAdmin

MySQL

Page 3: Introduction to MySQL and phpMyAdmin

3

MySQL

● My Sequel● 開放原始碼 (Open Source)● 關聯式資料庫管理系統 (Relational Database

Management System, RDBMS)● 使用 SQL (Structured Query Language) 管理資料

● 效能高、成本低、可靠性好

● 企業應用 : Google, Facebook, Wikipedia, etc.

Page 4: Introduction to MySQL and phpMyAdmin

4

MySQL 歷史

● 最早在瑞典 MySQL AB 公司開發。

● 2008 年被昇陽微系統 (Sun Microsystems) 收購。

● 2009 年甲骨文公司 (Oracle) 收購昇陽微系統公司, MySQL 成為 Oracle 旗下產品。

Page 5: Introduction to MySQL and phpMyAdmin

5

SQL

● 結構化查詢語言 (Structured Query Language)● 處理關聯式資料庫 ( 讀取、更新、儲存資料 )● 最早誕生於加州聖荷西的 IBM 實驗室 (1970s)● ANSI 標準

Page 6: Introduction to MySQL and phpMyAdmin

6

SQL

http://www.technobiography.com/

Page 7: Introduction to MySQL and phpMyAdmin

7

SQL 指令結構

https://en.wikipedia.org/wiki/SQL

Page 8: Introduction to MySQL and phpMyAdmin

8

SQL 指令種類

● SQL 指令包含三個部份:

– 資料定義語言 (Data Def inition Language, DDL)

– 資料操縱語言 (Data Manipulation Language, DML)

– 資料控制語言 (Data Control Language, DCL)

● 陳述式不分大小寫 ( 但物件名稱有分大小寫 )

● 單行註解 : -- comments● 多行註解 : /* comments */

create database sample_db;Create DATABASE sample_db;CREATE DATABASE sample_db;

Page 9: Introduction to MySQL and phpMyAdmin

9

資料定義語言 (DDL)

● 用來建立、改變或刪除一個資料庫物件。

● CREATE: 建立資料庫、資料表和用戶

● ALTER: 變更資料庫和資料表的結構

● DROP: 刪除資料庫、資料表和用戶

Page 10: Introduction to MySQL and phpMyAdmin

10

資料定義語言 (DDL)

● CREATE

Page 11: Introduction to MySQL and phpMyAdmin

11

資料定義語言 (DDL)

● CREATE

Page 12: Introduction to MySQL and phpMyAdmin

12

資料定義語言 (DDL)

● CREATE

Page 13: Introduction to MySQL and phpMyAdmin

13

資料定義語言 (DDL)

● ALTER

Page 14: Introduction to MySQL and phpMyAdmin

14

資料定義語言 (DDL)

● DROP

Page 15: Introduction to MySQL and phpMyAdmin

15

資料操縱語言 (DML)

● 對資料庫物件進行資料存取。

● SELECT: 讀取資料列

● INSERT: 建立資料列

● UPDATE: 變更資料列

● DELETE: 刪除資料列

Page 16: Introduction to MySQL and phpMyAdmin

16

資料操縱語言 (DML)

● SELECT

Page 17: Introduction to MySQL and phpMyAdmin

17

資料操縱語言 (DML)

● INSERT

Page 18: Introduction to MySQL and phpMyAdmin

18

資料操縱語言 (DML)

● UPDATE

Page 19: Introduction to MySQL and phpMyAdmin

19

資料操縱語言 (DML)

● DELETE

Page 20: Introduction to MySQL and phpMyAdmin

20

資料控制語言 (DCL)

● 對資料存取權進行控制

● GRANT: 給予用戶對資料庫物件的存取權限

● REVOKE: 撤銷用戶對資料庫物件的存取權限

● 可以設定的權限:CONNECT 、 SELECT 、 INSERT 、 UPDATE 、 DELETE 等。

● GRANT 時若用戶不存在,則會建立一個用戶。

Page 21: Introduction to MySQL and phpMyAdmin

21

資料控制語言 (DCL)

● GRANT

Page 22: Introduction to MySQL and phpMyAdmin

22

Advanced Usage

● MySQL Manual (off icial)

Page 23: Introduction to MySQL and phpMyAdmin

23

安裝 MySQL (Windows)

● 到官方網站下載 :http://dev.mysql.com/downloads/mysql/

● 下載完以後,依照軟體提供的步驟安裝即可

● 參考教學 : http://goo.gl/2bjtyn

Page 24: Introduction to MySQL and phpMyAdmin

24

安裝 MySQL (Ubuntu)

● sudo apt-get install mysql-server● 執行後會跳出畫面,需要輸入 root 的密碼,輸入

二次之後就會繼續安裝。

● 安裝完的 MySQL 設定檔在 /etc/mysql/my.cnf● 預設只能從本機存取,如果要改成可以從遠端存

取,修改 my.cnf :bind_address = 0.0.0.0 # 預設是 127.0.0.1

Page 25: Introduction to MySQL and phpMyAdmin

25

進入命令列模式

● Windows: 開始功能表 --> MySQL Server 5.5--> MySQL Command Line Client

● Ubuntu: 輸入 'mysql -u root -p' ,會出現密碼提示,接著輸入安裝時所設定的密碼。

Page 26: Introduction to MySQL and phpMyAdmin

26

命令列模式

mysql> show databases; # 顯示 mysql 所有已建立的資料庫名稱

Page 27: Introduction to MySQL and phpMyAdmin

27

命令列模式

mysql> use transcriptome; # 連到 transcriptome 資料庫

Page 28: Introduction to MySQL and phpMyAdmin

28

命令列模式

mysql> show tables; # 列出資料庫的所有資料表

Page 29: Introduction to MySQL and phpMyAdmin

29

命令列模式

mysql> show columns from transcriptome_homology;# 列出資料表的所有欄位

Page 30: Introduction to MySQL and phpMyAdmin

30

命令列模式

● 除了 MySQL 內建指令以外,也支援 SQL 語法

Page 31: Introduction to MySQL and phpMyAdmin

31

練習

● 新增一筆 PDB (Protien Data Bank) 蛋白質資料

● 主機 :[ip] [:3306]● 資料庫名稱

● 使用者名稱 / 密碼

Page 32: Introduction to MySQL and phpMyAdmin

32

練習

# 連到主機$ ssh [user]@[ip] -p [port]# 連接到資料庫$ mysql -h [ip] -u [username] -wp -pmysql> show databases;mysql> use [dbname];mysql> show tables;mysql> show columns from protein;mysql> insert into `protein` (`molecule`, `type`, `length`, `chains`)values ('ACETYLCHOLINESTERASE', 'protein', 586, 'A');

Page 33: Introduction to MySQL and phpMyAdmin

33

phpMyAdmin

Page 34: Introduction to MySQL and phpMyAdmin

34

phpMyAdmin

● MySQL 的 Web 管理介面,以 PHP 語言實作。

● 透過介面執行繁雜的 SQL 語法。

● 支援多國語系 (65 種語言 ) 。

● 可將資料表的資料匯入和匯出多種格式檔案。

– 匯出: CSV, Excel, LaTex, ODS, PDF, SQL, etc.

– 匯入: CSV, SQL

Page 35: Introduction to MySQL and phpMyAdmin

35

安裝 phpMyAdmin

● 安裝需求:

– Apache

– PHP

– MySQL Server

Page 36: Introduction to MySQL and phpMyAdmin

36

安裝 phpMyAdmin (Windows)

● 到官方網站下載 :http://www.phpmyadmin.net/home_page/downloads.php

● 解壓縮檔案,將 phpMyAdmin-x.y.z-all-languages目錄重新命名為 phpMyAdmin ,放到C:\apache\htdocs\ 目錄下。

● 複製 conf ig.sample.inc.php 成另一份檔案,檔名改成 conf ig.inc.php

Page 37: Introduction to MySQL and phpMyAdmin

37

安裝 phpMyAdmin (Windows)

● conf ig.inc.php也可以用 'http'

建議改成 'mysql'若沒有安裝 mysqli 可以不用改如果找不到 mysqli , phpMyAdmin會自動找 mysql

Page 38: Introduction to MySQL and phpMyAdmin

38

安裝 phpMyAdmin (Windows)

● 輸入網址 : http://127.0.0.1/phpMyAdmin– phpMyAdmin 即資料夾名稱

● 會出現提示輸入 MySQL 使用者名稱和密碼,輸入 root 和當初設定的 root 密碼

● 進入管理頁面,安裝完成。

● 參考教學 : http://www.justin.tw/?p=1130

Page 39: Introduction to MySQL and phpMyAdmin

39

安裝 phpMyAdmin (Ubuntu)

$ sudo apt-get install apache2 php5 mysql-server

$ sudo apt-get install phpmyadmin

● 安裝過程中會提示要選擇設定哪一種 Web server ,選擇 apache2 (按空白鍵 ) ,然後Enter 。

● 提示是否要用 dbconf ig-common 設定phpmyadmin 資料庫,選<Yes>

Page 40: Introduction to MySQL and phpMyAdmin

40

安裝 phpMyAdmin (Ubuntu)

● 提示輸入 database admin 密碼,輸入 MySQL root 密碼

● 提示輸入 MySQL application 密碼,也就是phpmyadmin 使用者密碼,可以留白 (隨機產生密碼 )

● 安裝完成 : http://127.0.0.1/phpmyadmin● 用 root帳號登入管理

Page 41: Introduction to MySQL and phpMyAdmin

41

設定 phpMyAdmin (Ubuntu)

apache 設定檔 :/etc/phpmyadmin/apache.conf

預設是 http://127.0.0.1/phpmyadmin安全起見,可以改成其他字

Page 42: Introduction to MySQL and phpMyAdmin

42

設定 phpMyAdmin (Ubuntu)

● phpmyadmin 設定檔 : ( 用來重載 (override) 原始設定,所有設定都是修改這個檔 ):/etc/phpmyadmin/conf ig.inc.php

● 原始設定檔 : ( 包含所有設定 )/usr/share/phpmyadmin/libraries/conf ig.default.php(DO NOT EDIT THIS FILE!)

Page 43: Introduction to MySQL and phpMyAdmin

43

設定 phpMyAdmin (Ubuntu)

# maximum execution time in seconds (0 for no limit) 設定 SQL 檔匯入執行的逾時時間

$cfg['ExecTimeLimit'] = 300;

Page 44: Introduction to MySQL and phpMyAdmin

44

設定 phpMyAdmin (Ubuntu)

● 修改檔案匯入大小 ( 預設是 2MB)● /etc/php5/apache2/php.ini (PHP環境設定檔 )upload_max_filesize = 2M 上傳檔案大小post_max_size = 8M POST字元最大長度memory_limit = 128M 上傳時使用記憶體限制upload_max_f ilesize ≤ post_max_size ≤ memory_limit

max_execution_time = 30 PHP腳本執行最大時間max_input_time = 60 PHP腳本解析資料最大時間( 從收到所有 input data 時開始算 )

Page 45: Introduction to MySQL and phpMyAdmin

45

Advanced Usage

● phpMyAdmin Wiki

Page 46: Introduction to MySQL and phpMyAdmin

46

And

PHP Runtime Conf iguration:http://tw1.php.net/info.conf iguration.php

Page 47: Introduction to MySQL and phpMyAdmin

47

PHP 連接 MySQL

http://php.net/manual/en/mysqli.get-host-info.php

Page 48: Introduction to MySQL and phpMyAdmin

48

PHP 連接 MySQL

Page 49: Introduction to MySQL and phpMyAdmin

49

Python 連接 MySQL

http://www.hkcode.com/programming/417

Page 50: Introduction to MySQL and phpMyAdmin

50

作業

練習架設 WordPress

Page 51: Introduction to MySQL and phpMyAdmin

51

Wordpress

● 開放原始碼 (Open Source)● 以 PHP 和 MySQL 為平台的部落格 / 內容管理系

統● 豐富的外掛模組

Page 52: Introduction to MySQL and phpMyAdmin

52

安裝 WordPress

● 下載和解壓縮檔案,將 wordpress移動到 /var/www/ 目錄下

● 複製 wp-conf ig-sample.php 為 wp-conf ig.php● 開啟wp-conf ig.php 設定資料庫名稱和使用者帳

密● http://127.0.0.1/wordpress/wp-admin/install.php