BMS-HOSxP Community

HOSxP => แจ้งปัญหา / ขอความช่วยเหลือ => ข้อความที่เริ่มโดย: natch2005 ที่ ตุลาคม 02, 2009, 10:29:26 AM

หัวข้อ: ต้องการ connect ฐานข้อมูลด้วย PHP
เริ่มหัวข้อโดย: natch2005 ที่ ตุลาคม 02, 2009, 10:29:26 AM
Linux Server ครับ

$hostname = "192.168.2.222"; //ชื่อโฮสต์
$user = "sa"; //ชื่อผู้ใช้
$password = "sa"; //รหัสผ่าน
$dbname = "hos"; //ชื่อฐานข้อมูล
$connection=mysql_connect($hostname, $user, $password) or die("ติดต่อฐานข้อมูลไม่ได้");

แบบนี้ไม่ได้ครับ ต้องเขียนโค้ดยังไงครับ
หัวข้อ: Re: ต้องการ connect ฐานข้อมูลด้วย PHP
เริ่มหัวข้อโดย: armds ที่ ตุลาคม 02, 2009, 10:37:39 AM
@$db=mysql_connect($hostname, $user, $password);
$charset = "SET NAMES'tis620'";
mysql_query($charset) or die('Invalid query: ' . mysql_error());
if(!$db)
{
   echo "เกิดข้อผิดพลาด";
 exit;
}mysql_select_db($dbname);

ลองดูครับ 
หัวข้อ: Re: ต้องการ connect ฐานข้อมูลด้วย PHP
เริ่มหัวข้อโดย: ทานน้ำ ที่ ตุลาคม 02, 2009, 10:40:52 AM
<?php
$dbhost="localhost";
$dbuser="sa";
$dbpass="sa";

$c=mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_query("set names 'tis620' ",$c);
mysql_select_db("hos");

$sqlviewdata=mysql_query("select * from vn_stat  where vstdate between '2009-08-21'  and '2009-08-21' ");
while($rs=mysql_fetch_array($sqlviewdata)){
echo"$rs[hn] $rs[vn]<br>";
}
?>





ลองเอาตัวนี้ไปรันดูก็ได้ค่ะ ค่ะ
หัวข้อ: Re: ต้องการ connect ฐานข้อมูลด้วย PHP
เริ่มหัวข้อโดย: natch2005 ที่ ตุลาคม 02, 2009, 14:43:50 PM
คือ เหมือนกับว่า มันล็อกไว้นะครับ

Client does not support authentication protocol requested by server; consider upgrading MySQL client in H:\www\hos1.php on line 6
หัวข้อ: Re: ต้องการ connect ฐานข้อมูลด้วย PHP
เริ่มหัวข้อโดย: James011 ที่ ตุลาคม 02, 2009, 15:52:04 PM
 ;D ;D  แก้ไขปัญหา Client does not support authentication protocol ใน MySQL

ใน MySQL 4.1 ขึ้นไปนั้น ได้ใช้ hashing algorithm ในการเก็บ password เพื่อใช้ในการ Authentication Protocol แบบใหม่ซึ่งทำให้ยังไม่สนับสนุนใน PHP หรือ software client ใน version เก่าๆ (หรือใหม่ๆ บางตัวก็ยังไม่สนับสนุน) ถ้าคุณ upgrade ไปเป็น MySQL 4.1 หรือสูงกว่า การติดต่อเพื่อเข้าไปใช้งานนั้น จะขึ้นข้อความว่า

Client does not support authentication protocol requested by server; consider upgrading MySQL client.

สาเหตุมาจากการที่ MySQL ได้ทำการปรับเปลี่ยนการเข้ารหัส password ใหม่อย่างที่ได้กล่าวไปแล้วโดย ถ้าใน MySQL version ก่อน 4.1 นั้นใช้การเข้ารหัสที่มีความยาว 16 bytes แต่ใน version ตั้งแต่ 4.1 ขึ้นมาถึงปัจจุบันนี้จะใช้การเข้ารหัสที่มีความยาว 41 bytes ซึ่ง client บางตัวยังไม่สนับสนุน password hashing algorithm แบบนี้ครับ ซึ่งการเข้ารหัส password ที่มีความยาว 41bytes นี้จะทำให้การถอดรหัสเพื่อทำการ hack ข้อมูลนั้นทำได้ยากขึ้นมากเลยทีเดียวครับ 

โดยถ้าเราเปรียบเทียบจากตัวก่อนหน้า version 4.1 จะได้

mysql> SELECT PASSWORD(’mypass’);
+——————–+
| PASSWORD(’mypass’) |
+——————–+
| 6f8c114b58f2ce9e |
+——————–+


แต่ถ้าเราใช้คำสั่งเดียวกันใน version หลังจาก 4.1 จะได้

mysql> SELECT PASSWORD(’mypass’);
+———————————————–+
| PASSWORD(’mypass’) |
+———————————————–+
| *43c8aa34cdc98eddd3de1fe9a9c2c2a9f92bb2098d75 |
+———————————————–+


จะเห็นความแตกต่างของการเข้ารหัส password ของ MySQL ครับ

