Embed
Email

Mysql

Document Sample
Mysql
Description

Mysql

Shared by: anusorn kuanha
Stats
views:
32
posted:
11/7/2011
language:
Thai
pages:
5
1





การใช้งาน MySQL เบื้องต้น

MySQL เป็นโปรแกรมระบบจัดการฐานข้อมูล (Database Management System) หรือที่เรียกว่า ดีบีเอ็มเอส

( DBMS) โดยใช้ภาษา SQL MySQL ได้รับความนิยมเป็นอย่างมากเนื่องจากทำางานได้เร็ว โดยเฉพาะการทำางานร่วมกับ

Apache และ PHP

จากการที่เราได้ติดตั้ง LAMP server ซึ่งเท่ากับว่าเราได้ติดตั้ง MySQL ไปแล้ว และได้เซ็ตรหัสผ่านให้กับ ผู้ใช้งาน

ที่ชื่อ root ไปแล้วในขั้นตอนติดตั้ง ซึ่งผู้ใช้ root เป็นผู้มีสิทธิสูงสุดในฐานข้อมูล ในการใช้งาน MySQL ร่ามกับ Apache



และ PHP เราไม่จำาเป็นต้องรู้ MySQL อย่างลึกซึ้งก็ได้ แค่สร้างฐานข้อมูลได้ก็สามารถติดตั้งโปรแกรมต่างๆ ทีพัฒนาด้วย

ภาษา PHP ได้แล้วเช่น Drupal, PHP Nuke, Joomla ฯลฯ เพราะโปรแกรมเหล่านี้จะมีระบบการนำาเข้าฐานข้อมูลให้อยู่แล้ว

เราเพียงสร้างฐานข้อมูลเปล่าให้กับระบบเท่านั้นก็พอ ยิ่งกว่านั้นถ้าได้ติดตั้ง phpMyAdmin ก็ยิ่งสะดวกเข้าไปใหญ่ แต่การ

ใช้งานคำาสั่งต่างๆ ของ MySQL ส่วนตัวผู้เขียนเองคิดว่ายังมีความจำาเป็นอยู่



การใช้งานฐานข้อมูล MySQL เบื้องต้น

1. เข้าใช้งานฐานข้อมูล



root@ubuntu­server:~# mysql ­u root ­p 

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g. 

Your MySQL connection id is 7 

Server version: 5.0.51a­3ubuntu5.1 (Ubuntu) 





Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 





mysql> 







2. สร้างฐานข้อมูล

mysql> CREATE DATABASE my_db_name; 

Query OK, 1 row affected (0.00 sec)





mysql>



หรือเพิ่มออฟชันต่างๆ ตามต้องการ

mysql> CREATE DATABASE my_db_name2 CHARACTER SET TIS620; 

Query OK, 1 row affected (0.00 sec)





mysql>









Ubuntu Server 8.04 : MySQL เบื้องต้น sothorn@gmail.com

2



3. แสดงชื่อฐานข้อมูลที่มีอยู่

mysql> show databases; 

+­­­­­­­­­­­­­­­­­­­­+ 

| Database           | 

+­­­­­­­­­­­­­­­­­­­­+ 

| information_schema | 

| my_db_name         | 

| my_db_name2        | 

| mysql              | 

+­­­­­­­­­­­­­­­­­­­­+ 

4 rows in set (0.00 sec) 







mysql>



4. เข้าใช้งานฐานข้อมูลที่ต้องการ

mysql> use mysql; 

Reading table information for completion of table and column names 

You can turn off this feature to get a quicker startup with ­A 





Database changed 





mysql> 









Ubuntu Server 8.04 : MySQL เบื้องต้น sothorn@gmail.com

3



5. ดูว่ามีตารางอะไรบ้างในฐานข้อมูลที่เลือก

mysql> show tables; 

+­­­­­­­­­­­­­­­­­­­­­­­­­­­+ 

| Tables_in_mysql           | 

+­­­­­­­­­­­­­­­­­­­­­­­­­­­+ 

| columns_priv              | 

| db                        | 

| func                      | 

| help_category             | 

| help_keyword              | 

| help_relation             | 

| help_topic                | 

| host                      | 

| proc                      | 

| procs_priv                | 

| tables_priv               | 

| time_zone                 | 

| time_zone_leap_second     | 

| time_zone_name            | 

| time_zone_transition      | 

| time_zone_transition_type | 

| user                      | 

+­­­­­­­­­­­­­­­­­­­­­­­­­­­+ 

17 rows in set (0.00 sec) 





mysql> 







6. ต้องการดูโครงสร้างของตาราง user

mysql> describe user;



...



7. ต้องการดูข้อมูลในตาราง user

mysql> select * from user; 



