Skip to main content

Posts

Showing posts with the label MySQL

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...

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...

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);

Compare 2 mysql databases

Below code will list all the tables with fields (& type) and total records in each table, so that we can verify whether fields are same in both database fields and record counts. <?php mysql_connect("localhost", "user1", "pwd1") or die(mysql_error()); mysql_select_db("db1") or die(mysql_error()); $tables = array('tbl1', 'tbl2','tbl3'); echo "<table  border='1' style='float: left'>"; foreach($tables as $tbl){   $sel = "SELECT COUNT(1) FROM $tbl";   $res = mysql_query($sel);   $rec = mysql_fetch_row($res);     echo "<tr><th colspan='2'>$tbl ($rec[0])</th></tr>";   $sel1 = "SHOW FIELDS FROM $tbl";   $res1 = mysql_query($sel1) or die($sel1.mysql_error());   while($rec1 = mysql_fetch_row($res1)){     echo "<tr><td>$rec1[0]</td><td>$rec1[1]</td></tr>";   } } ech...

SQL JOINS

Practice and crack the interviews on JOINS.

MySQL 4.1+ using old authentication

When I was working with XAMPP in Ubuntu and asked write PHP script to connect to remote MySQL server which is using PASSWORD hash function to save the password for user, and I found following error. Warning: mysql_connect() [function.mysql-connect]: Premature end of data (mysqlnd_wireprotocol.c:554) in path/to/the/file/where/connection/script/is/written/ Warning: mysql_connect() [function.mysql-connect]: OK packet 1 bytes shorter than expected in path/to/the/file/where/connection/script/is/written/ Warning: mysql_connect() [function.mysql-connect]: mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication. Please use an administration tool to reset your password with the command SET PASSWORD = PASSWORD('your_existing_password'). This will store a new, and more secure, hash value in mysql.user. If this user is used in other scripts executed by PHP 5.2 or earlier you might need to remove the old-passwords flag from your my.cnf file in path/to/the/file/where/conn...