หน้าเว็บ

แสดงบทความที่มีป้ายกำกับ mysql แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ mysql แสดงบทความทั้งหมด

วันจันทร์ที่ 26 ตุลาคม พ.ศ. 2552

Introduction to Wamp Server

Apache, MySQL, PHP on Windows


WampServer is a Windows web development environment. It allows you to create web applications with Apache, PHP and the MySQL database. It also comes with PHPMyAdmin to easily manage your databases.

WampServer installs automatically (installer), and its usage is very intuitive. You will be able to tune your server without even touching the setting files.

WampServer is the only packaged solution that will allow you to reproduce your production server. Once WampServer is installed, you have the possibility to add as many Apache, MySQL and PHP releases as you want.

WampServer also has a trayicon to manage your server and its settings.




Includes :
- Apache 2.2.11
- MySQL 5.1.36
- PHP 5.3.0



Installing


Double click on the downloaded file and just follow the instructions. Everything is automatic. The WampServer package is delivered whith the latest releases of Apache, MySQL and PHP.

Once WampServer is installed, you can add other releases by downloading them on this website. They will then appear in the WampServer menu and you will be able to switch releases with a simple click.
Each release of Apache, MySQL and PHP has its own settings and its own files (datas for MySQL).

Functionalities


WampServer's functionalities are very complete and easy to use so we won't explain here how to use them.

With a left click on WampServer's icon, you will be able to:
- manage your Apache and MySQL services
- switch online/offline (give access to everyone or only localhost)
- install and switch Apache, MySQL and PHP releases
- manage your servers settings
- access your logs
- access your settings files
- create alias

With a right click :
- change WampServer's menu language
- access this page

How to start


When you install WampServer, a "www" directory is created (generally c:\wamp\www). Create a directory inside for your project and put your PHP files in it.
Click on the link "Localhost" in the WampServer menu or open your browser and open the http://localhost address.




Add Apache, MySQL and PHP releases


WampServer allows you to install almost all the existing releases of Apache, PHP and MySQL so you can reproduce exactly the settings of your production server.
To add a new release, donwload it here and install it.

Then click on the WampServer menu and activate the release that you want to use.



Wait until the WampServer icon become white again and start to work.


Downloads


WampServer is an open source project, free to use (GPL licence). If you think our work deserves it and you want to help us, you can make a donation with paypal.



Apache 2.2.11
PHP 5.3.0
MySQL 5.1.36
Phpmyadmin

size: 16Mo

Thanks : wampserver.com

วันพุธที่ 7 ตุลาคม พ.ศ. 2552

problem thai language with Mysql

ปัญหาภาษาไทยกับ กับ Mysql


วิธีแก้ 1 กำหนด Database ให้ใช้ tis620

แก้ไขไฟล์ config ของ MySQL โดยการกำหนดค่าดังนี้
CODE
[mysqld]
default-character-set=tis-620

บน Linux จะอยู่ที่ etc/my.cnf
บน Windows จะอยู่ที่ windows/my.ini
restart MySQL อีกครั้งหนึ่ง ตรวจสอบให้แน่ใจว่า default character เป็น tis620 แล้ว
ตัวอย่าง การตรวจสอบ ด้วยคำสั่ง s ใน mysql client บน Windows
QUOTE
mysql> s
--------------
mysql Ver 14.12 Distrib 5.0.18, for Win32 (ia32)

Connection id: 2
Current database:
Current user: root@localhost
SSL: Not in use
Using delimiter: ;
Server version: 5.0.18
Protocol version: 10
Connection: localhost via TCP/IP
Server characterset: tis620
Db characterset: tis620
Client characterset: latin1
Conn. characterset: latin1
TCP port: 3306
Uptime: 30 sec

Threads: 1 Questions: 3 Slow queries: 0 Opens: 0 Flush tables: 1 Open table
s: 3 Queries per second avg: 0.100
--------------


ตัวอย่าง การตรวจสอบ ด้วยคำสั่ง s ใน mysql client บน Linux
QUOTE
mysql Ver 14.7 Distrib 4.1.7, for redhat-linux-gnu (i386)

Connection id: 3
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 4.1.7
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: tis620
Client characterset: tis620
Conn. characterset: latin1
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 5 sec

Threads: 1 Questions: 5 Slow queries: 0 Opens: 11 Flush tables: 1 Open tables: 0 Queries per second avg: 1.000
--------------