...









Ubuntu Server 8.04 : MySQL เบื้องต้น sothorn@gmail.com

4



ฐานข้อมูลที่ชื่อ mysql เป็นฐานข้อมูลระบบของ MySQL เช่นตาราง user ก็จะเก็บข้อมูลของผู้ใช้งาน

ที่มีสิทธิเข้าใช้งานฐานข้อมูลได้ เพราะฉะนั้นถ้าเราจะเปลี่ยนรหัสผ่านของผู้ใช้ที่ชื่อ root เราก็สามารถทำาได้



การเปลี่ยนรหัสผ่านให้กับผู้ใช้งาน root

การเปลี่ยนรหัสผ่านให้กับผู้ใช้ root สามารถทำาได้โดยใช้คำาสั่งดังนี้

root@ubuntu­server:~# mysql ­u root ­p 

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g. 

Your MySQL connection id is 8 

Server version: 5.0.51a­3ubuntu5.1 (Ubuntu) 





Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 





mysql> use mysql ;

Reading table information for completion of table and column names 

You can turn off this feature to get a quicker startup with ­A 





Database changed 

mysql> UPDATE mysql.user SET Password = PASSWORD('newpassword') WHERE User = 

'root'; 

Query OK, 3 rows affected (0.00 sec) 

Rows matched: 3  Changed: 3  Warnings: 0 





mysql> flush privileges; 

Query OK, 0 rows affected (0.00 sec) 





mysql> 



โดยที่ newpassword เป็นรหัสผ่านใหม่ที่ท่านต้องการ เมื่อเข้าใช้งานฐานข้อมูลใหม่อีกครั้งก็ต้องใช้รหัส

ผ่านใหม่

root@ubuntu­server:~# mysql ­u root ­p 

Enter password: ระบุ รหัสผ่านใหม่









Ubuntu Server 8.04 : MySQL เบื้องต้น sothorn@gmail.com

5



การสำารองฐานข้อมูล และการนำาเข้าฐานข้อมูล

เมื่อเราใช้งานฐานข้อมูลไปเรื่อยๆ ฐานข้อมูลที่ถูกเก็บอยู่ที่ /var/lib/mysql ก็จะโตขึ้นเรื่อยๆ

ใน /var/lib/mysql จะเป็นที่เก็บของฐานข้อมูลระบบของ MySQL เอง และฐานข้อมูลที่เราสร้างขึ้นมาซึ่งจะมีชื่อ

ไดเรกทอรีตรงกับชื่อฐานข้อมูลที่เราสร้าง ในการสำารองฐานข้อมูลก็สามารถก๊อปปี้ไดเรกทอรีที่เป็นชื่อฐานข้อมูล



เก็บไว้ได้แต่ไม่ใช่วิธีการที่ดนัก มีวิธีการที่ดีกว่านั้นคือใช้คำาสั่ง mysqldump



ตัวอย่างการสำารองข้อมูลด้วย mysqldump

root@ubuntu­server:~# mysqldump drupal6 > 

/home/sothorn/drupal5x_2008­09­30.sql ­u  root ­p

Enter password: 

root@ubuntu­server:~#





เราก็จะได้ไฟล์ drupal6_2008-09-30.sql เก็บเอาไว้สำาหรับการนำาเข้าที่ฐานข้อมูลใหม่ อย่าใช้คำาสั่งนำาเข้า

กับฐานข้อมูลเดิมเป็นอันขาดข้อมูลจะมีการเสียหาย หรือซำ้าซ้อนได้



ตัวอย่างการนำาเข้าฐานข้อมูล โดยการสร้างฐานข้อมูลใหม่ด้วยคำาสั่ง mysqladmin create

root@ubuntu­server:~# mysqladmin create new_drupal ­u root ­p

Enter password: 

root@ubuntu­server:~# mysql new_drupal 
­u root ­p

Enter password: 

root@ubuntu­server:~#









อ้างอิง

http://th.wikipedia.org/

http://dev.mysql.com/doc/refman/5.0/en/tutorial.html









Ubuntu Server 8.04 : MySQL เบื้องต้น sothorn@gmail.com


Related docs
Other docs by anusorn kuanha
Mysql
Views: 31  |  Downloads: 0
keyword
Views: 18  |  Downloads: 0
Apache for Windows Installation
Views: 27  |  Downloads: 0
GoogleAdsense
Views: 10  |  Downloads: 0
link
Views: 62  |  Downloads: 0
InternetMarketing
Views: 0  |  Downloads: 0
seo
Views: 17  |  Downloads: 0
php
Views: 24  |  Downloads: 0
Link Exchange
Views: 13  |  Downloads: 0
Webmaster Tools
Views: 16  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!