Skip to main content

Posts

Connect MySQL Remotely, Amazon EC2 using MySQL Workbench

Login to AWS Management Console. Under the security group, add inbound rule for MySQL. First login to EC2 instance using SSH, then login to mysql  mysql -hlocalhost -uroot -p provide the password. Once you are in mysql prompt CREATE USER 'testuser'@'%' IDENTIFIED BY 'testpwd' GRANT ALL PRIVILEGES ON *.* TO 'testuser'@'%' WITH GRANT OPTION FLUSH PRIVILEGES Note: Setting host to '%' may be harmful, you can set your IP address to access the MySQL from your IP address only Now open the my.cnf file from /etc/mysql and search for bind-address, the default value will be bind-address = 127.0.0.1 change it to  bind-address = 0.0.0.0 That's it now go to MySQL workbench, create connection with Host: Username: testuser  

MySQL Simple JOINS

Simple Example : Lets say you have a Students table, and a Lockers table. Each student can be assigned to a locker, so there is a " Locker Number " column in the student table. More than one student could potentially be in a single locker, but especially at the  beginning  of the school year, you may have some incoming students without lockers and some lockers that have no students assigned. For the sake of this example, lets say you have  100 students , 70 of which have lockers. You have a total of  50 lockers , 40 of which have at least 1 student and 10 lockers have no student. INNER JOIN  is equivalent to " show me all students with lockers ". Any students without lockers, or any lockers without students are missing. Returns 70 rows LEFT OUTER JOIN  would be " show me all students, with their corresponding locker if they have one ". This might be a general student list, or could be used to identify students with no locker. Returns 100 r...

Find relative duration of an event using PHP for given date time (seconds ago, minutes ago, hours ago, days ago, weeks ago, months ago, years ago)

You might have seen that many of the live applications shows the time relative to when it was posted as seconds ago, minutes ago, hours ago, days ago, weeks ago, month ago, year ago and so on. Instead of showing the whole date and time of any possible action on the application it is shown with smaller units, more simpler ones like a minute ago, 15 minutes and 10 seconds ago etc. Here is how we achieve it using PHP, below are two methods that can be used, it requires a Date Time string in YYYY-MM-DD HH:II:SS format. Method One: /** * returns the time ago in string * @param string $date_time * * @return string */ function timeAgo($date_time) { $time_ago = strtotime($date_time); $cur_time = time(); $time_elapsed = $cur_time - $time_ago; $seconds = $time_elapsed; $minutes = round($time_elapsed / 60); $hours = round($time_elapsed / 3600); $days = round($time_elapsed / 86400); $weeks = r...

Recovering an InnoDB table from only an .ibd file

Sometime you may need to recover a table when all you have is the .ibd file. In this case, if you try to load it into a new instance, your likely to encounter some errors about the table id not matching. And there is not really a way around this. However, I’ve found two work-arounds for this: Note: You will need the .ibd file and the CREATE TABLE statement for each table you want to recover using these methods. Simulate the internal InnoDB table counter. That is, create work tables (with innodb_file_per_table enabled) until you have the internal pointer of table id equal to (1 – id_of_ibd_table_you_need_to_restore). (See Method #1) Manually hex edit the .ibd file, changing the table id. (See Method #2) *Note: There are some internal structures with this meta information, so you’ll need to dump/import that data after you get it loaded, so you avoid unpleasantries that will inevitably show their face. Method #1 – Create work tables Start up clean/fresh instance of...

Convert Date Time to GMT/UTC

If you want to convert any date time to GMT/UTC for given timezone (observing DST/not). /**      * coverts the given date time to GMT/UTC based on timezone provided      * @param string $date      * @param string $timezone      * @return string      */     function getGMT($date, $timezone){         date_default_timezone_set("UTC");         $daylight_savings_offset_in_seconds = timezone_offset_get( timezone_open( $timezone ), new DateTime() );         return $new_date = date('Y-m-d H:i:s', strtotime('-'.$daylight_savings_offset_in_seconds.' seconds', strtotime($date)));     } $date = "2014-11-30 23:50:00"; //yyyy-mm-dd //$date = "11/30/2014 23:50:00"; //mm/dd/yyyy //$date = "30-11-2014 23:50:00"; //dd-mm-yyyy $timezone = "Asia/Kolkata"; //$timezon...

Find Random Coordinates in the proximity

If you are asked to find the random coordinates (latitude & longitude) considering that your latitude & longitude as the center and with in the given proximity (radius), then this service will help to get one such coordinate. An example, you are at X location (17.414472, 78.449024) and radius 1 mile.  /**      * picks the random latitude & longitude with respect to given within the provided radius      *      * @param array $centre    (lat, lng)      * @param number $radius                 * @return array:      */     function getCoordinates($centre, $radius)     {         $radius_earth = 3959; // miles                   ...

Random Numbers or Password or Pin in MySQL

To obtain a random integer R in the range i <= R < j, use the expression FLOOR(i + RAND() * (j – i)). For example, to obtain a random integer in the range the range 7 <= R < 12, you could use the following statement: SELECT FLOOR(7 + (RAND() * 5)); To generate between 1000 to 9999, i.e., 4 digits then you can do the following using update query    UPDATE users SET password = FLOOR(1000 + RAND() * 8999);