นอกจากนี้เราสามารถใช้ MySQL Client tool อื่นๆ สำหรับตรวจสอบได้เช่นกัน

ตัวอย่างค่า default เริ่มแรก
user posted image

เมื่อปรับค่า default เป็น tis620
user posted image



ดังนั้น ตัวแปลดังกล่าว ควรจะมีค่าตังนี้
QUOTE
- character_set_client = tis620
- character_set_connection = tis620
- character_set_database = tis620
- character_set_results = tis620
- character_set_server = tis620
- character_set_system = utf8
- collation_connection = tis620_thai_ci
- collation_database = tis620_thai_ci
- collation_server = tis620_thai_ci


character set กับ collation ??

character set คือรูปแบบของการจัดเก็บข้อมูล เราสามารถดูรูปแบบที่ MySQL สนับสนุนการใช้งานได้จาก คำสั่ง SHOW CHARACTER SET;

collation คือรูปแบบของการจัดเรียงและเปรียบเทียบ รูปแบบการจัดเรียงที่มักใช้กันบ่อยๆ มักลงท้ายด้วย ci ,cs, bin (เรียงตามลำดับ) เราสามารถดูรายชื่อ collation ที่สนับสนุนได้จาก SHOW COLLATION;

ci, cs, bin ?? มีความหมายดังนี้
ci = case-insensitive จัดเรียงตามตัวอักษร มองตัวอักษรใหญ่ และเล็กมีความสำคัญเท่ากัน โดยปกติมักจะใช้ collation แบบ ci
cs = case sensitive จัดเรียงโดยให้ความสำคัญกับอักษรตัวใหญ่ มากกว่าตัวเล็ก เช่น Z จะมาก่อน a
bin = binary ใช้สำหรับการเปรียบเทียบ





2. บอกให้ mysql รู้ถึงรูปแบบของภาษาที่ต้องการใช้ ผ่าน sql command
เราสามารถใช้คำสั่ง SET เพื่อกำหนดค่าตัวแปลของ MySQL ได้
ดังนั้น ก่อนที่จะ insert, update เราต้องบอกให้ MySQL ทราบถึงรูปแบบของภาษาที่ใช้ ยกตัวอย่างเช่น

CODE
mysql_query("SET NAMES 'tis620' ");
mysql_query("SELECT * FROM TEST");



(UPDATE - 2006.02.08)

ยังมีอีกวิธีหนึ่งคือ แนบคำสั่งไปกับประโยค SQL เลย เช่น

CODE
mysql_query("SELECT * FROM TEST ORDER BY idRec COLLATE tis620_thai_ci");


Thanks : Patrickz 2.0

Links
MySQL : Chapter 10. Character Set Support
เมื่ออักษรภาษาไทยกลายเป็น ?????? (phpconcept.com)

วันพุธที่ 29 กรกฎาคม พ.ศ. 2552

Replicate Mysql

Replicate Mysql

MySQL เป็นฐานข้อมูลที่ได้รับความนิยมอย่างมากในปัจจุบัน อาจทำงานได้ดี มีเสถียรภาพ มีขนาดเล็ก ติดตั้งง่าย หาเอกสารอ้างอิงง่าย อีกทั้งยังถูกจับคู่กับ PHP ชนิดที่เรียกกันว่า ซี้ปึ๊ก กันเลยทีเดียว หมายถึงคำสั่งต่างๆ ของ PHP จะมีสามารถเรียกใช้ความสามารถของ MySQL ได้อย่างเด็มประสิทธิภาพทีเดียว ยิ่งหากได้ใช้ phpmyadmin อีกยิ่งเข้ากันเป็นปี่เป็นขลุ่ย แถมหาก Run อยู่บน Apache อีก โอ้ว เ้ข้าแก๊ง แต่ยังขาดหัวหน้าแก๊งไปได้ไม่ได้ นั่นคือ Linux โอ อย่างนี้ต้องเรียกว่า มาเป็นทีม อาจเรียกได้ว่าเป็น Dream Team หรือทีมในฝันของนักพัฒนาเว็บแอพพลิเคชั่นเลยทีเดียว ซึ่งทีมนี้มีชื่อเล่น ว่า LAMP (Linux, Apache MySQL, PHP) *ขออภัย นอกเรื่องอีกแล้ว

