วันศุกร์ที่ 31 ตุลาคม พ.ศ. 2557

การติดตั้ง MySQL สำหรับ Windows 7

Step 1: Download and Install MySQL


เริ่มต้นไปยังลิงค์ http://dev.mysql.com/downloads/ จะไปหน้าเว็บตามรูปที่ 1 จากนั้นคลิกลิงค์ MySQL on Windows

รูปที่ 1

ตามรูปที่ 2 ให้คลิกลิงค์ MySQL Installer

รูปที่ 2

จากรูปที่ 2.1 ให้คลิกลิงค์ที่ MySQL Server

รูปที่ 2.1

ตามรูปที่ 3 ให้กดปุ่ม Download ซึ่งกรณีนี้จะเป็นการ Download .zip ไฟล์

รูปที่ 3

ถ้ายังไม่มี User Login จะ Download ไม่ได้นะครับ ไปกด Register ตามรูปที่ 4 ก่อน

รูปที่ 4 

เมื่อเราได้ไฟล์มาแล้วให้ทำการแตกมันออกมา ไว้ใน Path ที่เราต้องการ

รูปที่ 5 

ตามรูปที่ 6 แสดงผลของการแตก .zip ไฟล์

รูปที่ 6


Step 2: Create the Configuration File


ให้เราเปิดโปรแกรม text editor (เช่น NodePad++) และสร้างไฟล์ชื่อ "my.ini" และให้บันทึกลงใน Path ที่เรา unzip ไฟล์ MySQL ตาม Step 1 บรรทัดที่เริ่มต้นด้วย # เป็นการเขียน comments

ข้อสำคัญ: ให้แก้ไขส่วนที่ highlighted ให้ตรงกับ Path ที่เรา unzip ไฟล์ตาม step 1 !!!
# Save as "my.ini" in your MySQL installed directory (e.g. c:\mysql-5.6.21-winx64)
[mysqld]
# Set MySQL base (installed) directory
# @@ Change to your MySQL installed directory @@
# Use forward slash (instead of backward slash) as directory separator
basedir=c:/mysql-5.6.21-winx64
# Set MySQL data directory
# @@ Change to sub-directory "data" of your MySQL installed directory  @@
datadir=c:/mysql-5.6.21-winx64/data

# Run the server on this TCP port number
port=8888
   
[client]
# MySQL client connects to the server running on this TCP port number
port=8888

Step 3: Start the Server


จากรูปที่ 7 แสดงให้เห็นว่า MySQL เป็นระบบการทำงานแบบ client-server โดยเราจะใช้คำสั่ง "mysqld --console" ในการ start server ส่วนการใช้งานเราจะใช้คำสั่ง  "mysql" ในการติดต่อเพื่อใช้งานฐานข้อมูล MySQL Server 

รูปที่ 7

การ Start MySQL ด้วย command line


ให้เราเปิด windows command ขึ้นมาแล้ว cd เข้าไปยัง Path ที่เราวาง .zip ไฟล์ ตามรูปที่ 6 เราจะเห็นโฟลเดอร์ bin ให้ cd เข้าไปแล้วพิมพ์ mysqld --console แล้วกด enter
จะได้ผลการ start ตามรูปที่ 8 และพร้อมให้ client ติดต่อเพื่อใช้งานได้

รูปที่ 8


การ Stop MySQL ด้วย command line

ให้เรากดปุ่ม Ctrl+c เพื่อ shutdown ได้เลย 
หรือให้เราเปิด windows command ขึ้นมาแล้ว cd เข้าไปยัง Path ที่เราวาง .zip ไฟล์ ตามรูปที่ 6 เราจะเห็นโฟลเดอร์ bin ให้ cd เข้าไปแล้วพิมพ์ mysqladmin -u root shutdown แล้วกด enter จะได้ผลการ start ตามรูปที่ 9 

ข้อระวังไม่ควรปิด windows commad เลยโดยที่ยังไม่ได้ shutdown

รูปที่ 9

Step 4: Start a Client

เมื่อทำการ start server แล้ว เราสามารถใช้ client ติดต่อบ database server ได้มากกว่า 1 client และยังสามารติดต่อโดยตรงผ่าน local หรือ remote ผ่าน network จากเครื่องอื่นๆก็ได้ ซึ่งการติดต่อกับ MySQL Server ต้องมีการระบุ username และ password แต่โดย default เราจะมี superuser ไว้ใช้งานได้เลยซึ่งมี username ชื่อ "rootและ password เป็นค่าว่าง (ไม่ควรใช้ในงานจริง)
   

MySQL ได้เตรียมโปรแกรม client ไว้ให้เราแล้วโดยใช้คำสั่ง  "mysql" ดังข้างล่าง 

ให้ทำการเปิดหน้าต่าง windows command ขึ้นมาใหม่ เพื่อใช้รันโปรแกรม client:
-- Change the current directory to <MYSQL_HOME>\bin.
-- Assume that the MySQL is installed in "d:\myProject\mysql".
Prompt> c:                       -- Change the current drive
c:\...> cd \mysql-5.6.21-winx64\bin  -- Change Directory to YOUR MySQL's "bin" directory
   
-- Start a client with superuser "root"
c:\mysql-5.6.21-winx64\bin> mysql -u root -p
Enter password:     // Hit "Enter" key for empty password set in installation 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.39-community MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

-- Client started. The prompt changes to "mysql>".
-- You can now issue SQL commands.
mysql>

ให้ลองทดสอบการใช้งานเบื้องต้น
mysql> status
--------------
mysql  Ver 14.14 Distrib 5.1.51, for Win32 (ia32)
......
Current user:           root@localhost
TCP port:               8888    -- for Windows only
......



ไม่มีความคิดเห็น:

แสดงความคิดเห็น