การแก้ไขปัญหานี้ทำได้โดยการ

ทำการ upgrade ตัว client ที่ติดต่อกับ MySQL ให้เป็น version ที่สนับสนุน hashing algorithm ที่เก็บ password ใหม่ใน MySQL 4.1 ขึ้นไป (client ที่ว่านี้หมายถึง PHP MySQL Module, MySQL Front, PHPMyAdmin และตัวจัดการข้อมูลต่างๆ ที่เชื่อมต่อกับ MySQL ถือเป็น client ทั้งหมดครับ)

เมื่อทำการติดต่อกับ server โดยใช้ pre-4.1 client program ให้ใช้บัญชี username ที่ใช้ pre-4.1-style password แทนการใช้ username ที่ใช้ style password แบบเก่า

ทำการ Reset password ไปเป็น pre-4.1 style โดยใช้คำสั่ง SET PASSWORD และ OLD_PASSWORD() function โดยทำใน MySQL Command Line Client ซึ่งใช้คำสั่งดังนี้

mysql> SET PASSWORD FOR

-> ‘some_user‘@’some_host‘ = OLD_PASSWORD(’newpwd‘);

some_host ให้เปลี่ยนเป็น hostname ที่ใช้เช่น localhost, 127.0.0.1, 192.168.0.1 หรือที่เป็น hostname นั้นๆ
some_user ให้เปลี่ยนเป็น username ที่ใช้เช่น root, admin หรือ username อื่นๆ ที่ต้องการ
newpwd ให้เป็นเป็น password ที่ใช้เช่น 1234, abcde หรือที่ต้องการ
ตัวอย่าง :

mysql> SET PASSWORD FOR

-> ‘root‘@’localhost‘ = OLD_PASSWORD(’1234‘);

คือกำหนด password ใหม่ให้กับ root ที่ localhost โดยใช้ hashing algorithm password แบบเก่าโดยกำหนด password คือ 1234

แล้วทำการ UPDATE และ FLUSH PRIVILEGES โดยใช้สั่ง

mysql> UPDATE mysql.user SET Password = OLD_PASSWORD(’newpwd‘)

-> WHERE Host = ‘some_host‘ AND User = ‘some_user‘;

mysql> FLUSH PRIVILEGES;

ตัวอย่าง :

mysql> UPDATE mysql.user SET Password = OLD_PASSWORD(’1234‘)

-> WHERE Host = ‘localhost‘ AND User = ‘root‘;

mysql> FLUSH PRIVILEGES;

* ด้วยวิธีด้านบนนี้เหมาะสำหรับใช้ในกรณีที่มี user ที่อยู่ในฐานข้อมูลน้อยๆ เท่านั้น มิเช่นนั้นแล้วอาจจะต้องเปลี่ยนกันมืองึก -_-"

ถ้ามี user ในฐานข้อมูลมากๆ และการปรับเปลี่ยนวิธีด้านบนทั้งหมดเป็นการยากในการทำระบบฐานข้อมูลกลับมาใช้งานได้ปกติในเวลาอันสั้น เราสามารถบอกให้ MySQL ใช้ password hashing algorithm แบบเก่าตอน start-up service ได้เลย แต่แนะนำให้ใช้งานชั่วคราวเท่านั้นครับ และเมื่อมีเวลาให้ทำการปรับเปลี่ยนไปใช้ระบบ password hashing algorithm แบบใหม่แทน
โดยเริ่มการทำงานของ mysqld ด้วย –old-passwords ที่เป็น option command

ตัวอย่าง :

c:\MySQL\bin\mysqld –old-passwords
หัวข้อ: Re: ต้องการ connect ฐานข้อมูลด้วย PHP
เริ่มหัวข้อโดย: James011 ที่ ตุลาคม 02, 2009, 15:58:28 PM
 ;D ;D ;D อันนี้จาก www.mysql.com

Client does not support authentication protocol  
MySQL 5.1 uses an authentication protocol based on a password hashing algorithm that is incompatible with that used by older (pre-4.1) clients. If you upgrade the server from 4.0, attempts to connect to it with an older client may fail with the following message:

shell> mysql
Client does not support authentication protocol requested
by server; consider upgrading MySQL client
To solve this problem, you should use one of the following approaches:

Upgrade all client programs to use a 4.1.1 or newer client library.

When connecting to the server with a pre-4.1 client program, use an account that still has a pre-4.1-style password.

Reset the password to pre-4.1 style for each user that needs to use a pre-4.1 client program. This can be done using the SET PASSWORD statement and the OLD_PASSWORD() function:

mysql> SET PASSWORD FOR
    -> 'some_user'@'some_host' = OLD_PASSWORD('newpwd');
Alternatively, use UPDATE and FLUSH PRIVILEGES:

mysql> UPDATE mysql.user SET Password = OLD_PASSWORD('newpwd')
    -> WHERE Host = 'some_host' AND User = 'some_user';
mysql> FLUSH PRIVILEGES;
Substitute the password you want to use for “newpwd” in the preceding examples. MySQL cannot tell you what the original password was, so you'll need to pick a new one.