MySQL สามารถทำงานได้ทั้งระบบปฏิบัติการ Windows, Linux, Solaris, FreeBSD ทั้งแบบ 32 และ 64 Bit ซึ่งปัจจุบันที่กำัลังเขียนอยู่นี้ Version 5.1.20 Beta Replicate MySQL ล่ะคืออะไร เกี่ยวข้องกันอย่างไร … หลายท่านรู้จักการ Backup ข้อมูล หากท่านรู้จักการทำ Mirror Disk นั่นแหละ ความหมายเดียวกัน …

ฐานข้อมูลนั้นมีความสำคัญอย่างยิ่ง เนื่องจากเว็บแอพลิเคชั่นนั้นใช้ฐานข้อมูลเก็บข้อมูลทุกสิ่งทุก ๆ อย่าง ฐานข้อมูลก็ไม่ได้ถูกโหลดเข้า Memory ไ้้่ด้ทั้งหมด ก็ยังคงต้องใช้ Disk ซึ่งเจ้า Disk นี่แหละ ที่ชอบสร้างปัญหาให้กับระบบงานอยู่บ่อยครั้ง หากข้อมูลที่สำคัญ เช่น ข้อมูลด้านการเงิน ข้อมูลการขายหน้าร้าน เกิดเรียกใช้งานไม่ได้ หรือหายไป ย่อมเกิดความเสียหายให้กับหน่วยงาน เป็นอย่างมาก จะดีไหมหากเรามีข้อมูลสำรองไว้ยัง Disk อีกก้อน หรือ Server อีกตัว เพื่อเป็นการลดความเสี่ยงสำหรับการสูญเสียข้อมูล เพราะฉะัีนั้น เรามาลองทำ Replicate MySQL กัน สิ่งที่ท่านต้องมีการ

  1. เครื่องคอมพิวเตอร์จำนวน 2 เครื่อง หรือมากกว่า เครื่องแรกเรียกว่า Master เครื่องถัดไปเรียกว่า Slave.
  2. ทั้ง 2 เครื่องลง Program MySQL โดยที่สามารถ Download ได้ที่ http://dev.mysql.com/downloads/mysql/5.1.html

ภาพรวมของการทำ Replicate MySQL


replicatemysql3.jpg
Pdf:replicate-mysql.pdf

ภาพที่ 1 ภาพรวมของการทำ Replicate MySQL


ขั้นตอนที่ 1.

หากต้องการ Replicate Master ที่มีข้อมูลอยู่แล้ว จะต้อง Dump ข้อมูลออกมาเพื่อทำให้ Slave มีข้อมูลที่เท่าเทียมกันเสียก่อน

ขั้นตอนที่ 2.

สร้าง User ขึ้นมาเพื่อทำหน้าที่สำหรับทำหน้าที่ Replicate เท่านั้นในฝั่ง Master โดยใช้คำสั่ง

mysql> GRANT REPLICATION SLAVE ON *.*
    -> TO 'repl'@'%.192.168.0.20' IDENTIFIED BY 'slavepass';

* โดยที่ให้สิทธิ์ให้การ Replicate จาก User ที่ชื่อว่า repl และรหัสผ่าน slavepass ที่มาจากเครื่อง IP 192.168.0.20 เท่านั้น

ขั้นตอนที่ 3.

ฝั่ง Master เข้าไปแก้ไขไฟล์ my.ini ที่ C:\Program Files\MySQL\MySQL Server 5.1>my.ini โดยเพิ่มด้านล้างหัวข้อ [mysqld] ดังนี้

# ——————————
log-bin=mysql-bin
server-id=1
innodb_flush_log_at_trx_commit=1
sync_binlog=1
# ——————————-

โดยที่ :

log-bin=mysql-bin # Binary Log
server-id=1 # ลำดับ Server master กำหนด =1
innodb_flush_log_at_trx_commit=1 #สำหรับผู้ใช้ฐานข้อมูลที่เป็น InnoDB จำเ็ป็นจะต้องให้ Master
#commit งานให้เรียบร้อยก่อน ถึงจะทำการ Replicate ไปให้ Slave
sync_binlog=1 # ทำการ Sync Log สำหรับ InnoDB และหากเกิด Master Crash จะไม่ทำให้ Slave Sync ข้อมูลซ้ำ ในกรณีที่ Master ฟื้นกลับมาอีกครั้ง
ขั้นตอนที่ 4.

