<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SpaLinux.com - สปาลีนุกซ์ &#187; Database</title>
	<atom:link href="http://spalinux.com/category/database/feed" rel="self" type="application/rss+xml" />
	<link>http://spalinux.com</link>
	<description>เพื่อชีวิตที่ผ่อนคลายของคนใช้ลีนุกซ์</description>
	<lastBuildDate>Sat, 12 May 2012 10:38:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>ติดตั้งโมดูล PHP เพิ่มเติม บน CentOS 6</title>
		<link>http://spalinux.com/2012/04/install-more-php-module-on-centos-6</link>
		<comments>http://spalinux.com/2012/04/install-more-php-module-on-centos-6#comments</comments>
		<pubDate>Mon, 16 Apr 2012 16:00:43 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[CentOS 6]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Linux Installation]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Server]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=2115</guid>
		<description><![CDATA[จากการ ติดตั้งโปรแกรมภาษาเว็บ PHP บน Apache เว็บเซิร์ฟเวอร์ Apache และ PHP เครื่องนี้ สามารถเขียนเว็บเพจโดยใช้ภาษา PHP ได้แล้ว  แต่สามารถเรียกใช้ฟังก์ชั่นแค่พื้นฐานเท่านั้น  ถ้าต้องการใช้ฟังก์ชั่นอื่นๆ ด้วย เช่นเชื่อมต่อเข้ากับ MySQL เราจำเป็นต้องติดตั้งโมดูล PHP เพิ่มเติม ในบทความนี้จะอธิบายวิธีการตรวจสอบและติดตั้งโมดูลเพิ่มเติมจากไฟล์ rpm ที่อยู่ในแผ่นดีวีดี CentOS 6.2  โดยจะเป็นการติดตั้งโมดูล MySQL เพื่อให้สามารถเขียน PHP เชื่อมเข้ากับฐานข้อมูล MySQL ได้ ตัวอย่าง error เมื่อเรียกใช้ฟังก์ชั่นของโมดูลที่ไม่มีหรือยังไม่ได้ติดตั้ง [root@cent6 ~]# cat test-mysql.php &#60;?php mysql_connect('localhost', 'mysql_user', 'mysql_password'); ?&#62; ทดลองรันไฟล์นี้ด้วย php [root@cent6 ~]# php test-mysql.php PHP Fatal error:  Call [...]]]></description>
			<content:encoded><![CDATA[<p>จากการ <a title="View Details: ติดตั้งโปรแกรมภาษาเว็บ PHP บน Apache" href="http://spalinux.com/2012/04/install-php-web-programming-on-apache" rel="bookmark">ติดตั้งโปรแกรมภาษาเว็บ PHP บน Apache</a></p>
<p>เว็บเซิร์ฟเวอร์ Apache และ PHP เครื่องนี้ สามารถเขียนเว็บเพจโดยใช้ภาษา PHP ได้แล้ว  แต่สามารถเรียกใช้ฟังก์ชั่นแค่พื้นฐานเท่านั้น  ถ้าต้องการใช้ฟังก์ชั่นอื่นๆ ด้วย เช่นเชื่อมต่อเข้ากับ MySQL เราจำเป็นต้องติดตั้งโมดูล PHP เพิ่มเติม</p>
<p>ในบทความนี้จะอธิบายวิธีการตรวจสอบและติดตั้งโมดูลเพิ่มเติมจากไฟล์ rpm ที่อยู่ในแผ่นดีวีดี CentOS 6.2  โดยจะเป็นการติดตั้งโมดูล MySQL เพื่อให้สามารถเขียน PHP เชื่อมเข้ากับฐานข้อมูล MySQL ได้</p>
<p><span id="more-2115"></span></p>
<p>ตัวอย่าง error เมื่อเรียกใช้ฟังก์ชั่นของโมดูลที่ไม่มีหรือยังไม่ได้ติดตั้ง</p>
<pre>[root@cent6 ~]# <strong>cat test-mysql.php</strong>
&lt;?php
mysql_connect('localhost', 'mysql_user', 'mysql_password');
?&gt;</pre>
<p>ทดลองรันไฟล์นี้ด้วย php</p>
<pre>[root@cent6 ~]# <strong>php test-mysql.php</strong>
<span style="color: #ff0000;">PHP Fatal error:  Call to undefined function mysql_connect()</span> in /var/www/html/test-mysql.php on line 2</pre>
<p>วิธีการตรวจสอบว่า การเขียนโปรแกรมภาษา PHP บนเว็บเซิร์ฟเวอร์นี้ สามารถเรียกใช้โมดูล PHP อะไรได้บ้าง ทำได้โดยใช้ฟังก์ชั่น phpinfo() แล้วเปิดผ่านหน้าเว็บเพจ หรือรันคำสั่ง php ตามด้วยออปชั่น &#8220;-m&#8221;</p>
<pre>[root@cent6 html]# <strong>php -m</strong>
[PHP Modules]
bz2
calendar
Core
ctype
curl
date
ereg
exif
fileinfo
filter
ftp
gettext
gmp
hash
iconv
json
libxml
openssl
pcntl
pcre
Phar
readline
Reflection
session
shmop
SimpleXML
sockets
SPL
standard
tokenizer
xml
zip
zlib</pre>
<pre>[Zend Modules]</pre>
<p>&nbsp;</p>
<p>หากต้องการเรียกใช้โมดูลมากกว่านี้ จำเป็นต้องติดตั้งเพิ่มเติม  วิธีหนึ่งคือติดตั้งจากไฟล์ rpm ในแผ่นดีวีดี CentOS 6.2</p>
<p>ไฟล์ rpm ใน CentOS 6.2 ที่เกี่ยวข้องกับ PHP ไม่ว่าจะเป็นส่วนโปรแกรมหลัก หรือโมดูลเพิ่มเติม จะขึ้นต้นชื่อไฟล์ด้วย php</p>
<pre>[root@cent6 ~]# <strong>mount /dev/cdrom /mnt</strong>
 mount: block device /dev/sr0 is write-protected, mounting read-only</pre>
<pre>[root@cent6 ~]# <strong>cd /mnt/Packages/</strong>
[root@cent6 Packages]# <strong>ls -l php-*</strong>
-rw-r--r--. 2 500 500 1176604 Nov 15 18:02 php-5.3.3-3.el6_1.3.x86_64.rpm
-rw-r--r--. 2 500 500 2276776 Nov 15 18:02 php-cli-5.3.3-3.el6_1.3.x86_64.rpm
-rw-r--r--. 2 500 500  534024 Nov 15 18:02 php-common-5.3.3-3.el6_1.3.x86_64.rpm
-rw-r--r--. 2 500 500  105780 Nov 15 18:02 php-gd-5.3.3-3.el6_1.3.x86_64.rpm
-rw-r--r--. 2 500 500   36360 Nov 15 18:02 php-ldap-5.3.3-3.el6_1.3.x86_64.rpm
-rw-r--r--. 2 500 500   80300 Nov 15 18:02 <span style="color: #0000ff;">php-mysql-5.3.3-3.el6_1.3.x86_64.rpm</span>
-rw-r--r--. 2 500 500   49116 Nov 15 18:02 php-odbc-5.3.3-3.el6_1.3.x86_64.rpm
-rw-r--r--. 2 500 500   74148 Nov 15 18:02 <span style="color: #0000ff;">php-pdo-5.3.3-3.el6_1.3.x86_64.rpm</span>
-rw-r--r--. 2 500 500  402492 Dec  9 03:54 php-pear-1.9.4-4.el6.noarch.rpm
-rw-r--r--. 2 500 500   95556 Jul  3  2011 php-pecl-apc-3.1.3p1-1.2.el6.1.x86_64.rpm
-rw-r--r--. 2 500 500   61400 Sep 26  2011 php-pecl-memcache-3.0.5-3.el6.x86_64.rpm
-rw-r--r--. 2 500 500   69044 Nov 15 18:02 php-pgsql-5.3.3-3.el6_1.3.x86_64.rpm
-rw-r--r--. 2 500 500  140600 Nov 15 18:02 php-soap-5.3.3-3.el6_1.3.x86_64.rpm
-rw-r--r--. 2 500 500  102132 Nov 15 18:02 php-xml-5.3.3-3.el6_1.3.x86_64.rpm
-rw-r--r--. 2 500 500   50964 Nov 15 18:02 php-xmlrpc-5.3.3-3.el6_1.3.x86_64.rpm</pre>
<p>ใช้คำสั่ง rpm เพื่อติดตั้ง module เพิ่มเติม สำหรับการติดตั้ง MySQL module</p>
<pre>[root@cent6 Packages]# <strong>rpm -i php-mysql-5.3.3-3.el6_1.3.x86_64.rpm</strong>
<span style="color: #ff0000;">error: Failed dependencies:</span>
 php-pdo is needed by php-mysql-5.3.3-3.el6_1.3.x86_64</pre>
<p>ต้องติดตั้ง php-pdo (PDO module) ก่อน</p>
<pre>[root@cent6 Packages]# <strong>rpm -i php-pdo-5.3.3-3.el6_1.3.x86_64.rpm</strong>
[root@cent6 Packages]# <strong>rpm -i php-mysql-5.3.3-3.el6_1.3.x86_64.rpm</strong></pre>
<p>ใช้คำสั่ง php ออปชั่น -m เพื่อตรวจสอบ module ที่ติดตั้งเพิ่มเติม</p>
<pre>[root@cent6 Packages]# <strong>php -m</strong>
[PHP Modules]
bz2
calendar
Core
ctype
curl
date
ereg
exif
fileinfo
filter
ftp
gettext
gmp
hash
iconv
json
libxml
<span style="color: #0000ff;">mysql</span>
<span style="color: #0000ff;">mysqli</span>
openssl
pcntl
pcre
<span style="color: #0000ff;">PDO</span>
<span style="color: #0000ff;">pdo_mysql</span>
<span style="color: #0000ff;">pdo_sqlite</span>
Phar
readline
Reflection
session
shmop
SimpleXML
sockets
SPL
<span style="color: #0000ff;">sqlite3</span>
standard
tokenizer
xml
zip
zlib</pre>
<pre>Zend Modules]</pre>
<p>คำอธิบาย</p>
<ul>
<li>PDO, pdo_sqlite, sqlite3 ได้จากการติดตั้งไฟล์ php-pdo</li>
<li>mysql, mysqli, pdo_mysql ได้จากการติดตั้งไฟล์ php-mysql</li>
</ul>
<p>&nbsp;</p>
<p>หลังจากการติดตั้ง PHP module เพิ่มเติม การรัน PHP ที่เป็น command line สามารถเรียกใช้โมดูลใหม่นี้ได้เลย แต่ถ้าต้องการเรียกผ่านเว็บเซิร์ฟเวอร์ ต้องรีสตาร์ตเซอร์วิส httpd ก่อน</p>
<p>ใช้คำสั่ง service เพื่อรีสตาร์ตเซอร์วิส httpd</p>
<pre>[root@cent6 Packages]# <strong>service httpd restart</strong>
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]</pre>
<p>ตัวอย่างการใช้ฟังก์ชั่น phpinfo() แสดงผลผ่านหน้าเว็บเพจ เพื่อดูข้อมูลรายละเอียดโมดูล MySQL ที่ติดตั้งใหม่</p>
<pre>[root@cent6 ~]# <strong>cat /var/www/html/info.php</strong>
 &lt;?php
 phpinfo();
 ?&gt;</pre>
<p>ผลลัพธ์ที่ได้จากการเรียกผ่านหน้าเว็บ</p>
<p><a href="http://spalinux.com/wp-content/uploads/2012/04/install-php-module.png"><img title="install-php-module" src="http://spalinux.com/wp-content/uploads/2012/04/install-php-module.png" alt="" width="624" height="289" /></a></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2012/04/install-more-php-module-on-centos-6/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>การสตาร์ต MySQL Database Server บน CentOS 6</title>
		<link>http://spalinux.com/2012/04/howto-start-apache-web-server-on-centos-6-2</link>
		<comments>http://spalinux.com/2012/04/howto-start-apache-web-server-on-centos-6-2#comments</comments>
		<pubDate>Fri, 13 Apr 2012 17:12:56 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[CentOS 6]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Linux Installation]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[start]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=2077</guid>
		<description><![CDATA[หลังจากที่ ติดตั้งฐานข้อมูล MySQL เรียบร้อยแล้ว ขั้นตอนต่อไปเป็นการสตาร์ต MySQL Database Server เพื่อรันเซอร์วิสทำหน้าที่เป็นเซิร์ฟเวอร์ฐานข้อมูล ใน CentOS 6 จะตั้งชื่อเซอร์วิส MySQL Database Server ว่า mysqld รันเซอร์วิส mysqd ใช้คำสั่ง service เพื่อสตาร์ตเซอร์วิส mysqld [root@cent6 ~]# service mysqld start Initializing MySQL database:  Installing MySQL system tables... OK Filling help tables... OK To start mysqld at boot time you have to copy support-files/mysql.server to the right [...]]]></description>
			<content:encoded><![CDATA[<p>หลังจากที่ <a title="View Details: ติดตั้งฐานข้อมูล MySQL" href="http://spalinux.com/2012/04/install-mysql-database" rel="bookmark">ติดตั้งฐานข้อมูล MySQL</a> เรียบร้อยแล้ว</p>
<p>ขั้นตอนต่อไปเป็นการสตาร์ต MySQL Database Server เพื่อรันเซอร์วิสทำหน้าที่เป็นเซิร์ฟเวอร์ฐานข้อมูล</p>
<p>ใน CentOS 6 จะตั้งชื่อเซอร์วิส MySQL Database Server ว่า mysqld</p>
<h4><span id="more-2077"></span><br />
รันเซอร์วิส mysqd</h4>
<p>ใช้คำสั่ง service เพื่อสตาร์ตเซอร์วิส mysqld</p>
<pre>[root@cent6 ~]# <strong>service mysqld start</strong>
Initializing MySQL database:  Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h cent6.example.com password 'new-password'

Alternatively you can run:
<span style="color: #0000ff;">/usr/bin/mysql_secure_installation</span>

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &amp;

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

                                                           [  OK  ]
Starting mysqld:                                           [  OK  ]</pre>
<p>&nbsp;</p>
<p>หากเป็นการรันเซอร์วิส mysqld ครั้งแรกจะมีข้อความต่างๆ ขึ้นมากมาย เหมือนข้างบน เพื่อแนะนำการรันเซอร์วิส พร้อมทั้งเตือนให้มีการเปลี่ยน password ในการใช้ฐานข้อมูล</p>
<p>แนะนำให้รันคำสั่ง mysql_secure_installation เพื่อแก้ไขคอนฟิกต่างๆ ตามคำแนะนำ</p>
<pre>[root@cent6 ~]# <strong>mysql_secure_installation</strong>

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

<span style="color: #0000ff;">Enter current password for root (enter for none):</span></pre>
<p><span style="color: #0000ff;">ใส่รหัสผ่านของ root (ของ mysqld)  ถ้าเป็นการสตาร์ตรันครั้งแรก รหัสผ่านไม่มี ให้กดปุ่ม [Enter] ผ่านไปได้เลย</span></p>
<pre>OK, successfully used password, moving on...</pre>
<pre>Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.</pre>
<pre><span style="color: #0000ff;">Set root password? [Y/n] <strong>Y</strong></span>
<span style="color: #0000ff;">New password:</span>
<span style="color: #0000ff;">Re-enter new password:</span></pre>
<p><span style="color: #0000ff;">ต้องการเปลี่ยนรหัสผ่าน (password) ของ root (ของ mysqld) ไหม ถ้าต้องการให้พิมพ์ &#8220;Y&#8221; แล้วกดปุ่ม [Enter]</span></p>
<p><span style="color: #0000ff;">ใส่รหัสผ่านใหม่สองครั้งให้เหมือนกัน</span></p>
<pre>Password updated successfully!
Reloading privilege tables..
... Success!</pre>
<pre>By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.</pre>
<pre><span style="color: #0000ff;">Remove anonymous users? [Y/n] <strong>y</strong></span></pre>
<p><span style="color: #0000ff;">แนะนำให้ลบสิทธิการใช้งานฐานข้อมูลแบบ anonymous พิมพ์ &#8220;Y&#8221; แล้วกดปุ่ม [Enter]</span></p>
<p><span style="color: #0000ff;">หลังจากลบสิทธิ anonymous แล้ว ผู้ใช้ต้องระบุ user, password เพื่อเข้าใช้ฐานข้อมูล</span></p>
<pre>... Success!</pre>
<pre>Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.</pre>
<pre><span style="color: #0000ff;">Disallow root login remotely? [Y/n] <strong>y</strong></span></pre>
<p><span style="color: #0000ff;">แนะนำให้ปิดการล็อกอินด้วย root จากเครื่องอื่น (remote)</span></p>
<pre>... Success!</pre>
<pre>By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.</pre>
<pre><span style="color: #0000ff;">Remove test database and access to it? [Y/n] <strong>y</strong></span></pre>
<p><span style="color: #0000ff;">แนะนำให้ลบฐานข้อมูลทดสอบ test พิมพ์ &#8220;Y&#8221; แล้วกดปุ่ม [Enter]</span></p>
<pre>- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!</pre>
<pre>Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.</pre>
<pre><span style="color: #0000ff;">Reload privilege tables now? [Y/n] <strong>y</strong></span></pre>
<p><span style="color: #0000ff;">พิมพ์ &#8220;Y&#8221; แล้วกดุป่ม [Enter] เพื่อให้สิทธิและคอนฟิกใหม่ที่แก้ไขมีผล</span></p>
<pre>... Success!</pre>
<pre>Cleaning up...</pre>
<pre>All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.</pre>
<pre>Thanks for using MySQL!</pre>
<pre>[root@cent6 ~]#</pre>
<p>&nbsp;</p>
<h4>โปรเซสของ MySQL Server</h4>
<p>ใช้คำสั่ง ps และคำสั่ง egrep เพื่อดูเฉพาะโปรเซส mysqld</p>
<pre>[root@cent6 ~]# <strong>ps -ef | egrep "(UID|mysqld)"</strong>
UID        PID  PPID  C STIME TTY          TIME CMD
root      2710     1  0 23:24 pts/0    00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
mysql     2799  2710  0 23:24 pts/0    00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock</pre>
<p>&nbsp;</p>
<h4>ทดสอบด้วยโปรแกรม mysql</h4>
<p>คำสั่ง mysql เป็นโปรแกรม MySQL Client ไว้สำหรับเชื่อมต่อเข้าเซิร์ฟเวอร์ฐานข้อมูล MySQL</p>
<p>ทดลองใช้คำสั่ง mysql</p>
<pre>[root@cent6 ~]# <strong>mysql</strong>
<span style="color: #ff0000;">ERROR 1045 (28000): Access denied for user 'root'@'localhost'</span> (using password: NO)</pre>
<p>หากขึ้น ERROR แบบด้านบน หมายถึง user, password ที่ใช้ในการเชื่อมต่อเข้าฐานข้อมูลไม่ถูกต้อง</p>
<p>ระบุออปชั่น</p>
<ul>
<li><strong>-u</strong> เว้นวรรค แล้วตามด้วยชื่อ user</li>
<li><strong>-p</strong> แล้วตามด้วย password (ไม่มีเว้นวรรคคั่น) หากไม่ได้ใส่ password จะมีพร้อมต์ให้ใส่ password</li>
</ul>
<p>คำแนะนำ ด้วยเหตุผลทางด้านความปลอดภัย ไม่ให้เห็นรหัสผ่านที่พิมพ์  แนะนำให้ระบุแค่ออปชั่น &#8216;-p&#8217; แล้วใส่ password อีกที</p>
<p>ตัวอย่างเช่น</p>
<pre>[root@cent6 ~]# <strong>mysql -u root -p</strong>
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.1.52 Source distribution</pre>
<pre>Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license</pre>
<pre>Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.</pre>
<pre>mysql&gt;</pre>
<p>พิมพ์คำสั่ง quit เพื่อออกจากคำสั่ง mysql</p>
<pre>mysql&gt; <strong>quit</strong>
Bye
[root@cent6 ~]#</pre>
<p>&nbsp;</p>
<h4>คอนฟิกให้ สตาร์ต mysqld ตอนบู๊ตเครื่อง</h4>
<p>เมื่อทดสอบเรียบร้อยแล้ว หากต้องการให้สตาร์ต mysqld ตอนบู๊ตเครื่องโดยอัตโนมัติ สามารถทำได้โดยใช้คำสั่ง chkconfig</p>
<pre>[root@cent6 ~]# <strong>chkconfig mysqld on</strong></pre>
<pre>[root@cent6 ~]# <strong>chkconfig --list mysqld</strong>
mysqld          0:off   1:off  <span style="color: #0000ff;"> 2:on    3:on    4:on    5:on</span>    6:off</pre>
<p>&nbsp;</p>
<h4>ปิดเซอร์วิส mysqld</h4>
<p>ในกรณีที่ต้องการปิดฐานข้อมูล หรือปิดเซอร์วิส mysqld สามารถทำได้โดยใช้คำสั่ง service</p>
<pre>[root@cent6 ~]# <strong>service mysqld stop</strong>
Stopping mysqld:                                           [  OK  ]</pre>
<p>&nbsp;</p>
<h4>ข้อมูลอ้างอิง</h4>
<ul>
<li><a title="View Details: ติดตั้งฐานข้อมูล MySQL" href="http://spalinux.com/2012/04/install-mysql-database" rel="bookmark">ติดตั้งฐานข้อมูล MySQL</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2012/04/howto-start-apache-web-server-on-centos-6-2/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ติดตั้งฐานข้อมูล MySQL</title>
		<link>http://spalinux.com/2012/04/install-mysql-database</link>
		<comments>http://spalinux.com/2012/04/install-mysql-database#comments</comments>
		<pubDate>Sun, 01 Apr 2012 10:58:43 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[CentOS 6]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Linux Installation]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=1974</guid>
		<description><![CDATA[ในบทนี้จะสอนวิธีการติดตั้งไฟล์ rpm ฐานข้อมูล MySQL บน CentOS 6.2 โดยจะอธิบายทีละขั้นตอน ถึงวิธีการติดตั้งไฟล์ rpm และการแก้ไขปัญหา ใส่แผ่นดีวีดีติดตั้ง ใช้คำสั่ง mount แผ่นกับพาธ /mnt [root@cent6 ~]# mount /dev/cdrom /mnt mount: block device /dev/sr0 is write-protected, mounting read-only เข้าไปใน /mnt/Packages/ เพื่อติดตั้งไฟล์ rpm สำหรับ CentOS ไฟล์ rpm ของ MySQL จะมีชื่อไฟล์ขึ้นต้นด้วย mysql [root@cent6 ~]# cd /mnt/Packages/ [root@cent6 Packages]# ls -l mysql-* -rw-r--r--. 2 500 500 910096 [...]]]></description>
			<content:encoded><![CDATA[<p>ในบทนี้จะสอนวิธีการติดตั้งไฟล์ rpm ฐานข้อมูล MySQL บน CentOS 6.2</p>
<p>โดยจะอธิบายทีละขั้นตอน ถึงวิธีการติดตั้งไฟล์ rpm และการแก้ไขปัญหา</p>
<p><span id="more-1974"></span></p>
<p>ใส่แผ่นดีวีดีติดตั้ง ใช้คำสั่ง mount แผ่นกับพาธ /mnt</p>
<pre>[root@cent6 ~]# <strong>mount /dev/cdrom /mnt</strong>
mount: block device /dev/sr0 is write-protected, mounting read-only</pre>
<p>เข้าไปใน /mnt/Packages/ เพื่อติดตั้งไฟล์ rpm</p>
<p>สำหรับ CentOS ไฟล์ rpm ของ MySQL จะมีชื่อไฟล์ขึ้นต้นด้วย mysql</p>
<pre>[root@cent6 ~]# <strong>cd /mnt/Packages/</strong>
[root@cent6 Packages]# <strong>ls -l mysql-*</strong>
-rw-r--r--. 2 500 500 910096 Jul 6 2011 mysql-5.1.52-1.el6_0.1.x86_64.rpm
-rw-r--r--. 2 500 500 436268 Jul 6 2011 mysql-bench-5.1.52-1.el6_0.1.x86_64.rpm
-rw-r--r--. 2 500 500 2202512 Jul 3 2011 mysql-connector-java-5.1.12-2.el6.x86_64.rpm
-rw-r--r--. 2 500 500 116916 Jul 3 2011 mysql-connector-odbc-5.1.5r1144-7.el6.x86_64.rpm
-rw-r--r--. 4 500 500 128868 Jul 6 2011 mysql-devel-5.1.52-1.el6_0.1.i686.rpm
-rw-r--r--. 2 500 500 128840 Jul 6 2011 mysql-devel-5.1.52-1.el6_0.1.x86_64.rpm
-rw-r--r--. 4 500 500 1291304 Jul 6 2011 mysql-libs-5.1.52-1.el6_0.1.i686.rpm
-rw-r--r--. 2 500 500 1280936 Jul 6 2011 mysql-libs-5.1.52-1.el6_0.1.x86_64.rpm
-rw-r--r--. 2 500 500 8492548 Jul 6 2011 mysql-server-5.1.52-1.el6_0.1.x86_64.rpm
-rw-r--r--. 2 500 500 5340912 Jul 6 2011 mysql-test-5.1.52-1.el6_0.1.x86_64.rpm</pre>
<p>ไฟล์ rpm ของ MySQL จะถูกแบ่งเป็นไฟล์ย่อยๆ เช่นเดียวกับโปรแกรมอื่นๆ ที่สำคัญจะมีสองไฟล์</p>
<ul>
<li>mysql รวมไฟล์เกี่ยวกับโปรแกรม MySQL Client</li>
<li>mysql-server รวมไฟล์เกี่ยวกับโปรแกรมที่รันเป็น MySQL Server</li>
</ul>
<p>&nbsp;</p>
<h4>ติดตั้ง MySQL Client</h4>
<p>ติดตั้งไฟล์ rpm ชื่อ mysql</p>
<pre>[root@cent6 Packages]# <strong>rpm -i mysql-5.1.52-1.el6_0.1.x86_64.rpm</strong></pre>
<p>ทดลองรันโปรแกรม mysql</p>
<pre>[root@cent6 Packages]# <strong>mysql -V</strong>
mysql Ver 14.14 Distrib 5.1.52, for unknown-linux-gnu (x86_64) using readline 5.1</pre>
<p>&nbsp;</p>
<h4>ติดตั้ง MySQL Server</h4>
<p>ติดตั้งไฟล์ rpm ชื่อ mysql-server</p>
<pre>[root@cent6 Packages]# <strong>rpm -i mysql-server-5.1.52-1.el6_0.1.x86_64.rpm</strong>
<span style="color: #ff0000;">error: Failed dependencies</span>:
 perl(DBI) is needed by mysql-server-5.1.52-1.el6_0.1.x86_64
 perl-DBD-MySQL is needed by mysql-server-5.1.52-1.el6_0.1.x86_64
 perl-DBI is needed by mysql-server-5.1.52-1.el6_0.1.x86_64</pre>
<p>ต้องติดตั้งไฟล์ rpm อื่นๆ ก่อน</p>
<p>ติดตั้งไฟล์ perl-DBI</p>
<pre>[root@cent6 Packages]# <strong>rpm -i perl-DBI-1.609-4.el6.x86_64.rpm</strong></pre>
<p>ติดตั้งไฟล์ perl-DBD-MySQL</p>
<pre>[root@cent6 Packages]# <strong>rpm -i perl-DBD-MySQL-4.013-3.el6.x86_64.rpm</strong></pre>
<p>ลองติดตั้ง mysql-server อีกครั้ง</p>
<pre>[root@cent6 Packages]# <strong>rpm -i mysql-server-5.1.52-1.el6_0.1.x86_64.rpm</strong></pre>
<p>&nbsp;</p>
<h4>ข้อมูลอ้างอิง</h4>
<ul>
<li><a title="View Details: ติดตั้งโปรแกรมเพิ่มบน CentOS 6.2" href="http://spalinux.com/2012/04/how-to-install-program-on-centos-6-2" rel="bookmark">ติดตั้งโปรแกรมเพิ่มบน CentOS 6.2</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2012/04/install-mysql-database/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>หัดใช้ PostgreSQL</title>
		<link>http://spalinux.com/2011/01/learn_to_use_postgresql</link>
		<comments>http://spalinux.com/2011/01/learn_to_use_postgresql#comments</comments>
		<pubDate>Sun, 30 Jan 2011 13:31:00 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[PostgreSQL]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=1249</guid>
		<description><![CDATA[ช่วงนี้เหตุจำเป็นต้องใช้ฐานข้อมูล PostgreSQL  เลยลองติดตั้ง และหัดใช้กันซะหน่อย เห็นว่าน่าจะมีประโยชน์จึงมาเผยแพร่ให้อ่านกัน เนื่องจากคุ้นเคยกับ MySQL มานาน ในแต่ละหัวข้อจะเปรียบเทียบกับคำสั่ง mysql กรุณาสังเกต prompt ที่รันคำสั่ง จะมีอยู่ 2 โหมด คือ UNIX shell และ postgres ที่เข้าได้ด้วยคำสั่ง psql ติดตั้ง postgresql จากไฟล์ rpm บทความนี้ทดสอบบน Fedora 14 ติดตั้งจากไฟล์ rpm ที่อยู่ในแผ่นติดตั้ง Fedora 14 [root@fc14-64a ~]# mount /dev/cdrom /media mount: block device /dev/sr0 is write-protected, mounting read-only ไฟล์ที่ต้องติดตั้งมี 3 ไฟล์ ดังนี้ [root@fc14-64a ~]# cd [...]]]></description>
			<content:encoded><![CDATA[<p>ช่วงนี้เหตุจำเป็นต้องใช้ฐานข้อมูล PostgreSQL  เลยลองติดตั้ง และหัดใช้กันซะหน่อย เห็นว่าน่าจะมีประโยชน์จึงมาเผยแพร่ให้อ่านกัน</p>
<p>เนื่องจากคุ้นเคยกับ MySQL มานาน ในแต่ละหัวข้อจะเปรียบเทียบกับคำสั่ง mysql</p>
<p>กรุณาสังเกต prompt ที่รันคำสั่ง จะมีอยู่ 2 โหมด คือ UNIX shell และ postgres ที่เข้าได้ด้วยคำสั่ง psql</p>
<p><span id="more-1249"></span></p>
<h4>ติดตั้ง postgresql จากไฟล์ rpm</h4>
<p>บทความนี้ทดสอบบน Fedora 14</p>
<p>ติดตั้งจากไฟล์ rpm ที่อยู่ในแผ่นติดตั้ง Fedora 14</p>
<pre>[root@fc14-64a ~]# <strong>mount /dev/cdrom /media
</strong>mount: block device /dev/sr0 is write-protected, mounting read-only</pre>
<p>ไฟล์ที่ต้องติดตั้งมี 3 ไฟล์ ดังนี้</p>
<pre>[root@fc14-64a ~]# <strong>cd /media/Packages/
</strong>[root@fc14-64a Packages]# <strong>ls -l postgresql-*
</strong>-rw-r--r-- 6 root 101737 2804052 Oct  6 22:31 postgresql-8.4.5-1.fc14.x86_64.rpm
-rw-r--r-- 6 root 101737  197124 Oct  6 22:26 postgresql-libs-8.4.5-1.fc14.x86_64.rpm
-rw-r--r-- 6 root 101737 3409060 Oct  6 22:31 postgresql-server-8.4.5-1.fc14.x86_64.rpm</pre>
<p>ใช้คำสั่ง rpm เพื่อติดตั้ง</p>
<pre>[root@fc14-64a Packages]# <strong>rpm -i postgresql-libs-8.4.5-1.fc14.x86_64.rpm
</strong>[root@fc14-64a Packages]# <strong>rpm -i postgresql-8.4.5-1.fc14.x86_64.rpm
</strong>[root@fc14-64a Packages]# <strong>rpm -i postgresql-server-8.4.5-1.fc14.x86_64.rpm</strong></pre>
<pre>[root@fc14-64a ~]# <strong>umount /media</strong></pre>
<h4>รันเซอร์วิส postgresql (server)</h4>
<p>ใช้คำสั่ง service เพื่อรัน postgresql</p>
<pre>[root@fc14-64a ~]# <strong>service postgresql start</strong></pre>
<pre>/var/lib/pgsql/data is missing. Use "service postgresql initdb" to initialize the cluster first.
                                                           [<span style="color: #ff0000;">FAILED</span>]</pre>
<p>ไม่สามารถรันได้ เพราะยังไม่มีไฟล์ที่จำเป็นของฐานข้อมูล</p>
<pre>[root@fc14-64a ~]# ls -l /var/lib/pgsql/data/
total 0</pre>
<p>สร้างไฟล์ฐานข้อมูลเริ่มต้นด้วยคำสั่ง service initdb</p>
<pre>[root@fc14-64a ~]# <strong>service postgresql initdb
</strong>Initializing database:                                     [  OK  ]</pre>
<p>หลังจากรัน initdb จะมีไฟล์ถูกสร้างขึ้นในไดเรคทอรี /var/lib/pgsql/data/</p>
<pre>[root@fc14-64a ~]# <strong>ls -l /var/lib/pgsql/data/
</strong>total 36
drwx------ 5 postgres postgres    38 Jan 30 17:29 base
drwx------ 2 postgres postgres  4096 Jan 30 17:29 global
drwx------ 2 postgres postgres    17 Jan 30 17:29 pg_clog
-rw------- 1 postgres postgres  3411 Jan 30 17:29 pg_hba.conf
-rw------- 1 postgres postgres  1631 Jan 30 17:29 pg_ident.conf
drwx------ 2 postgres postgres     6 Jan 30 17:29 pg_log
drwx------ 4 postgres postgres    34 Jan 30 17:29 pg_multixact
drwx------ 2 postgres postgres     6 Jan 30 17:29 pg_stat_tmp
drwx------ 2 postgres postgres    17 Jan 30 17:29 pg_subtrans
drwx------ 2 postgres postgres     6 Jan 30 17:29 pg_tblspc
drwx------ 2 postgres postgres     6 Jan 30 17:29 pg_twophase
-rw------- 1 postgres postgres     4 Jan 30 17:29 PG_VERSION
drwx------ 3 postgres postgres    58 Jan 30 17:29 pg_xlog
-rw------- 1 postgres postgres 16867 Jan 30 17:29 postgresql.conf</pre>
<p>ใช้คำสั่ง service เพื่อรันเซอร์วิส postgresql อีกครั้ง</p>
<pre>[root@fc14-64a ~]# <strong>service postgresql start
</strong>Starting postgresql service:                               [  OK  ]</pre>
<p>ถึงตอนนี้เราก็ได้ postgresql server ไว้ลองใช้งานแล้ว ใช้คำสั่ง ps เพื่อดูโพรเซสของ postgresql</p>
<pre>[root@fc14-64a ~]# <strong>ps -ef | grep postgres
</strong>postgres  1101     1  2 17:29 ?        00:00:00 /usr/bin/postmaster -p 5432 -D /var/lib/pgsql/data
postgres  1103  1101  0 17:29 ?        00:00:00 postgres: logger process
postgres  1105  1101  0 17:29 ?        00:00:00 postgres: writer process
postgres  1106  1101  0 17:29 ?        00:00:00 postgres: wal writer process
postgres  1107  1101  0 17:29 ?        00:00:00 postgres: autovacuum launcher process
postgres  1108  1101  0 17:29 ?        00:00:00 postgres: stats collector process</pre>
<h4>คำสั่ง psql (client)</h4>
<p>ส่วนโปรแกรมฝั่ง client ต้องใช้คำสั่ง psql เพื่อติดต่อกับ postgresql server</p>
<p>ลองรันคำสั่ง psql ด้วย root จะขึ้น error ไม่สามารถเข้า database ได้</p>
<pre>[root@fc14-64a ~]# <strong>psql</strong>
psql: FATAL:  Ident authentication failed for user "root"</pre>
<p>ใช้คำสั่ง su เปลี่ยนเป็น postgres แล้วรันคำสั่ง psql อีกที จะเข้าโหมด client ได้</p>
<pre>[root@fc14-64a ~]# <strong>su - postgres</strong></pre>
<pre>-bash-4.1$ <strong>id</strong>
uid=26(postgres) gid=26(postgres) groups=26(postgres)</pre>
<pre>-bash-4.1$ <strong>psql
</strong>psql (8.4.5)
Type "help" for help.</pre>
<p>พิมพ์ &#8221;\h&#8221; หรือ help เพื่อดูวิธีการใช้งานเบื้องต้น  พิมพ์ &#8220;\q&#8221; เพื่อออกจาก postgres กลับสู่ shell</p>
<p>เครื่องหมาย prompt # แสดงถึงการเข้าด้วย database superuser</p>
<pre>postgres=# <strong>help</strong>
You are using psql, the command-line interface to PostgreSQL.
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit
postgres=# \q</pre>
<p>-bash-4.1$</p>
<h4>SHOW DATABASES</h4>
<p>ในโหมด postgres พิมพ์ &#8220;\l&#8221; หรือ &#8220;SELECT * FROM pg_database;&#8221; เพื่อแสดง database ที่มีอยู่ หรือ</p>
<pre>-bash-4.1$ <strong>psql
</strong>psql (8.4.5)
Type "help" for help.</pre>
<pre>postgres-# <strong>\l
</strong>                                  List of databases
   Name    |  Owner   | Encoding |  Collation  |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
                                                             : postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
                                                             : postgres=CTc/postgres
(3 rows)
postgres=#</pre>
<pre>postgres=# <strong>SELECT * FROM pg_database;
</strong>  datname  | datdba | encoding | datcollate  |  datctype   | datistemplate | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | dattablespace | datconfig |
datacl
-----------+--------+----------+-------------+-------------+---------------+--------------+--------------+---------------+--------------+---------------+-----------+---------------
----------------------
 template1 |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | t             | t            |           -1 |         11563 |          648 |          1663 |           | {=c/postgres,p
ostgres=CTc/postgres}
 template0 |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | t             | f            |           -1 |         11563 |          648 |          1663 |           | {=c/postgres,p
ostgres=CTc/postgres}
 postgres  |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         11563 |          648 |          1663 |           |
(3 rows)</pre>
<pre>postgres=#</pre>
<p> </p>
<h4>CREATE DATABASE</h4>
<p>การสร้าง database ใน postgresql ทำได้ด้วยคำสั่งบน shell คือ &#8220;createdb&#8221; ตามด้วยชื่อที่ต้องการสร้าง</p>
<p>ตัวอย่างเช่น ต้องการสร้างฐานข้อมูลชื่อ &#8220;testdb&#8221;</p>
<pre>-bash-4.1$ <strong>createdb testdb</strong></pre>
<p>แสดงชื่อฐานข้อมูลที่สร้าง</p>
<pre>-bash-4.1$ <strong>psql</strong>
psql (8.4.5)
Type "help" for help.</pre>
<pre>postgres=# <strong>\l
</strong>                                  List of databases
   Name    |  Owner   | Encoding |  Collation  |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
                                                             : postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
                                                             : postgres=CTc/postgres
 <span style="color: #0000ff;">testdb    </span>| postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)</pre>
<h4>CREATE TABLE</h4>
<p>บน shell พิมพ์คำสั่ง psql ตามด้วยชื่อ database เพื่อเข้าสู่ชื่อฐานข้อมูล สังเกตว่า prompt จะขึ้นชื่อ database ที่ใช้อยู่</p>
<pre>-bash-4.1$ <strong>psql testdb
</strong>psql (8.4.5)
Type "help" for help.</pre>
<pre>testdb=#</pre>
<p>ทดลองสร้าง table ชื่อ weather  รูปแบบก็เป็นมาตรฐาน SQL ทั่วไป</p>
<pre>testdb=# <strong>CREATE TABLE weather (
</strong>testdb(#     <strong>city            varchar(80),
</strong>testdb(#     <strong>temp_lo         int,           -- low temperature
</strong>testdb(#     <strong>temp_hi         int,           -- high temperature
</strong>testdb(#     <strong>prcp            real,          -- precipitation
</strong>testdb(#     <strong>date            date
</strong>testdb(# <strong>);
</strong>CREATE TABLE
testdb=#</pre>
<p>พิมพ์ &#8220;\d&#8221; หรือ &#8220;SELECT * FROM pg_tables WHERE schemaname=&#8217;public&#8217;;&#8221; เพื่อแสดงชื่อ table ที่มีอยู่</p>
<pre>testdb=# <strong>\d
</strong>          List of relations
 Schema |  Name   | Type  |  Owner
--------+---------+-------+----------
 public | <span style="color: #0000ff;">weather</span> | table | postgres
(1 row)</pre>
<pre>testdb=# <strong>SELECT * FROM pg_tables WHERE schemaname='public';
</strong> schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers
------------+-----------+------------+------------+------------+----------+-------------
 public     | <span style="color: #0000ff;">weather</span>   | postgres   |            | f          | f        | f
(1 row)</pre>
<p>ลองใส่ข้อมูลเข้า table ด้วย คำสั่ง INSERT และดึงข้อมูลด้วยคำสั่ง SELECT</p>
<pre>testdb=# <strong>INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');
</strong>INSERT 0 1</pre>
<pre>testdb=# <strong>SELECT * FROM weather;
</strong>     city      | temp_lo | temp_hi | prcp |    date
---------------+---------+---------+------+------------
 San Francisco |      46 |      50 | 0.25 | 1994-11-27
(1 row)</pre>
<h4>DROP TABLE</h4>
<p>ลบ table ด้วยคำสั่ง &#8220;DROP TABLE&#8221;</p>
<pre>testdb=# <strong>DROP TABLE weather;
</strong>DROP TABLE</pre>
<pre>testdb=# <strong>SELECT * FROM weather;
</strong>ERROR:  relation "weather" does not exist
LINE 1: SELECT * FROM weather;
                      ^
testdb=# <strong>\d
</strong>No relations found.</pre>
<h4>DROP DATABASE</h4>
<p>ลบ database ด้วยคำสั่ง shell &#8220;dropdb&#8221;</p>
<pre>-bash-4.1$ <strong>dropdb testdb</strong></pre>
<h4>ข้อมูลอ้างอิง</h4>
<ul>
<li><a href="http://www.postgresql.org/" target="_blank">PostgreSQL: Documentation</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2011/01/learn_to_use_postgresql/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>เปลี่ยน MySQL Prompt</title>
		<link>http://spalinux.com/2010/07/change_mysql_prompt</link>
		<comments>http://spalinux.com/2010/07/change_mysql_prompt#comments</comments>
		<pubDate>Fri, 02 Jul 2010 08:17:18 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[mysql prompt]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=1115</guid>
		<description><![CDATA[เริ่มมีหลายฐานข้อมูลมากขึ้น เวลาใช้คำสั่ง mysql เพื่อจัดการฐานข้อมูลอาจงงๆ ว่าตอนนี้แก้ไขฐานข้อมูลใดอยู่ การเปลี่ยน prompt ในคำสั่ง mysql จะช่วยคุณได้ วิธีง่ายๆ คือตั้งค่าตัวแปรใน shell ที่ชื่อ MYSQL_PS1 ก่อนที่จะรันคำสั่ง ซึ่งวิธีการตั้งค่าจะแตกต่างกันในแต่ละ shell สำหรับ BASH shell ดีฟอลต์ของลีนุกซ์ส่วนใหญ่ทำได้โดย $ export MYSQL_PS1="\u@\d&#62; " $ mysql -u root -p Enter password: root@(none)&#62; root@(none)&#62; connect project1 root@project1&#62; สังเกตว่า prompt จะเปลี่ยนไปตามชื่อ user &#8220;\u&#8221; กับชื่อฐานข้อมูล &#8220;\d&#8221; ที่ใช้ หรือถ้าสามารถแก้ไขไฟล์ /etc/my.cnf ได้ ก็สามารถทำได้โดยเพิ่มคอนฟิก prompt ลงไปในส่วนที่คอนฟิกของ [mysql] [mysql] prompt="\u@\d&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>เริ่มมีหลายฐานข้อมูลมากขึ้น เวลาใช้คำสั่ง mysql เพื่อจัดการฐานข้อมูลอาจงงๆ ว่าตอนนี้แก้ไขฐานข้อมูลใดอยู่ การเปลี่ยน prompt ในคำสั่ง mysql จะช่วยคุณได้</p>
<p><span id="more-1115"></span></p>
<p>วิธีง่ายๆ คือตั้งค่าตัวแปรใน shell ที่ชื่อ MYSQL_PS1 ก่อนที่จะรันคำสั่ง ซึ่งวิธีการตั้งค่าจะแตกต่างกันในแต่ละ shell สำหรับ BASH shell ดีฟอลต์ของลีนุกซ์ส่วนใหญ่ทำได้โดย</p>
<pre>$ <strong>export MYSQL_PS1="\u@\d&gt; "</strong></pre>
<pre>$ <strong>mysql -u root -p
</strong>Enter password:</pre>
<pre>root@(none)&gt;
root@(none)&gt; <strong>connect project1
</strong>root@project1&gt;</pre>
<p>สังเกตว่า prompt จะเปลี่ยนไปตามชื่อ user &#8220;\u&#8221; กับชื่อฐานข้อมูล &#8220;\d&#8221; ที่ใช้</p>
<p>หรือถ้าสามารถแก้ไขไฟล์ /etc/my.cnf ได้ ก็สามารถทำได้โดยเพิ่มคอนฟิก prompt ลงไปในส่วนที่คอนฟิกของ [mysql]</p>
<pre>[mysql]
prompt="\u@\d&gt; "</pre>
<p>มีค่าอื่นๆ ที่สามารถตั้งเป็น prompt ได้ ดูรายละเอียดเพิ่มเติมในส่วนข้อมูลอ้างอิง</p>
<h4>ข้อมูลอ้างอิง</h4>
<ul>
<li><a href="http://dev.mysql.com/doc/refman/5.1/en/mysql-commands.html" target="_blank">MySQL 5.1 Reference Manual :: mysql Commands</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2010/07/change_mysql_prompt/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>คอนฟิก MySQL Replication</title>
		<link>http://spalinux.com/2010/02/configure_mysql_replication</link>
		<comments>http://spalinux.com/2010/02/configure_mysql_replication#comments</comments>
		<pubDate>Fri, 26 Feb 2010 09:20:58 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[Backup]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[mysql replication]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=1084</guid>
		<description><![CDATA[บทความนี้จะอธิบายวิธีการคอนฟิก Replication ของ MySQL เพื่อทำการ replicate ข้อมูลในฐานข้อมูล (database) จากเครื่องหลัก (Master) ไปยังเครื่องสำรอง (Slave) ได้ หลังจากที่คอนฟิก Replication ถูกต้องเรียบร้อย เมื่อมีการเปลี่ยนแปลงข้อมูลในฐานข้อมูลบนเครื่องหลัก (Master) ไม่ว่าจะเป็นการ INSERT, UPDATE, DELETE หรือ แก้ไข table การเปลี่ยนแปลงนี้จะถูกส่งต่อ (replicate) ไปยังเครื่องสำรอง (Slave) โดยอัตโนมัติ การคอนฟิก Replication เป็นการป้องกันการเสียหาย ถ้าเครื่องหลักมีปัญหา ข้อมูลที่ถูกเก็บไว้ในเครื่องสำรอง ก็ยังสามารถใช้งานได้ ระบบทดสอบ ตัวอย่างในบทความนี้ จะทดสอบบนเครื่องสองเครื่องที่ติดตั้ง CentOS 5.4 และ MySQL 5.1.43 เรียบร้อยแล้ว เครื่อง Master ip: 192.168.1.1 hostname: centos54-a เครื่อง Slave ip: 192.168.1.2 [...]]]></description>
			<content:encoded><![CDATA[<p>บทความนี้จะอธิบายวิธีการคอนฟิก Replication ของ MySQL เพื่อทำการ replicate ข้อมูลในฐานข้อมูล (database) จากเครื่องหลัก (Master) ไปยังเครื่องสำรอง (Slave) ได้</p>
<p>หลังจากที่คอนฟิก Replication ถูกต้องเรียบร้อย เมื่อมีการเปลี่ยนแปลงข้อมูลในฐานข้อมูลบนเครื่องหลัก (Master) ไม่ว่าจะเป็นการ INSERT, UPDATE, DELETE หรือ แก้ไข table การเปลี่ยนแปลงนี้จะถูกส่งต่อ (replicate) ไปยังเครื่องสำรอง (Slave) โดยอัตโนมัติ</p>
<p>การคอนฟิก Replication เป็นการป้องกันการเสียหาย ถ้าเครื่องหลักมีปัญหา ข้อมูลที่ถูกเก็บไว้ในเครื่องสำรอง ก็ยังสามารถใช้งานได้</p>
<p><span id="more-1084"></span></p>
<h4>ระบบทดสอบ</h4>
<p>ตัวอย่างในบทความนี้ จะทดสอบบนเครื่องสองเครื่องที่ติดตั้ง CentOS 5.4 และ MySQL 5.1.43 เรียบร้อยแล้ว</p>
<p>เครื่อง <strong>Master</strong></p>
<ul>
<li>ip: 192.168.1.1</li>
<li>hostname: centos54-a</li>
</ul>
<p>เครื่อง <strong>Slave</strong></p>
<ul>
<li>ip: 192.168.1.2</li>
<li>hostname: centos54-b</li>
</ul>
<p>ตัวอย่างการใช้คำสั่ง rpm เพื่อตรวจสอบเวอร์ชั่นของ MySQL ที่ติดตั้ง</p>
<pre>[root@centos54-a ~]# <strong>rpm -qa | grep -i mysql
</strong>MySQL-server-community-5.1.43-1.rhel5
MySQL-client-community-5.1.43-1.rhel5
MySQL-shared-compat-5.1.43-1.rhel5</pre>
<p>หมายเหตุ</p>
<ul>
<li>MySQL 5.1.43 ที่ทดสอบนี้ ใช้เวอร์ชั่นที่ดาวน์โหลดจาก <a href="http://dev.mysql.com">http://dev.mysql.com</a></li>
<li>แนะนำให้ใช้ MySQL เวอร์ชั่นเดียวกันบนเครื่อง Master และ Slave</li>
</ul>
<h4>เครื่อง Master &#8211; สร้าง user สำหรับ Replication</h4>
<p>เริ่มต้น ต้องสร้าง user ของ MySQL บนเครื่อง Master เพื่ออนุญาตสิทธิในการทำ &#8216;REPLICATION&#8217; จากเครื่อง Slave</p>
<p>ตัวอย่างการสร้าง user ชื่อ &#8216;repl&#8217; และรหัส &#8216;repl1234&#8242;เพื่อให้สิทธิการทำ REPLICATION จากเครื่องที่มี ip 192.168.1.2</p>
<pre>[root@centos54-a ~]# <strong>mysql -u root -p</strong></pre>
<pre>mysql&gt; <strong>GRANT REPLICATION SLAVE ON *.* TO '<span style="color: #0000ff;">repl</span>'@'<span style="color: #0000ff;">192.168.1.2</span>' IDENTIFIED BY '<span style="color: #0000ff;">repl1234</span>';</strong></pre>
<h4>เครื่อง Master &#8211; คอนฟิก Replication Master</h4>
<p>บนเครื่อง Master ปิดเซอร์วิส MySQL แล้วแก้ไขไฟล์ /etc/my.cnf เพื่อเปิดคุณสมบัติ binary log (log-bin) และเซ็ต server-id</p>
<pre>[root@centos54-a ~]# <strong>service mysql stop
</strong>Shutting down MySQL..                                      [  OK  ]</pre>
<p>ตัวอย่างไฟล์คอนฟิก /etc/my.cnf เพื่อคอนฟิกเป็น Replication Master</p>
<pre>[root@centos54-a ~]# <strong>cat /etc/my.cnf
</strong>[mysqld]
log-bin=mysql-bin
server-id=1</pre>
<p>ใช้คำสั่ง service เพื่อรันเซอร์วิส MySQL</p>
<pre>[root@centos54-a ~]# <strong>service mysql start
</strong>Starting MySQL.                                            [  OK  ]</pre>
<h4>เครื่อง Master &#8211; ตรวจสอบสถานะของ Master Binary Log</h4>
<p>บนเครื่อง Master ใช้คำสั่ง SHOW MASTER STATUS ตรวจสอบสถานะของ Master Binary Log</p>
<p>เราจะใช้ค่า &#8216;File&#8217; และ &#8216;Position&#8217; เพื่อระบุบนเครื่อง Slave</p>
<pre>[root@centos54-a ~]# <strong>mysql -u root -p</strong></pre>
<pre>mysql&gt; <strong>SHOW MASTER STATUS;
</strong>+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| <strong><span style="color: #0000ff;">mysql-bin.000001</span></strong> |      <strong><span style="color: #0000ff;">224</span> </strong>|              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)</pre>
<h4>เครื่อง Slave &#8211; คอนฟิก Replication Slave</h4>
<p>บนเครื่อง Slave ปิดเซอร์วิส MySQL เพื่อแก้ไขคอนฟิกไฟล์ /etc/my.cnf</p>
<pre>[root@centos54-b ~]# <strong>service mysql stop
</strong>Shutting down MySQL..                                      [  OK  ]</pre>
<p>เบื้องต้นบนเครื่อง Slave คอนฟิกแค่ &#8216;server-id&#8217; ที่มีค่าแตกต่างจากเครื่อง Master</p>
<pre>[root@centos54-b ~]# <strong>cat /etc/my.cnf
</strong>[mysqld]
server-id=2</pre>
<p>รันเซอร์วิส MySQL บนเครื่อง Slave</p>
<pre>[root@centos54-b ~]# <strong>service mysql start
</strong>Starting MySQL.                                            [  OK  ]</pre>
<p>ใช้คำสั่ง CHANGE MASTER เพื่อระบุ ip ของ Master ชื่อ user, password ที่ใช้เพื่อการทำ Replication และต้องระบุค่า log_file (File) และ log_pos (Position) ที่ได้จากผลลัพธ์ในการรันคำสั่ง SHOW MASTER STATUS บนเครื่อง Master</p>
<pre>mysql&gt; <strong>CHANGE MASTER TO MASTER_HOST='<span style="color: #0000ff;">192.168.1.1</span>',
                        MASTER_USER='<span style="color: #0000ff;">repl</span>',
                        MASTER_PASSWORD='<span style="color: #0000ff;">repl1234</span>',
                        MASTER_LOG_FILE='<span style="color: #0000ff;">mysql-bin.000001</span>',
                        MASTER_LOG_POS=<span style="color: #0000ff;">224</span>;
</strong>Query OK, 0 rows affected (0.03 sec)</pre>
<p>รันคำสั่ง START SLAVE เพื่อเริ่มต้นการทำ Replicate ข้อมูลจาก เครื่อง Master</p>
<pre>mysql&gt; <strong>START SLAVE;
</strong>Query OK, 0 rows affected (0.00 sec)</pre>
<p>ตรวจสอบสถานะ Replication บนเครื่อง Slave</p>
<pre>mysql&gt; <strong>SHOW SLAVE STATUS \G
</strong>*************************** 1. row ***************************
               <span style="color: #0000ff;">Slave_IO_State: Waiting for master to send event
</span>                  Master_Host: <span style="color: #0000ff;">192.168.1.1
</span>                  Master_User: <span style="color: #0000ff;">repl</span>
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: <span style="color: #0000ff;">mysql-bin.000001
</span>          Read_Master_Log_Pos: <span style="color: #0000ff;">244</span>
               Relay_Log_File: centos54-b-relay-bin.000004
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 244
              Relay_Log_Space: 411
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
1 row in set (0.00 sec)</pre>
<p>หากคอนฟิกถูกต้อง ค่า &#8216;Slave_IO_State&#8217; จะแสดงเป็น &#8216;Waiting for master to send event&#8217;</p>
<h4>ทดสอบการ Replication</h4>
<p>เพื่อง่ายต่อการแยกแยะระหว่าง Master และ Slave ในที่นี้จะคอนฟิก mysql prompt เป็น mysql-1&gt; และ mysql-2&gt; ตามลำดับ</p>
<p>การคอนฟิก prompt บนเครื่อง Master</p>
<pre>[root@centos54-a ~]# <strong>mysql -u root -p
</strong>Enter password:</pre>
<pre>mysql&gt; <strong>PROMPT mysql-1&gt;
</strong>PROMPT set to 'mysql-1&gt; '
<span style="color: #0000ff;">mysql-1&gt;</span></pre>
<p>การคอนฟิก prompt บนเครื่อง Slave</p>
<pre>[root@centos54-b ~]# <strong>mysql -u root -p
</strong>Enter password:</pre>
<pre>mysql&gt; <strong>PROMPT mysql-2&gt;
</strong>PROMPT set to 'mysql-2&gt; '
<span style="color: #0000ff;">mysql-2&gt;</span></pre>
<p>ทดสอบสร้าง database บนเครื่อง Master</p>
<pre>mysql-1&gt; <strong>CREATE DATABASE <span style="color: #0000ff;">test1</span>;
</strong>Query OK, 1 row affected (0.01 sec)</pre>
<pre>mysql-1&gt; <strong>SHOW DATABASES;
</strong>+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| <span style="color: #0000ff;">test1</span>              |
+--------------------+
3 rows in set (0.00 sec)</pre>
<p>ข้อมูลการสร้าง database จะถูกส่งต่อ (replicate) ไปยังเครื่อง Slave โดยอัตโนมัติ</p>
<pre>mysql-2&gt; <strong>SHOW DATABASES;
</strong>+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| <span style="color: #0000ff;">test1</span>              |
+--------------------+
3 rows in set (0.00 sec)</pre>
<p>สร้าง table และทดสอบการ INSERT ข้อมูล</p>
<pre>mysql-1&gt; <strong>CONNECT test1
</strong>mysql-1&gt; <strong>CREATE TABLE books (id INTEGER, name VARCHAR(255));
</strong>mysql-1&gt; <strong>INSERT INTO books VALUES (1, 'how to setup...');
</strong>mysql-1&gt; <strong>INSERT INTO books VALUES (2, 'how to read...');</strong></pre>
<pre>mysql-1&gt; <strong>SELECT * FROM books;
</strong>+------+-----------------+
| id   | name            |
+------+-----------------+
|    1 | how to setup... |
|    2 | how to read...  |
+------+-----------------+
2 rows in set (0.00 sec)</pre>
<p>การเปลี่ยนแปลงบนเครื่อง Master จะถูก replicate ไปยังเครื่อง Slave โดยอัตโนมัติ</p>
<pre>mysql-2&gt; <strong>CONNECT test1;
</strong>mysql-2&gt; <strong>SELECT * FROM books;
</strong>+------+-----------------+
| id   | name            |
+------+-----------------+
|    1 | how to setup... |
|    2 | how to read...  |
+------+-----------------+
2 rows in set (0.00 sec)</pre>
<h4>ข้อมูลอ้างอิง</h4>
<ul>
<li><a href="http://dev.mysql.com/doc/refman/5.1/en/replication.html" target="_blank">MySQL 5.1 Reference Manual :: Replication</a></li>
<li><a href="http://spalinux.com/2009/02/install_mysql_51_on_fedora_10">ติดตั้ง MySQL 5.1 บน Fedora 10</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2010/02/configure_mysql_replication/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ทดสอบ FOREIGN KEY Constraints ใน MySQL</title>
		<link>http://spalinux.com/2010/01/testing_mysql_foreign_key_constraints</link>
		<comments>http://spalinux.com/2010/01/testing_mysql_foreign_key_constraints#comments</comments>
		<pubDate>Sat, 23 Jan 2010 10:13:04 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[mysql constraints]]></category>
		<category><![CDATA[mysql foreign key]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=1061</guid>
		<description><![CDATA[บทความนี้ต่อจาก การสร้าง FOREIGN KEY Constraints ใน MySQL เพื่อทดสอบการ เพิ่ม แก้ไข ลบ ข้อมูล โดยในเวลาเริ่มแต่ละหัวข้อจะใช้ข้อมูลนี้เป็นหลัก mysql&#62; SELECT * FROM groups; +----------+------------+ &#124; group_id &#124; group_name &#124; +----------+------------+ &#124;      101 &#124; Accounting &#124; &#124;      102 &#124; Engineer   &#124; &#124;      103 &#124; IT         &#124; &#124;      104 &#124; Manager    &#124; +----------+------------+ 4 rows in set (0.00 sec) mysql&#62; SELECT * FROM [...]]]></description>
			<content:encoded><![CDATA[<p>บทความนี้ต่อจาก <a href="http://spalinux.com/2010/01/creating_mysql_foreign_key_constraints">การสร้าง FOREIGN KEY Constraints ใน MySQL</a> เพื่อทดสอบการ เพิ่ม แก้ไข ลบ ข้อมูล โดยในเวลาเริ่มแต่ละหัวข้อจะใช้ข้อมูลนี้เป็นหลัก</p>
<pre><span id="more-1061"></span>mysql&gt; <strong>SELECT * FROM groups;
</strong>+----------+------------+
| group_id | group_name |
+----------+------------+
|      101 | Accounting |
|      102 | Engineer   |
|      103 | IT         |
|      104 | Manager    |
+----------+------------+
4 rows in set (0.00 sec)</pre>
<pre>mysql&gt; <strong>SELECT * FROM users;
</strong>+---------+----------+-----------+
| user_id | group_id | user_name |
+---------+----------+-----------+
|     501 |      101 | Ms.A      |
|     502 |      102 | Ms.B      |
|     503 |      102 | Mr.C      |
|     504 |      103 | Mr.D      |
|     505 |      104 | Mr.E      |
+---------+----------+-----------+
5 rows in set (0.00 sec)</pre>
<h4>การ DELETE ข้อมูล</h4>
<p>หลังจากใส่ FOREIGN KEY ในตาราง users เรียบร้อยแล้ว ทดสอบการลบ DELETE กลุ่ม &#8216;IT&#8217; ออกจาก table &#8216;groups&#8217;</p>
<pre>mysql&gt; <strong>DELETE FROM groups WHERE group_id='103';
</strong><span style="color: #ff0000;">ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails
</span>(`test_db`.`users`, CONSTRAINT `users_ibfk_1` FOREIGN KEY (`group_id`) REFERENCES `groups` (`group_id`))</pre>
<p>ข้อมูลกลุ่ม &#8216;IT&#8217; ยังอยู่ใน table &#8216;groups&#8217;</p>
<pre>mysql&gt; <strong>SELECT * FROM groups;
</strong>+----------+------------+
| group_id | group_name |
+----------+------------+
|      101 | Accounting |
|      102 | Engineer   |
|      103 | IT         |
|      104 | Manager    |
+----------+------------+
4 rows in set (0.00 sec)</pre>
<p>เราไม่สามารถลบ group &#8216;IT&#8217; ได้ เนื่องจากมีการอ้างอิงจาก table &#8216;users&#8217;</p>
<p>หากต้องการจะลบ ต้องแก้ไขข้อมูลที่มีการอ้างอิงมายัง group &#8216;IT&#8217; นี้ (group_id=103) เช่นในที่นี้ต้องเปลี่ยน group_id ของ &#8216;Mr.D&#8217; ไปเป็นกลุ่มอื่น เช่นเปลี่ยนเป็น กลุ่ม &#8216;Engineer&#8217;</p>
<p>ตรวจสอบว่าใครอยู่ group &#8216;IT&#8217; บ้าง</p>
<pre>mysql&gt; <strong>SELECT * FROM users WHERE group_id=103;
</strong>+---------+----------+-----------+
| user_id | group_id | user_name |
+---------+----------+-----------+
|     504 |      103 | Mr.D      |
+---------+----------+-----------+
1 row in set (0.00 sec)</pre>
<p>เปลี่ยนกลุ่มของ &#8216;MR.D&#8217; เป็น Engineer</p>
<pre>mysql&gt; <strong>UPDATE users SET group_id=102 WHERE user_id=504;
</strong>Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0</pre>
<p>ตรวจสอบว่าใครอยู่ group &#8216;IT&#8217; อีกครั้ง</p>
<pre>mysql&gt; <strong>SELECT * FROM users WHERE group_id=103;
</strong>Empty set (0.00 sec)</pre>
<p>ทดสอบการลบกลุ่ม IT</p>
<pre>mysql&gt; <strong>DELETE FROM groups WHERE group_id=103;
</strong>Query OK, 1 row affected (0.00 sec)</pre>
<p>ครั้งนี้จะสามารถลบได้แล้ว เพราะไม่มี user คนไหนอยู่ในกลุ่มนี้แล้ว</p>
<h4>การ INSERT, UPDATE ข้อมูล</h4>
<p>จะด้วยความผิดพลาดของโปรแกรมหรือตั้งใจ สมมติเราเพิ่ม users ใหม่ แล้วตั้ง group_id=109 ซึ่งไม่มีอยู่ใน table &#8216;groups&#8217;</p>
<pre>mysql&gt; <strong>INSERT INTO users (user_id, group_id, user_name) VALUES (506, <span style="color: #ff0000;">109</span>, 'Mr.X');
</strong><span style="color: #ff0000;">ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
</span>(`test_db`.`users`, CONSTRAINT `users_ibfk_1` FOREIGN KEY (`group_id`) REFERENCES `groups` (`group_id`))</pre>
<p>เราจะไม่สามารถ INSERT ข้อมูลแบบนี้ได้ ต้องไปสร้าง group_id 109 ใน table &#8216;groups&#8217; ก่อน</p>
<p>เช่นเดียวกัน เราไม่สามารถ UPDATE ข้อมูลที่ไม่ถูกต้องได้</p>
<pre>mysql&gt; <strong>UPDATE users SET group_id=109 WHERE user_id=505;
</strong><span style="color: #ff0000;">ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
</span>(`test_db`.`users`, CONSTRAINT `users_ibfk_1` FOREIGN KEY (`group_id`) REFERENCES `groups` (`group_id`))</pre>
<h4>การ DROP TABLE</h4>
<p>หากยังมีการอ้างอิงฟิลด์ข้อมูลอยู่ เราไม่สามารถ DROP TABLE ที่ยังมีข้อมูลอ้างอิงอยู่ได้ เช่นในที่นี้เราไม่สามารถ DROP TABLE groups ได้</p>
<pre>mysql&gt; <strong>DROP TABLE groups;
</strong><span style="color: #ff0000;">ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails</span></pre>
<p>ถึงแม้ไม่มีข้อมูล rows ใดเลยใน users ก็ไม่สามารถ DROP TABLE groups ได้</p>
<pre>mysql&gt; <strong>DELETE FROM users;
</strong>Query OK, 5 rows affected (0.00 sec)</pre>
<pre>mysql&gt; <strong>DROP TABLE groups;
</strong>ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails</pre>
<h4>ข้อมูลอ้างอิง</h4>
<ul>
<li><a href="http://spalinux.com/2010/01/creating_mysql_foreign_key_constraints">การสร้าง FOREIGN KEY Constraints ใน MySQL</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2010/01/testing_mysql_foreign_key_constraints/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>การสร้าง FOREIGN KEY Constraints ใน MySQL</title>
		<link>http://spalinux.com/2010/01/creating_mysql_foreign_key_constraints</link>
		<comments>http://spalinux.com/2010/01/creating_mysql_foreign_key_constraints#comments</comments>
		<pubDate>Sat, 23 Jan 2010 09:22:39 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[mysql constraints]]></category>
		<category><![CDATA[mysql foreign key]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=1053</guid>
		<description><![CDATA[ส่วนใหญ่แล้วการออกแบบฐานข้อมูลจะมีการใช้บางฟิลด์ข้อมูล เพื่อเชื่อมโยงความสัมพันธ์ระหว่าง table ทำให้ต้องมีการคำนึงถึงเวลา เพิ่ม แก้ไข หรือ ลบฟิลด์ที่ใช้เชื่อมนั้น ไม่เช่นนั้นข้อมูลในฐานข้อมูลอาจมีปัญหาได้ MySQL สนับสนุนคุณสมบัติการใช้ FOREIGN KEY Constraints เพื่อแก้ไขปัญหานี้ได้ แต่ต้องใช้ table เป็นแบบ InnoDB ในบทความนี้ขออธิบายปัญหาที่เกิดขึ้น พร้อมยกตัวอย่างวิธีการใช้ FOREIGN KEY Constraints เพื่อป้องกันปัญหาได้ ตัวอย่างเช่น ต้องการสร้างฐานข้อมูลสำหรับเก็บชื่อ users และแต่ละคนเป็นสมาชิกได้ 1 กลุ่มจากตาราง groups เริ่มต้นออกแบบ table ง่ายๆ ได้ดังนี้ mysql&#62; CREATE TABLE groups (  group_id     INT UNSIGNED NOT NULL,  group_name   VARCHAR(255),  PRIMARY KEY    (group_id) ); mysql&#62; CREATE TABLE [...]]]></description>
			<content:encoded><![CDATA[<p>ส่วนใหญ่แล้วการออกแบบฐานข้อมูลจะมีการใช้บางฟิลด์ข้อมูล เพื่อเชื่อมโยงความสัมพันธ์ระหว่าง table ทำให้ต้องมีการคำนึงถึงเวลา เพิ่ม แก้ไข หรือ ลบฟิลด์ที่ใช้เชื่อมนั้น ไม่เช่นนั้นข้อมูลในฐานข้อมูลอาจมีปัญหาได้</p>
<p>MySQL สนับสนุนคุณสมบัติการใช้ FOREIGN KEY Constraints เพื่อแก้ไขปัญหานี้ได้ แต่ต้องใช้ table เป็นแบบ InnoDB ในบทความนี้ขออธิบายปัญหาที่เกิดขึ้น พร้อมยกตัวอย่างวิธีการใช้ FOREIGN KEY Constraints เพื่อป้องกันปัญหาได้</p>
<p><span id="more-1053"></span></p>
<p>ตัวอย่างเช่น ต้องการสร้างฐานข้อมูลสำหรับเก็บชื่อ users และแต่ละคนเป็นสมาชิกได้ 1 กลุ่มจากตาราง groups เริ่มต้นออกแบบ table ง่ายๆ ได้ดังนี้</p>
<pre>mysql&gt; <strong>CREATE TABLE groups (
 group_id     INT UNSIGNED NOT NULL,
 group_name   VARCHAR(255),
 PRIMARY KEY    (group_id)
);</strong></pre>
<pre>mysql&gt; <strong>CREATE TABLE users (
 user_id      INT UNSIGNED NOT NULL,
 group_id     INT UNSIGNED NOT NULL,
 user_name    VARCHAR(255),
 PRIMARY KEY    (user_id)
);</strong></pre>
<p>ใส่ข้อมูลทดสอบดังนี้</p>
<pre>mysql&gt; <strong>INSERT INTO groups (group_id, group_name) VALUES
 (101, 'Accounting'),
 (102, 'Engineer'),
 (103, 'IT'),
 (104, 'Manager');</strong></pre>
<pre>mysql&gt; <strong>INSERT INTO users (user_id, group_id, user_name) VALUES
 (501, 101, 'Ms.A'),
 (502, 102, 'Ms.B'),
 (503, 102, 'Mr.C'),
 (504, 103, 'Mr.D'),
 (505, 104, 'Mr.E');</strong></pre>
<p>จากฐานข้อมูลตัวอย่าง เราใช้ฟิลด์ group_id ในการเชื่อมโยงความสัมพันธ์ระหว่าง groups และ users เพื่อระบุว่า แต่ละ users อยู่ใน group ใด</p>
<p>หากมีการแก้ไขข้อมูล เช่นต้องการลบ (DELETE) ข้อมูล group ที่ชื่อ &#8216;IT&#8217; ออกจากฐานข้อมูล หากไม่มีวิธีการตรวจสอบ ไปลบแค่จากตาราง &#8216;groups&#8217; จะทำให้ ข้อมูลของ &#8216;Mr.D&#8217; ในตาราง &#8216;users&#8217; มีปัญหาทันที เพราะไม่สามารถอ้างอิงกลับมายังชื่อ group ได้</p>
<p>โดยดีฟอลต์ การสร้าง table ใน MySQL หากไม่มีการระบุชนิด ในคำสั่ง &#8216;CREATE TABLE&#8217; ตารางที่สร้างได้จะเป็นแบบ MyISAM ซึ่งไม่สามารถสร้าง FOREIGN KEY ได้</p>
<p>สามารถใช้คำสั่ง &#8216;SHOW ENGINES&#8217; ใน mysql เพื่อตรวจสอบชนิดของ table ที่สร้างได้</p>
<pre>mysql&gt; <strong>SHOW ENGINES;
</strong>+------------+---------+------------------------------------------------------------+--------------+------+------------+
| Engine     | Support | Comment                                                    | Transactions | XA   | Savepoints |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| ndbcluster | NO      | Clustered, fault-tolerant tables                           | NULL         | NULL | NULL       |
| MRG_MYISAM | YES     | Collection of identical MyISAM tables                      | NO           | NO   | NO         |
| CSV        | YES     | CSV storage engine                                         | NO           | NO   | NO         |
| <strong><span style="color: #0000ff;">MyISAM</span>     </strong>| <strong><span style="color: #0000ff;">DEFAULT</span> </strong>| Default engine as of MySQL 3.23 with great performance     | NO           | NO   | NO         |
| <strong><span style="color: #0000ff;">InnoDB</span></strong>     | <strong><span style="color: #0000ff;">YES</span></strong>     | Supports transactions, row-level locking, and <strong><span style="color: #0000ff;">foreign keys</span></strong> | YES          | YES  | YES        |
| MEMORY     | YES     | Hash based, stored in memory, useful for temporary tables  | NO           | NO   | NO         |
+------------+---------+------------------------------------------------------------+--------------+------+------------+</pre>
<p>ถ้าจะใช้ FOREIGN KEY ได้นั้น table ที่มีการอ้างอิงกันทั้งหมด ต้องเป็นชนิด InnoDB คือต้องระบุออปชั่น ENGINE=InnoDB เวลาสร้าง table</p>
<p>เมื่อสร้างเป็น InnoDB แล้ว ระบุฟิลด์ที่ต้องการตรวจสอบ constraints ใน table ที่ใช้อ้างอิงไปยัง table อื่น</p>
<p>เช่นในที่นี้เราต้องการให้ table &#8216;users&#8217; ใช้ฟิลด์ชื่อ group_id เพื่ออ้างอิงไปยัง table &#8216;groups&#8217; เราต้องระบุ FOREIGN KEY ใน table &#8216;users&#8217;</p>
<p>วิธีการระบุการอ้างอิง FOREIGN KEY ฟิลด์ &#8216;group_id&#8217; จาก table &#8216;groups&#8217;</p>
<pre><span style="color: #0000ff;"><strong>FOREIGN KEY (group_id) REFERENCES groups(group_id)</strong></span></pre>
<p>วิธีการเปลี่ยน table จาก MyISAM ให้เป็น InnoDB และเพิ่ม FOREIGN KEY ทำได้ 2 วิธี</p>
<p> 1. ใช้คำสั่ง &#8216;ALTER TABLE&#8217; เพื่อแก้ไข<br />
 2. ใช้คำสั่ง &#8216;DROP TABLE&#8217; ทิ้งไปแล้วสร้างใหม่ แต่ข้อมูลที่มีอยู่จะหายหมด</p>
<h4>ใช้คำสั่ง ALTER TABLE เพื่อเปลี่ยนชนิดเป็น InnoDB</h4>
<p>เราสามารถใช้คำสั่ง &#8216;ALTER TABLE&#8217; เพื่อเปลี่ยนชนิดของ table หรือแก้ไขฟิลด์ต่างๆ ได้</p>
<p>การใช้คำสั่ง ALTER TABLE เพื่อเปลี่ยนชนิด table เป็นแบบ InnoDB</p>
<pre>mysql&gt; <strong>ALTER TABLE groups ENGINE=InnoDB;
</strong>Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0</pre>
<pre>mysql&gt; <strong>ALTER TABLE users ENGINE=InnoDB;
</strong>Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0</pre>
<p>เพิ่ม FOREIGN KEY ใน table &#8216;users&#8217;</p>
<pre>mysql&gt; <strong>ALTER TABLE `users` ADD FOREIGN KEY (group_id) REFERENCES groups(group_id);
</strong>Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0</pre>
<p>หมายเหตุ ต้องดูผลลัพธ์การใช้คำสั่ง ALTER TABLE ด้วย ว่าการเปลี่ยนแปลง table สำเร็จหรือไม่</p>
<h4>ลบแล้วสร้าง TABLE ใหม่แบบ InnoDB และใช้ FOREIGN KEY</h4>
<p>ใช้คำสั่ง DROP TABLE เพื่อลบ table &#8216;users&#8217;</p>
<pre>mysql&gt; <strong>DROP TABLE users;
</strong>mysql&gt; <strong>DROP TABLE groups;</strong></pre>
<p>ใช้คำสั่ง CREATE TABLE เพื่อสร้าง table ใหม่ แล้ว ใส่ข้อมูลลงไปเหมือนเดิม</p>
<pre>mysql&gt; <strong>CREATE TABLE groups (
 group_id     INT UNSIGNED NOT NULL,
 group_name   VARCHAR(255),
 PRIMARY KEY    (group_id)
) <span style="color: #0000ff;">ENGINE=InnoDB</span>;</strong></pre>
<pre>mysql&gt; <strong>INSERT INTO groups (group_id, group_name) VALUES
 (101, 'Accounting'),
 (102, 'Engineer'),
 (103, 'IT'),
 (104, 'Manager');</strong></pre>
<pre>mysql&gt; <strong>CREATE TABLE users (
 user_id      INT UNSIGNED NOT NULL,
 group_id     INT UNSIGNED NOT NULL,
 user_name    VARCHAR(255),
 PRIMARY KEY    (user_id),
<span style="color: #0000ff;"> FOREIGN KEY (group_id) REFERENCES groups(group_id)
</span>) <span style="color: #0000ff;">ENGINE=InnoDB;</span></strong></pre>
<pre>mysql&gt; <strong>INSERT INTO users (user_id, group_id, user_name) VALUES
 (501, 101, 'Ms.A'),
 (502, 102, 'Ms.B'),
 (503, 102, 'Mr.C'),
 (504, 103, 'Mr.D'),
 (505, 104, 'Mr.E');</strong></pre>
<p>สังเกตสิ่งที่เพิ่มขึ้น</p>
<h4>ข้อมูลอ้างอิง</h4>
<ul>
<li><a href="http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html" target="_blank">MySQL 5.1 Reference Manual :: 13.6.4.4 FOREIGN KEY Constraints</a></li>
<li><a href="http://spalinux.com/2010/01/testing_mysql_foreign_key_constraints">ทดสอบ FOREIGN KEY Constraints ใน MySQL</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2010/01/creating_mysql_foreign_key_constraints/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>ติดตั้งและคอนฟิก MySQL ODBC ตอนที่ 2</title>
		<link>http://spalinux.com/2009/12/install_configure_mysql_odbc_part_2</link>
		<comments>http://spalinux.com/2009/12/install_configure_mysql_odbc_part_2#comments</comments>
		<pubDate>Fri, 04 Dec 2009 16:03:57 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[MyODBC]]></category>
		<category><![CDATA[ODBC]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=1018</guid>
		<description><![CDATA[หลังจาก ติดตั้งและคอนฟิก MySQL ODBC ตอนที่ 1 ก่อนที่จะใช้ ODBC เพื่อดึงข้อมูลจากฐานข้อมูล MySQL ได้ ต้องมีการคอนฟิกค่าพารามิเตอร์ต่างๆ ก่อน  ในตอนที่ 2 นี้จะแสดงตัวอย่างการคอนฟิกค่า ODBC เพื่อจะเชื่อมต่อเข้ากับ Database Server และตัวอย่างการใช้โปรแกรม Excel เพื่อดึงข้อมูลมาจากฐานข้อมูล MySQL คอนฟิก ODBC การคอนฟิก ODBC บน Windows จะเป็นการสร้าง DSN (Data Source Name) ซึ่งสามารถเริ่มต้นทำได้โดยไปที่ Control Panel &#8211;&#62; Administrative Tools &#8211;&#62; Data Sources (ODBC)   เลือก driver เป็นแบบ MySQL ODBC 5.1 Driver    ใส่ค่าพารามิเตอร์สำหรับการเชื่อมต่อฐานข้อมูล Data [...]]]></description>
			<content:encoded><![CDATA[<p>หลังจาก <a href="http://spalinux.com/2009/12/install_configure_mysql_odbc_part_1">ติดตั้งและคอนฟิก MySQL ODBC ตอนที่ 1</a> ก่อนที่จะใช้ ODBC เพื่อดึงข้อมูลจากฐานข้อมูล MySQL ได้ ต้องมีการคอนฟิกค่าพารามิเตอร์ต่างๆ ก่อน </p>
<p>ในตอนที่ 2 นี้จะแสดงตัวอย่างการคอนฟิกค่า ODBC เพื่อจะเชื่อมต่อเข้ากับ Database Server และตัวอย่างการใช้โปรแกรม Excel เพื่อดึงข้อมูลมาจากฐานข้อมูล MySQL</p>
<p><span id="more-1018"></span></p>
<h4>คอนฟิก ODBC</h4>
<p>การคอนฟิก ODBC บน Windows จะเป็นการสร้าง DSN (Data Source Name) ซึ่งสามารถเริ่มต้นทำได้โดยไปที่</p>
<p>Control Panel &#8211;&gt; Administrative Tools &#8211;&gt; Data Sources (ODBC)</p>
<p><img class="alignnone size-full wp-image-1020" title="B1 User DSN" src="http://spalinux.com/wp-content/uploads/2009/12/B1_User_DSN.png" alt="B1 User DSN" width="461" height="384" /></p>
<p> </p>
<p>เลือก driver เป็นแบบ MySQL ODBC 5.1 Driver </p>
<p><img class="alignnone size-full wp-image-1021" title="B2 Create New Data Source" src="http://spalinux.com/wp-content/uploads/2009/12/B2_Create_New_Data_Source.png" alt="B2 Create New Data Source" width="468" height="352" /></p>
<p> </p>
<p>ใส่ค่าพารามิเตอร์สำหรับการเชื่อมต่อฐานข้อมูล</p>
<p><strong>Data Source Name</strong> : ตั้งชื่อ DSN<br />
<strong>Server</strong> : ชื่อ Hostname หรือ IP Address ของ Database Server<br />
<strong>Port </strong>: พอร์ตของ MySQL ดีฟอลต์ 3306<br />
<strong>User</strong> : Username สำหรับการล็อกเข้าฐานข้อมูล<br />
<strong>Password</strong> : รหัสผ่าน<br />
<strong>Database</strong> : ชื่อ Database ที่ต้องการ </p>
<p>หากใส่ข้อมูล Server, User, Password ถูกต้อง จะสามารถเลือกชื่อ Database ในช่อง Database ได้</p>
<p><img title="B3 Connection Parameters" src="http://spalinux.com/wp-content/uploads/2009/12/B3_Connection_Parameters.png" alt="B3 Connection Parameters" width="425" height="410" /></p>
<p> </p>
<p>ตัวอย่างผลลัพธ์จากการกดปุ่ม [Test]</p>
<p><img title="B4 Test Result" src="http://spalinux.com/wp-content/uploads/2009/12/B4_Test_Result.png" alt="B4 Test Result" width="134" height="107" /></p>
<p> </p>
<p>หลังจากคอนฟิกเรียบร้อย จะมีชื่อ DSN ที่เพิ่มขึ้น</p>
<p><img title="B5 Finished" src="http://spalinux.com/wp-content/uploads/2009/12/B5_Finished.png" alt="B5 Finished" width="461" height="384" /></p>
<p> </p>
<h4> ทดลองดึงข้อมูลด้วย Excel</h4>
<p>เพื่อความง่ายในการทดสอบ จะใช้ Microsoft Excel  </p>
<p> เปิดโปรแกรม Excel</p>
<p><img title="C1 New Excel File" src="http://spalinux.com/wp-content/uploads/2009/12/C1_New_Excel_File.png" alt="C1 New Excel File" width="698" height="312" /></p>
<p> </p>
<p>เลือกเมนู Data &#8211;&gt; Import External Data &#8211;&gt; Import Data จะมีหน้าต่าง &#8220;Select Data Source&#8221; แสดงขึ้นมา</p>
<p>เลือก &#8220;+Connect to New Data Source.odc&#8221; แล้วกดปุ่ม [Open]</p>
<p><img title="C2 Select Data Source" src="http://spalinux.com/wp-content/uploads/2009/12/C2_Select_Data_Source.png" alt="C2 Select Data Source" width="601" height="393" /></p>
<p> </p>
<p>หน้าจอ Wizard ช่วยในการคอนฟิก เลือก &#8220;ODBC DSN&#8221;</p>
<p><img title="C3 Data Connection Wizard" src="http://spalinux.com/wp-content/uploads/2009/12/C3_Data_Connection_Wizard.png" alt="C3 Data Connection Wizard" width="498" height="363" /></p>
<p> </p>
<p>เลือกชื่อ Data Source ที่คอนฟิกไว้แล้ว</p>
<p><img title="C4 Choose Data Source" src="http://spalinux.com/wp-content/uploads/2009/12/C4_Choose_Data_Source.png" alt="C4 Choose Data Source" width="498" height="363" /></p>
<p> </p>
<p>เลือกชื่อ Database และ Table ที่ต้องการจะดึงข้อมูล</p>
<p><img title="C5 Select Database and Table" src="http://spalinux.com/wp-content/uploads/2009/12/C5_Select_Database_and_Table.png" alt="C5 Select Database and Table" width="498" height="369" /></p>
<p> </p>
<p>สามารถเซฟคอนฟิกที่สร้างไว้ เป็นไฟล์ เพื่อสะดวกในการดึงข้อมูลครั้งต่อไปได้</p>
<p><img title="C6 Save Data Connection File" src="http://spalinux.com/wp-content/uploads/2009/12/C6_Save_Data_Connection_File.png" alt="C6 Save Data Connection File" width="509" height="363" /></p>
<p> </p>
<p> เลือกช่องใส่ข้อมูลใน Excel แล้วกด [OK]</p>
<p><img title="C7 Where to put Data" src="http://spalinux.com/wp-content/uploads/2009/12/C7_Where_to_put_Data.png" alt="C7 Where to put Data" width="347" height="218" /></p>
<p> </p>
<p> ตัวอย่างผลลัพธ์การดึงข้อมูลจาก MySQL ผ่าน ODBC แล้วแสดงใน Excel</p>
<p><img title="C8 Imported Data" src="http://spalinux.com/wp-content/uploads/2009/12/C8_Imported_Data.png" alt="C8 Imported Data" width="726" height="459" /></p>
<p> </p>
<h4>ข้อมูลอ้างอิง</h4>
<ul>
<li><a href="http://spalinux.com/2009/12/install_configure_mysql_odbc_part_1">ติดตั้งและคอนฟิก MySQL ODBC ตอนที่ 1</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2009/12/install_configure_mysql_odbc_part_2/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ติดตั้งและคอนฟิก MySQL ODBC ตอนที่ 1</title>
		<link>http://spalinux.com/2009/12/install_configure_mysql_odbc_part_1</link>
		<comments>http://spalinux.com/2009/12/install_configure_mysql_odbc_part_1#comments</comments>
		<pubDate>Fri, 04 Dec 2009 11:39:06 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[MyODBC]]></category>
		<category><![CDATA[ODBC]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=1005</guid>
		<description><![CDATA[บทความนี้จะกล่าวถึงการติดตั้งโปรแกรม MySQL Connector/ODBC (หรือชื่อเดิมคือ MyODBC) บน Windows XP เพื่อให้โปรแกรมที่รันอยู่บน Windows หรือโปรแกรมที่พัฒนาขึ้นมาเอง สามารถที่จะดึงข้อมูลจาก MySQL Database ได้ ในตอนแรกจะเป็นการสร้างตัวอย่างฐานข้อมูลสำหรับการทดสอบ และการติดตั้งโปรแกรม ODBC สร้างฐานข้อมูลสำหรับทดสอบ เพื่อการทดสอบการเชื่อมต่อ ODBC เราจะสร้าง User, Password, Database และ Table สำหรับการทดสอบ บน Database Server โดยมีรายละเอียดดังนี้ Database Name:    myodbc_test User:                          myodbc Password:               password Table Name:          test01 ตัวอย่างการใช้คำสั่งเพื่อสร้างฐานข้อมูลสำหรับทดสอบ mysql&#62; CREATE DATABASE myodbc_test; mysql&#62; GRANT ALL ON myodbc_test.* TO 'myodbc' IDENTIFIED [...]]]></description>
			<content:encoded><![CDATA[<p>บทความนี้จะกล่าวถึงการติดตั้งโปรแกรม MySQL Connector/ODBC (หรือชื่อเดิมคือ MyODBC) บน Windows XP เพื่อให้โปรแกรมที่รันอยู่บน Windows หรือโปรแกรมที่พัฒนาขึ้นมาเอง สามารถที่จะดึงข้อมูลจาก MySQL Database ได้</p>
<p>ในตอนแรกจะเป็นการสร้างตัวอย่างฐานข้อมูลสำหรับการทดสอบ และการติดตั้งโปรแกรม ODBC</p>
<h4><span id="more-1005"></span>สร้างฐานข้อมูลสำหรับทดสอบ</h4>
<p>เพื่อการทดสอบการเชื่อมต่อ ODBC เราจะสร้าง User, Password, Database และ Table สำหรับการทดสอบ บน Database Server โดยมีรายละเอียดดังนี้</p>
<p>Database Name:    myodbc_test<br />
User:                          myodbc<br />
Password:               password<br />
Table Name:          test01</p>
<p>ตัวอย่างการใช้คำสั่งเพื่อสร้างฐานข้อมูลสำหรับทดสอบ</p>
<pre>mysql&gt; <strong>CREATE DATABASE myodbc_test;
</strong>mysql&gt; <strong>GRANT ALL ON myodbc_test.* TO 'myodbc' IDENTIFIED BY 'password';
</strong>mysql&gt; <strong>USE myodbc_test;</strong></pre>
<pre>mysql&gt; <strong>CREATE TABLE `test01` (
  `id`      INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `name`    VARCHAR(255),
  `email`   VARCHAR(255),
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM;</strong></pre>
<pre>mysql&gt; <strong>INSERT INTO test01 (id, name, email) VALUES (NULL, 'user01', 'user01@email.com');
</strong>mysql&gt; <strong>INSERT INTO test01 (id, name, email) VALUES (NULL, 'user02', 'user02@email.com');
</strong>mysql&gt; <strong>INSERT INTO test01 (id, name, email) VALUES (NULL, 'user03', 'user03@email.com');</strong></pre>
<pre>mysql&gt; <strong>SELECT * FROM test01;
</strong>+----+--------+------------------+
| id | name   | email            |
+----+--------+------------------+
|  1 | user01 | user01@email.com |
|  2 | user02 | user02@email.com |
|  3 | user03 | user03@email.com |
+----+--------+------------------+</pre>
<h4>ดาวน์โหลดและติดตั้ง</h4>
<p>ดาวน์โหลดโปรแกรม MySQL Connector/ODBC 5.1 ได้จาก <a href="http://dev.mysql.com/downloads/connector/odbc/5.1.html">http://dev.mysql.com/downloads/connector/odbc/5.1.html</a></p>
<p>เพื่อความง่ายแนะนำให้เลือกแบบ Windows MSI Installer (x86)</p>
<p>หมายเหตุ เวอร์ชั่นล่าสุดที่เขียนบทความคือ 5.1.6 ชื่อไฟล์คือ mysql-connector-odbc-5.1.6-win32.msi</p>
<p> คลิ้กที่ไฟล์เพื่อรันติดตั้งโปรแกรม</p>
<p><img class="alignnone size-full wp-image-1006" title="A1 Welcome to Setup Wizard" src="http://spalinux.com/wp-content/uploads/2009/12/A1_Welcome_to_Setup_Wizard.png" alt="A1 Welcome to Setup Wizard" width="504" height="386" /></p>
<p> </p>
<p>เลือก Setup Type เป็นแบบ Custom</p>
<p><img title="A2 Setup Type" src="http://spalinux.com/wp-content/uploads/2009/12/A2_Setup_Type.png" alt="A2 Setup Type" width="504" height="386" /></p>
<p> </p>
<p>เลือกทุก features แล้วกด [Next]</p>
<p><img title="A3 Select the Program Features" src="http://spalinux.com/wp-content/uploads/2009/12/A3_Select_the_Program_Features.png" alt="A3 Select the Program Features" width="504" height="386" /></p>
<p> </p>
<p> กดปุ่ม [Install]</p>
<p><img title="A4 Ready to Install the Program" src="http://spalinux.com/wp-content/uploads/2009/12/A4_Ready_to_Install_the_Program.png" alt="A4 Ready to Install the Program" width="504" height="386" /></p>
<p> </p>
<p>หน้าจอแสดงกำลังติดตั้งโปรแกรม </p>
<p> <img title="A5 Installing" src="http://spalinux.com/wp-content/uploads/2009/12/A5_Installing.png" alt="A5 Installing" width="504" height="386" /></p>
<p> </p>
<p>ติดตั้งเสร็จสิ้น </p>
<p><img title="A6 Wizard Completed" src="http://spalinux.com/wp-content/uploads/2009/12/A6_Wizard_Completed.png" alt="A6 Wizard Completed" width="504" height="386" /></p>
<p> </p>
<p>ข้อมูลอ้างอิง</p>
<ul>
<li><a href="http://dev.mysql.com/downloads/connector/odbc/5.1.html" target="_blank">MySQL Connector/ODBC 5.1</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2009/12/install_configure_mysql_odbc_part_1/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

