Category: MySQL

forget mySQL root password

By สุดเดช, 19 กรกฎาคม 2010 15:31

หายหน้าไปน๊านนาน หลายท่านคงนึกว่าคนเขียน ม่องไปล่ะ
ป่าวครับไปเตรียมตัวกับหุ้นส่วน(เพื่อนกาก) เตรียมเปิด กิจการที่ถนัด กันอยู่
ซักพักคงได้เห็นกันวันนี้ก็มีปัญหา แล้วก็หาข้อมูลจนทำได้เลยนำมาให้เพื่อน ๆ ดูน่ะครับ
เป็นปัญหาของตัว mysql ที่มีคนแย่งไป (ได้ยังไงไม่รู้) เลยต้องไปเอาคืนมาเพื่อให้มันทำงานได้ปกติสุข
วันนี้เลยเป็นวันที่ แจ่มอุราอีกวัน ทำงานได้อย่างฟลุ๊ก ๆ  :D จัดไปลุ๊ยยย
เพิ่ม ลงไปในไฟล์ my.cnf ตามตัวอย่าง

[mysqld]
skip-grant-table 

จากนั้นให้ restart service mysql
ต่อด้วยคำสั่ง

mysqladmin -u root flush-privileges password "$password"

จากนั้นเข้าไปที่ mysql โดยการพิมพ์

mysql 

พอเข้ามาใน mysql แล้ว
ให้เลือก database mysql โดยคำสั่ง

use mysql

ตามด้วยสองคำสั่งนี้

UPDATE user SET password='$password' WHERE user='root';
GRANT ALL PRIVILEGES ON mysql TO root @'%' IDENTIFIED BY 'localhost';

จากนั้นก็ออกจากโปรแกรม แล้ว ลบ skip-grant-table ออกจาก ไฟล์ my.cnf
แล้วสั่ง restart mysql

จากนั้นก็ จัดได้เลยครับแจ่ม

หมายเหตุ : ต้องใช้สิทธิ์ su , root ทำงานเท่านั้น OS ที่ทำการทดสอบ คือ debian-linux-gnu
$password = รหัสผ่านของคุณ

Bookmark and Share

Crontab backup database

By สุดเดช, 27 กุมภาพันธ์ 2009 15:16

จากเมื่อวาน << คลิ๊กเพื่อดูเรื่องที่ต่อเนื่อง

วันนี้จะบอกวิธีการติดตั้ง Crontab กันน่ะคับ
เริ่มจากการสร้างไฟล์ Crontab ขึ้นมาก่อนน่ะคับ

15 4 * * * php -q /home/backup/rbackup.php > /dev/null 2>&1

backup_crontab

ความหมายของมันก็คือ ทุก ๆ วัน เวลา ตี 4.15 ให้ทำการ รัน สริปต์ ที่ชื่อว่า rbackup.php
เมื่อสร้างเสร็จแล้ว คราวนี้ก็มาดูผลการทำงาน ของ script กันน่ะคับ

backup_ssh

backup_mail2

backup_mail3

backup_mail4

ขอบคุณทุกท่าน ที่ดูแล้ว จะนำไปให้ให้เกิดประโยชน์ต่อไป
ขอบคุณทุกท่าน ที่เอาไปใช้แล้วให้เครดิตผม

กันลืม

crontab filename       	การนำเอาคำสั่ง crontab เข้ามาจาก ไฟล์อื่น

crontab -e                แก้ไข crontab ปัจจุบัน

crontab -l                 ดูคำสั่ง crontab ทั้งหมดที่มีอยู่

crontab -r                 ลบคำสั่ง crontab ที่มีทั้งหมด

crontab -u user          เป็นคำสั่งของผู้ดูแลระบบเท่านั้น(administrators) เพื่อใช้ดู  แก้ไข ลบ crontab ของ user แต่ล่ะคน

By default cron jobs sends a email to the user account executing the cronjob.  If this is not needed put the following command At the end of the cron job line  .

 >/dev/null 2>&1