ฝั่ง Slave ให้เข้าไปแก้ไขไฟล์ my.ini ที่ C:\Program Files\MySQL\MySQL Server 5.1>my.ini เช่นเดียวกับฝั่ง Master โดยเพิ่มด้านล้างหัวข้อ [mysqld] ดังนี้

#————————————————-
server-id=2
master-host=192.168.0.10
master-port=3306
master-user=repl
master-password-slavepass
master-connect-retry=30
replicate-wild-do-table= %.%
report-host=192.168.0.20
#————————————————-

โดยที่
server-id=2 # ลำดับ Slave หากมี Slave มากกว่า 1 ตัวสามารถกำหนด Server-id ได้จนถึง (2 ยกกำลัง 32) -1 เครื่อง
master-host=192.168.0.10 # หมายเลข IP เครื่อง Master
master-port=3306 # กำหนด Port
master-user=repl # ชื่อ User สำหรับการ Replicate
master-password-slavepass # Password
master-connect-retry=30 # หากติดต่อ Master ไ่ม่ได้ จะติดต่อซ้ำภายใน
replicate-wild-do-table= %.% #กำหนดฐานข้อมูลที่ต้องการ Replicate %.% หมายถึง Database ทุกตัว

ขั้นตอนที่ 5.

เมื่อ Master และ Slave มีข้อมูลเท่ากันแล้ว ดังในขั้นตอนที่ 1 ให้สร้างจุด Check Point เพื่อให้เป็นจุดเริ่มต้นสำหรับการ Replicate โดยใช้คำสั่ง

 mysql> FLUSH TABLES WITH READ LOCK;

ให้เรียกดูสถานะการทำงาน เพื่อดู Binary Log name และ Offset ของ Master ดังนี้

replicatemysql5.jpg

ภาพการเรียกดู BinaryLog Name และ Offset ของ Master

Binary Log Name : mysql-bin.00004
Offset : 106

ขั้นตอนที่ 6.

กำหนดฝั่ง Client ให้ติดต่อกับฐานข้อมูล

mysql> CHANGE MASTER TO

-> MASTER_HOST='192,168.0.10',

-> MASTER_USER='repl',

-> MASTER_PASSWORD='slavepass',

-> MASTER_LOG_FILE='mysql-bin.00004‘,

-> MASTER_LOG_POS=106;

และเริ่มต้น Replicate โดยใช้คำสั่ง

mysql> START SLAVE;

เพียงเท่านี้ Slave ก็จะเริ่ม Replicate ข้อมูลจาก Master แล้วครับ

สรุป

การ Replicate นั้นเป็นเพียงการพอจะรับประกันได้ว่า ข้อมูลจะไม่สูญหาย แต่ไม่ได้หมายความถึงการแบ่งการทำงานเช่น การทำ Load Balance หรือแม้กระทั่ง หาก Server มีปัญหา ไม่ใช่หมายความว่า Slave จะสามารถทำงานได้ทันที ซึ่งหากต้องการทำเช่นนั้นจะมีต้องการเช็คสถานะการทำงานของตัว Master และ Slave ที่ส่วนใหญ่จะเรียกกันว่า สาย heartbeat สำหรับ คอยตรวจสอบสถานะซึ่งกันและกัน ซึ่งพอจะมี Software OpenSource เช่น Ultra Monky ในการทำ Load Balance (แต่ดูเหมือนจะหยุดพัฒนาไปแล้วนะ) สามารถเข้าไปดูรายละเอียดได้ที่

Ultra Monky : http://www.ultramonkey.org/

แต่หากต้องการทำทุกสิ่งทุกอย่าง เช่น Replicate, Load Balance ทาง MySQL เองก็มีเทคโนโลยี Cluster ที่เหมาุะสำหรับระบบที่ต้องการเสถียรภาพสูง (ซึ่งต้องแลกมาด้วยการใช้ Server อีกหลายตัว) โดยขั้นต่ำ ต้องใช้อย่างน้อย 4 ตัว สำหรับการทำ Cluster แต่ีรับประกันความเสถียร และประสิทธิภาพ โดยสามารถดูเอกสารประกอบได้ที่

Thanks : ejeepss blog || MySQL Cluster || MySQL

icon_pdf.gif : how-to-replicate-mysql-5-ejeepss.pdf