Recovering Corrupted InnoDB Table

Corrupted MySQL database:

If you suspect data corruption in a MySQL database,

First make a copy of the database for safe keeping:

mysqldump –user=root post_planner > /tmp/post_plannerdb.sql

Prove it:

To prove that the database is corrupted, do:

mysql> check database

Find the table:

To detemine which table has corruption, do this for each table that you suspect:

mysql> check table post_planner.posted;
+—————————-+—————-+
| Table | Op | Msg_type | Msg_text |
+—————————-+—————-+
| post_planner.posted | check | Error | Can’t open table |
| post_planner.posted | check | error | Corrupt |
+—————————-+—————-+
2 rows in set (0.01 sec)

Once you know that the table is corrupt, you need to recover it. This is easy in MyISAM tables with the CHECK TABLEand REPAIR TABLE, but that doesn’t work with InnoDB. InnoDB tables can be checked with CHECK TABLE, but not repaired with REPAIR TABLE.

Copying over the good data to a new databases:

Here is what I did to fix it:

I created a second table called posted2 like this:

create table posted2 like posted;

SO that gets you the structure. Now lets see what we can do about getting the data:

mysql> insert ignore into posted2 select * from posted limit 1000000;
Query OK, 1000000 rows affected (0.00 sec)
Records: 1000000 Duplicates: 0 Warnings: 0

Keep increasing the limit until you see that the number of Records is fewer than the number you requested. At this point, do the following:

select max(id) from posted2;

This will give you how many records were successfully imported before it encountered corruption. Then you increment through with a the rest of the data using the first record that is not corrupted. You might have to try skipping one by selecting all records that are greater than the last known good record +1. It that fails, skip two on the next attempt. The command to skip the records basically looks like this:

mysql> insert ignore into posted2 select * from posted where ID>1000001;
Query OK, 9999999 rows affected (0.00 sec)

If you encounter more corruption, lather, rinse, repeat

Posted in Linux | Comments Off on Recovering Corrupted InnoDB Table

Restricting sshd by username

Step # 1: Open sshd_config file

# vi /etc/ssh/sshd_config

Step # 2: Specify a user

Only allow user king to login by adding following line:
AllowUsers king

Step # 3: Restart sshd

Save and close the file. In the above example, user vivek has already been created on the system. Now just restart sshd:
# service sshd restart

Posted in Linux | Comments Off on Restricting sshd by username

Changing the MySQL timezone via PHP

If you don’t have root access in your mysql instance and want to change the time_zone, you’ll have to change it in the code:

To have the time zone setup as you would like, you would you need to execute the following command each time you make a connection:
SET time_zone=’-3:00′

Here is an example of how to achieve that in PHP:

$offset = ‘-3:00’
$db = new \PDO(‘mysql:host=localhost;dbname=test’, ‘dbuser’, ‘dbpassword’);
$db->exec(“SET time_zone=’$offset’;”);

To verify:

mysql> select now()

Posted in Linux | Comments Off on Changing the MySQL timezone via PHP

Letting WordPress users install themes, plugins without FTP credentials

Quick answer: Put this in the user’s wp-config.php file:
define(‘FS_METHOD’, ‘direct’);

If you are having trouble updating the core wordpress, remember that no matter what the permissions are, wordpress needs it to be owned by apache:apache.

Posted in Linux | Comments Off on Letting WordPress users install themes, plugins without FTP credentials

Redirecting MySQL

To redirect traffic from one db to another (as a temporary measure if you have multiple places in your app to change the login data) you might try this:

iptables -t nat -A PREROUTING -p tcp –dport 3306 -j DNAT –to :3306

I ran across this when a customer wanted to redirect traffic from the mysql instance on his server to the CloudDB that he was creating. He wanted to make sure that if he missed an update in the app, he stil got data. I definitely put the YMMV with this one!

Posted in Linux | Comments Off on Redirecting MySQL

Which script sent this email

Sometimes you have multiple php scripts that send email and want to find out which one sent a specific email. I followed the instructions here:

