Sunday, 26 October 2014

[MySql] Reset MySQL root password on Linux

1. Login to server as root/su

2. Stop the MySQL service
service mysqld stop
* Note : Be carefully, all your application will be temporarily unable to use mysql.
3. Start MySQL Safe mode with skip grant tables option
mysqld_safe --skip-grant-tables & 
(press ctrl+C to exit, if required)

4. Start the MySQL service
service mysqld start

5. Log into the MySQL server without any password
mysql -u root -p mysql

6. Reset the password for ‘root’ user
UPDATE user SET password=PASSWORD('new-password') where user='root';

7. Flush privileges
flush privileges;

8. Stop MySQL Safe mode
killall mysqld

9. Start the MySQL service again
service mysqld start

10. Try to Log-in with the new password
mysql -u root -p 
<enter new password when prompted>

Friday, 19 September 2014

MySQL: Allowed to remote connect to MySQL server

MySQL Error : Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server
How to fix it ?

STEP 1: Change mysql config :

vi /etc/mysql/my.cnf
Comment out following lines or edit to your client ip :
#bind-address           = 127.0.0.1
#skip-networking
Restart mysql server:
service mysql restart
STEP 2: Change GRANT privilege :

Login MySQL using command line .
Then run a command like below to grant access for user. Replace 'username' and 'password' with your username and password.
CREATE USER 'username'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' WITH GRANT OPTION;
* '%' mean you can remote access from any ip address, You can replace it with a specified ip address.

Then flush MySQL :
FLUSH PRIVILEGES;

If it's still not working. Let's check your server firewall (iptables ...) and client firewall (windows firewall, antivirus software)


PHP: Create big unique ID like Facebook or MongoDB

Most of web application using a relational database management system (MSQL, SQL ..etc) to store data as records. We need an unique id to use as a key Primary.
How to do it ?
* SQL AUTO INCREMENT a Field :
--> 1,2,3,4,5..... #anyone can count the number of your records and more

* PHP uniqid :
--> 541cf7f7d685e, 541cf7f7d685e, 541cf7f7d685e .....  #somewhat similar?

* Or use a custom function like this :

function gen_id() {
    return sprintf( '%04x%04x%04x%04x%04x%04x%04x%04x',
        // 32 bits for "time_low"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),

        // 16 bits for "time_mid"
        mt_rand( 0, 0xffff ),

        // 16 bits for "time_hi_and_version",
        // four most significant bits holds version number 4
        mt_rand( 0, 0x0fff ) | 0x4000,

        // 16 bits, 8 bits for "clk_seq_hi_res",
        // 8 bits for "clk_seq_low",
        // two most significant bits holds zero and one for variant DCE1.1
        mt_rand( 0, 0x3fff ) | 0x8000,

        // 48 bits for "node"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
    );
}
Result : 6800cd499f25434aac48c0bb51f46307, caa02c4831194690ad8458c3a4e07a0c, 7488a4c459d64f8dbacd2b79f44ed4bf ......

- differences
- professional (like facebook, google, MongoDB ...)
high performance
- unrestricted

Reference : PHP uuid lib

Tuesday, 1 April 2014

Linux: Delete all contents of file by only a command in vi editor

To clear the contents of a file is opening. Let's try folow the following command.

           :1,$d

Other basic useful command :
* Create a file
vi filename
* To exit vi and save changes:
         ZZ   or  :wq
* To exit vi without saving changes:
         :q!
* To edit file content :
         i
* Deleting Text :

*xdelete single character under cursor
 Nxdelete N characters, starting with character under cursor
 dwdelete the single word beginning with character under cursor
 dNwdelete N words beginning with character under cursor;
  e.g., d5w deletes 5 words
 Ddelete the remainder of the line, starting with current cursor position
*dddelete entire current line
 Ndd or dNddelete N lines, beginning with the current line;
  e.g., 5dd deletes 5 lines

* Searching Text:

 /stringsearch forward for occurrence of string in text
 ?stringsearch backward for occurrence of string in text
 nmove to next occurrence of search string
 Nmove to next occurrence of search string in opposite direction
* Screen Manipulation:

 ^fmove forward one screen
 ^bmove backward one screen
 ^dmove down (forward) one half screen
 ^umove up (back) one half screen
 ^lredraws the screen
 ^rredraws the screen, removing deleted lines

Excel: How to remove VBA password from excel file

If you want to remove VBA password protect excel file without password, please follow the following guidelines.

Step by step :
  1. Download a HEX editor (HxD Hex Editor)
  2. Open xls/xlsx file and Save As xls. Do this even if the file is xls, just to be sure
  3. Open xls file with a hex editor
  4. Search for DPB
  5. Replace DPB to DPx (Note: the editor overrides, not inserts. So pay attention)
  6. Save file
  7. Open file in Excel
  8. Click "Yes" if you get any message box. There will be a few
  9. Click Alt + F11
  10. Click Tools -> [Project Name] Properties
  11. Click Protection
  12. Enter a new password twice
  13. Save
  14. Close and open the file again
  15. Type your new password to get to the required information.

Saturday, 7 December 2013

Linux-vsftpd : restrict user to root directory

To avoid security issues or restrict user to root directory you have to limit users of vsftp to only their home directory. So how to do it ? 

Open vsftpd configuration file - /etc/vsftpd/vsftpd.conf :

Make sure following line exists and uncommented (add if not exists):

chroot_local_user=YES 

Save and close the file. Restart vsftpd service.

/etc/init.d/vsftpd restart 

 Done. Using a ftp client to check with some acccount.

Friday, 6 December 2013

Yii: Widget example and common errors

Widget is a flexible structure in yii. So how and where it can be used ?

Intended use : 

Create static blocks such as : header, footer, menu, sidebar blocks (other post, contact, ...)

Simple header widget example: 

Directory structure :
-Protected
---widgets
-----header
-------header.php
-------views
---------headerView.php
...

header.php
<?php
class header extends CWidget { //class must be same with file name and extended CWidget class
    public function init() {
            // do something
    }
    
    public function run() {
        //do something ...
        $model = Model::model()->findAll();
        $this->render('headerView',array('model'=>$model ));
    }
}
?>

headerView.php
<div id="topHeader">
          <!-- header content -->
          <?php var_dump($model); ?>
</div>

Using in layout :
<!DOCTYPE html>
<html dir="ltr" lang="en"> 
          <head>
          </head>
          <body>                  
                   <?php $this->widget('application.widgets.header.header'); ?>
                   <?php echo $content; ?>
          </body>
</html>

Common errors:

 "include(header.php): failed to open stream: No such file or directory"

How to solve :
1. Check alias path : "'application.widgets.header.header'" make it correct
2. Check class name in file header.php : make sure that it is same with file name.
3. Check other logic

Friday, 15 November 2013

CSS: Position fixed not working in IE

* IE 6 does not support position: fixed
* Other versions of IE (7,8,9) don't support position: fixed in quirks mode when you missing Doctype.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

Friday, 4 October 2013

Linux: how to find OS name and version

There are three way to show your linux server infomation :

 #1 
        $ cat /etc/*-release 

 #2 
        $ lsb_release -a 

 #3 
        $ cat /proc/version 

 Output example : 

 Linux version 2.6.32-358.11.1.el6.x86_64 (mockbuild@c6b7.bsys.dev.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC) ) #1 SMP Wed Jun 12 03:34:52 UTC 2013

Saturday, 21 September 2013