****
15 * * * * rm -rf /var/www/html/xxx.com/ocache/*.png > /dev/null 2>&1

sp thx[1]: NECTEC
sp thx[2]: Thai Admin

Bookmark and Share

php script backup database

By สุดเดช, 26 กุมภาพันธ์ 2009 20:17

วันนี้ผมจะมานำเสนอ การ Backup database โดยใช้ php น่ะคับ

เป็นการ backup ข้อมูลผ่าน สริปต์ โดยใช้ ตัว crontab ใน Linux เป็นตัวเรียก สริปต์นี้

ใ้ห้ทำงานน่ะคับ วันนี้จะนำเสนอ ในเรื่องของ สริปต์กันก่อนน่ะคัีบ

ผมได้ เรียนรู้บางคำสั่ง จาก อาจารย์ เปิ้ล แอบ เซิฟ  และ การโค้ดดิ้ง แนะแนวโดยพี่แม๊กมอไซค์คับ

โดยการ backup ในครั้งนี้ เมื่อทำการแบ๊กอัพ เสร็จแล้ว มันจะส่งเมล์ไปบอก ยัง ผู้ดูแลระบบ
ในครั้งต่อไปผมจะ นำ คำสั่ง พร้อมกับ ภาพ มาประกอบให้ดูกันน่ะคับ
ขอบคุณทุกท่าน ที่ดูแล้ว จะนำไปให้ให้เกิดประโยชน์ต่อไป
ขอบคุณทุกท่าน ที่เอาไปใช้แล้วให้เครดิตผม

สังคมน่าอยู่เพราะว่ามีคนมีน้ำใจ

sp thx[1]: NECTEC
sp thx[2]: Thai Admin

<?

class BackUp{

function WhatBackup($whatBackup){
if (    (!$whatBackup) || ($whatBackup == "")    )    {
die("Specify folder to backup");
}
$this->whatBackup    = $whatBackup;
}

###    Make dir if not exists    ###
function WhereBackup($whereBackup){
$this->whereBackup    = $whereBackup;
}

###    Build FileName    ###
function FileName($fileName){
$today        = date("Y_m_d_H-i");
$fileName    = (    (!$fileName) || ($fileName == "")    ) ? $today : $fileName;
$this->fileName    = $fileName;
}
###    Do It    ###
function Backup(){

$today        = date("d-m-Y, H:i:s");
$comm         = "tar cfvz ".$this->whereBackup."/phpbb_".$this->fileName.".tar.gz ".$this->whatBackup;
###    If u want All output, use passthru() function    ###

### Send E-mail ###

$run        = exec($comm);
$msg        = ($run != "") ? "Backup OK" : "Backup FAILED";

$to  ="webxxx@mail.com";

$subject = "Backup system Database ip xxx [phpbb]";

$message = "Backup System Report Server : ip xxx <br />";
$message .="Date : ".$today ."<br />";
$message .="Status : ".$msg."<br />";
$message .="What Backup : ".$this->whatBackup."<br />";
$message .="Where Backup : ".$this->whereBackup."<br />";
$message .="File name : phpbb_".$this->fileName.".tar.gz";

$headers  = "MIME-Version: 1.0" . "rn";
$headers .= "Content-type: text/html; charset=utf-8" . "rn";
$headers .= "From: email@mail.com" . "rn";
$headers .= "X-Mailer: PHP/".phpversion();
@mail($to, $subject, $message, $headers);
exec("sleep 30");
}

}

?>
Bookmark and Share

TRUNCATE TABLES ALL

By สุดเดช, 19 กุมภาพันธ์ 2009 19:25

เมื่อผู้พัฒนา เรื่องเข้าใจ ในการทำงานของ คลาส ดาต้าเบส แล้ว
ต่อจากนี้ผมจะเอาตัวที่จัดการ การทำงาน ของ MySQL ตัวหนึ่งมาให้รู้กัน
นั้นคือ คำสั่ง TRUNCATE นั้นเองการทำงานของคำสั่งนี่คือการทำให้ Table ที่ทุกสั่งนั้น จะเป็น EMPTY ทั้งหมด
ในโปรแกรม phpmyadmin นั้นมีอยู่แล้ว แต่ว่า ผมอยากเขียนขึ้นมาใหม่อ่ะ ขี้เกียจไปเข้ามันช้า

มาดูการเขียนกันคับ
รูปแบบคำสั่งของมันจะเป็นแบบนี้คับ และ เพิ่มเติมให้อีกอันน่ะคับ นั้นก็คือตัว SHOW TABLES;

TRUNCATE TABLE ชื่อเทเบิ้ล;

ex TRUNCATE TABLE tbl_model;

งั้นมาดูโค้ดที่ผมเขียนกันเลยน่ะคับ

ขั้นตอนแรกก็ include ไฟล์ที่ชื่อว่า class.mysql.php เข้ามาก่อนน่ะคับ
แ้ล้วไปตั้ง ค่า config ที่ตัว constant กันก่อนคับ ลอง ๆ หาดู จะได้คล่อง
ถ้าติดตรงไหนก็ถามผมได้คับยินดีตอบคับ

<?
include("class.mysql.php");

$db = new db;
$db->connectdb(DB_NAME,DB_USERNAME,DB_PASSWORD);

$tables=$_GET["tables"];
if($tables){
if($tables=="all"){

$sql=$db->select_query("show tables;");
while(list($query_tables)=$db->fetch($sql)){
$db->select_query("TRUNCATE TABLE $query_tables;");
}
echo"TRUNCATE TABLE ALL";
header("refresh: 3; url=truncate_table.php");
}else{
$check=$db->select_query("TRUNCATE TABLE $tables;");
if($check){
echo"System :: <br />";
echo"<u>TRUNCATE TABLE $tables;</u><br />";
echo" Finish<br />";
header("refresh: 3; url=truncate_table.php");
}else{
echo"Have not your select table";
header("refresh: 3; url=truncate_table.php");
}
}
}else{
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TRUNCATE TABLE </title>
</head>

<body>
<h1>Select Truncate Table</h1>
<ul>
<?
$sql=$db->select_query("show tables;");
while(list($show_tables)=$db->fetch($sql)){
?>
<li>
<a href="?tables=<?=$show_tables;?>" onclick="javascript:return confirm('TRUNCATE TABLE <?=$show_tables;?>;');">
<?=$show_tables;?>
</a>
</li>
<?
}
?>
</ul>
<a href="?tables=all" onclick="javascript:return confirm('TRUNCATE TABLE ALL;');">
[TRUNCATE TABLES ALL]</a>
</body>
</html>
<?
}
?>

RESULT :

truncate_table

ใครมีอะไรที่เจ๋ง ๆ ก็แนะนำกันหน่อยน่ะคับ โลกใบนี้หาความรู้ได้ทุกวันจริง ๆ ฮ าๆ

Bookmark and Share

database class

เขียนคลาสขึ้นมาเพื่อควบคุมการใช้งาน DATABASE ได้อย่างมีประสิทธิ์ภาพ
ขอขอบคุณ พี่แม๊กมอไซค์ สำหรับโค้ดบางส่วนในการพัฒนางานในครั้งนี้

class db{
var $host = DB_HOST;
var $database;
var $connect_db;
var $selectdb;
var $db;
var $sql;
var $table;
var $where;
public function connectdb($db_name="database",$user="username",$pwd="password"){
$this->database = $db_name;
$this->username = $user;
$this->password = $pwd;
$this->connect_db = mysql_pconnect($this->host, $this->username, $this->password ) or $this->_error();
$this->db = mysql_select_db ( $this->database, $this->connect_db) or $this->_error();

mysql_query("SET NAMES UTF8");
mysql_query("SET character_set_results=utf8");
return true;
}
//// MYSQL Query ////
function select_query($sql="sql"){
if ($result = mysql_query($sql)){
return $result;
}else{
$this->_error();
return false;
}
}
//// MYSQL Result ////
function fetch($sql="sql"){
if ($result = mysql_fetch_array($sql)){
return $result;
}else{
$this->_error();
return false;
}
}
} // close class

จะมีงานต่อจากนี้ ให้ผู้ที่จะใช้ในการพัฒนา ตั้งชื่อไฟล์นี้ไว้ว่า class.mysql.php คับผม

Bookmark and Share

Enum Type

By สุดเดช, 7 มกราคม 2009 22:07

The ENUM Type

An ENUM is a string object with a value chosen from a list of allowed values that are enumerated explicitly in the column specification at table creation time.

For example, a column specified as ENUM('one', 'two', 'three') can have any of the values shown here. The index of each value is also shown:

enum1

An enumeration can have a maximum of 65,535 elements.

Here is an working example that ensures you that you will get the values inside a enum field in your mysql table.


function enum($table, $field)
{
$result = @mysql_query("show columns from {$table} like \"$field\"");
$result  = @mysql_fetch_assoc($result);
if($result["Type"])
{
preg_match("/(enum\((.*?)\))/", $result["Type"], $enumArray);
$getEnumSet = explode("'", $enumArray["2"]);
$getEnumSet = preg_replace("/,/", "", $getEnumSet);
$enumFields = array();
foreach($getEnumSet as $enumFieldValue)
{
if($enumFieldValue)
{
$enumFields[] = $enumFieldValue;
}
}
return $enumFields;
}
return "Unable to get enum FIELD {$field} from table {$table}";
}

// Returns an array set of all values of a enum field.
print_r(enum("MY_TABLE_NAME","MY_FIELD_NAME"));

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

Bookmark and Share

Panorama theme by Themocracy