http://www.iezzi.ch/archives/217

Worked like a charm.

Posted in Linux | Leave a comment

Browser-Side Caching with Apache

Browser-side caching relies on HTTP headers to know whether or not the content it has in its local cache is still valid. If it is, then it uses the local copy, rather than downloading the file from the server again. Many sites like whysoslow.com will report that you’re missing this HTTP header on your static files, which is normal, since you aren’t explicitly setting that in the files, since they’re mostly images and the like. In order to improve browser-side caching of your static content you could add mod_expires to your Apache configuration. This would add the necessary HTTP headers to make browser-side caching of your static content possible.

Install mod_expires and add the following to the .htaccess:

# Deflate output configuration
#
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/javascript
#AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

ExpiresActive On
ExpiresDefault “access plus 1 month”
ExpiresByType image/png “access plus 1 month”
ExpiresByType image/gif “access plus 1 month”
ExpiresByType image/jpeg “access plus 1 month”
ExpiresByType text/css “access plus 1 month”

AddType image/x-icon .ico
ExpiresByType image/x-icon “access plus 1 year”
ExpiresByType image/ico “access plus 1 years”

Posted in Apache, Linux | Leave a comment

Magento Notes

Deleting magento cache:

You may safely delete the content of var/cache/* and var/session/* or use the “Refresh Cache” functionality within the Admin.

To determine magento version:
include ‘app/Mage.php’;
php > echo Mage::getVersion();
1.6.2.0
php >exit();

Posted in Uncategorized | Leave a comment

Subversion Stuff

To establish a subversion repository anew:

svnadmin create /var/svn/myrepos
svn mkdir -m “creating the trunk dir” file:///var/svn/myrepos/repo1
svn import -m “Adding first directory” /var/www/vhosts/mydomain.com/wordpress file:///var/svn/myrepos/repo1
**Note that if you don’t specify a directory to copy, subversion is happy to add the one you are in to the repo.
svnadmin verify /var/svn/myrepos/

Subversion is not so forthcoming with the meaning of errors. If there is corruption in one of the revisions, it will just output the error when you do a

svnadmin verify repos/
[…]
* Verified revision 907.
* Verified revision 908.
* Verified revision 909.
svnadmin: Corrupt representation ‘907 21815 45 30922 158d3e72732f45bf6f02919b22fc899a’
svnadmin: Malformed representation header

This means that Revision 909 is good but Revision 910 is corrupted. The next step is to dump all the repos from 911 through the end of the repo which is in this case revision 947.

$ svnadmin dump –incremental -r 911:947 master/ > repo_name.svn_dump
* Dumped revision 911.
* Dumped revision 912.
* Dumped revision 913.
[…]
* Dumped revision 947.

svnadmin create /path/to/newrepo
svnadmin load /path/to/newrepoy < repo_name.svn_dump svnadmin verify repos/ * Verified revision 0. * Verified revision 1. * Verified revision 2. [...] * Verified revision 945. * Verified revision 946. * Verified revision 947.

Posted in Linux | Leave a comment

Grepping (and egrepping) with Style

Grep is a powerful tool. It can do many things well. It can also be used in conjunction with other tools. If you are new to grep, it is a supercharged search tool. The basic format of the command is:

grep [options] PATTERN [FILE…]

Many options are available and described in the man page, so I won’t go into them here. I will just list some of my favorite grep commands. (who am I kidding? I just want a place to copy them from when I am not at my own computer)

#This command searches for a string and includes the preceding 5 lines and the 5 lines that follow it and shows the line numbers as well.

grep -n -B5 -A5 searchstring /tmp/filename

#This command uses grep to traverse the output from a find command.
find . -exec grep searchstring {} \; -print

#This command allows you to output a sorted list of connections from the month of Mar 2013
grep Mar/2013 /var/log/apache2/access.log |  awk ‘{ print $1 }’ | sort -n | uniq -c | sort -rn | head

#Finding warnings and errors in your log
egrep -w ‘warning|error|critical’ /var/log/messages

Posted in Linux | Leave a comment