Monthly Archives: May 2014

Reset MySQL Root/Admin/AnyUser forgoten password using SSH

Stop MySQL

#For Ubuntu or Debian
sudo /etc/init.d/mysql stop

#For CentOS, Fedora, and RHEL
sudo /etc/init.d/mysqld stop

Start MySQL in safe mode
start MySQL but skip the user privileges table, you will need to have sudo access for these commands so you don’t need to worry about any user being able to reset the MySQL root password:

sudo mysqld_safe --skip-grant-tables &
#ampersand (&) at the end of the command is required.

Login MySQL
No password is required at this stage as when we started MySQL we skipped the user privileges table

mysql> mysql -u root

Tell MySQL which database to use

mysql> use mysql;

Check list of users from user

mysql> select User, Password from user;

Reset Password

mysql> update user set password=PASSWORD("mynewpassword") where User='root';

Flush the privileges

mysql> flush privileges;

Exit MySQL

mysql> quit

Stop and start MySQL

#On Ubuntu and Debian:
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start

#On CentOS and Fedora and RHEL:
sudo /etc/init.d/mysqld stop
sudo /etc/init.d/mysqld start

Login with new Password

mysql -u root -p

Motivation behind using Object Oriented Programming (OOP)

Simplicity
One important way to deal with complexity is to reduce it to something simpler.
Divide and conquer principle: Divide the original problem into separate sub-problems that can be solved individually.

Maintainability
We must also be able to maintain and extend the solution in an evolving world. So it is important that the use OOP not only solve the problem, but also result in a program that can be maintained.

Reusability
When you divide problems into sub-problems, you will sometimes see similar sub-problems. Then it would be nicer to reuse a similar solution.

Flexibility
It is also desirable that your software is flexible. You may for example need to deliver several variants of your software to different customers.
Or you may need to reconfigure the software to a changed context. Also some of your shared sub-solutions may need to go into contexts that are not completely the same.

Difference between Class and Object

Class vs Object
A class is a Blue print of an object. a general concept (like an Animal), an object is a very specific embodiment of that class, with a limited lifespan (like a lion, cat, or a zebra).

Class

  1. Definition: Class is mechanism of binding data members and associated methods in a single unit.
  2. Existence: It is logical existence
  3. Lifespan: Classes do not have a lifespan
  4. Memory Allocation: Memory space is not allocated , when it is created.
  5. Declaration/Definition: Definition is created once.

Object

  1. Definition: Instance of class or variable of class.
  2. Existence: It is physical existence
  3. Lifespan: Objects have a lifespan
  4. Memory Allocation: Memory space is allocated, when it is created.
  5. Declaration/Definition: it is created many time as you require.

Late Static Bindings (PHP 5.3)

// Limitation of Self:: in PHP 5.2 
// before Late Static Binding PHP 5.3
class A {
    public static function who() {
        echo __CLASS__;
    }
    public static function test() {
        self::who();
    }
}

class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}

B::test(); // This will output: A

// Late Static Binding in PHP 5.3
class A {
    public static function who() {
        echo __CLASS__;
    }
    public static function test() {
        static::who(); // Late Static Bindings
    }
}

class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}

B::test(); // This will output: B

Difference between Abstraction and Encapsulation

Encapsulation : Wrapping up of data and methods into a single unit is called Encapsulation (e.g. Class)
Abstraction : It is an act of representing only the essential things without including background details. (e.g. Interface)

Real world example
Abstraction : You’ll never buy a “device”, but always buy something more specific : iPhone, Nokia 3310… Here, iPhone, and N3310 are concrete things, device is abstract.
Encapsulation : you’ve got several devices, all of them have got an USB port. You don’t know what kind of printed circuit they have, you just have to know you’ll be able to plug an USB cable onto.

Abstraction is a concept, which is allowed by encapsulation. You can do encapsulation without using abstraction, but if you wanna use some abstraction in your projects, you’ll need encapsulation.

Benefits of Abstraction :
Advantages of abstraction are the hiding of implementation details, component reuse, extensibility, and testability.

When we hide implementation details, we reveal a cleaner, more comprehensible and usable interface to our users.
We are separating our interface from our implementation, and this makes component reuse more practical.

Benefits of Encapsulation :

  • In Encapsulation fields of a class can be read-only or can be write-only.
  • A class can have control over in its fields.
  • A class can change data type of its fields anytime but users of this class do not need to change any code.

We can see the use of Encapsulation by using properties. The property has two accessor get and set. The get accessor returns the value of the some property field. The set accessor sets the value of the some property field with the contents of “value”.  Properties can be made read-only. This is accomplished by having only a get accessor in the property implementation.