How to Dump SQL Result to a Text File Using cli

Just a quick tip on how to dump a sql result to a txt file.

mysql -e “select * from [table]” -u[user] -p [database] > sqlresult.txt

For example:

mysql -e "select * from orders where product_id = 15" -uroot -p  > sqlresult.txt

Related: Some MySQL cli Syntax ExamplesBasic MySQL Performance MonitoringMySQL Performance Tuning Tips

Updates Needed When Upgrading from Apache 2.2 to 2.4

I updated from Ubuntu 12.04 to Ubuntu 14.04 on a virtual private server.

When you do that update, Apache is updated from 2.2 to 2.4. Certain changes mean that until you update the site-available configuration files no web sites will work.

The quick checklist of what you have to do for each configuration

  1. update the file to add or edit the directory details (where /var/www/curiouscatnetwork.com/public_html/ is the path to the website files on your server)
    <Directory /var/www/curiouscatnetwork.com/public_html/>
       Require all granted
    </Directory>

    Remember if you have details needed for WordPress those instructions need to remain. I didn’t have a Directory area listed on my non-Wordpress sites.

  2. The files now need a .conf extension so move the rename/move the files to the new name
    sudo mv curiouscatnetwork.com curiouscatnetwork.com.conf
  3. Enable the new configuration
    sudo a2ensite curiouscatnetwork.com.conf
  4. Reload apache to reload the new configuration
    sudo service apache2 reload

More details from Linode.

The upgrade to Ubuntu 14.04 LTS was interrupted (I quit a window using the wrong command – oops). Luckily it wasn’t a big deal. I was able to delete the lock file.

sudo rm /var/lib/dpkg/lock

Then trying to update again

sudo apt-get dist-upgrade

gave a message telling me what command to use to have the upgrade continue.

sudo dpkg --configure -a

which seemed to work fine.

Related: Bash Profile Adjustments, Scrolling HistoryChecklist: Setting Up a New Domain on VPSModSecurity: Adjustments for WordPress

Reset Root Password on MySQL Database

How to reset the root password on a MySQL database.

Stop mysql using this command:

sudo /etc/init.d/mysql stop

Because you are not checking user privledges at this point, it’s safest to disable networking. Then, start the mysqld demon process using the –skip-grant-tables option using

sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &

Next, start the mysql client process using this command:

mysql -u root

from the mysql prompt execute this command to be able to change any password

FLUSH PRIVILEGES;

Then reset/update your password

SET PASSWORD FOR root@'localhost' = PASSWORD('password');

Related: Turn It Off and On Again (WordPress Loses Database Connection)Some MySQL cli syntax examplesKeeping Your Hosted Ubuntu Web Server Software Up to Date

Fix for When a WordPress Blog Stops Displaying Images

My WordPress network site stopped displaying uploaded images. I finally found something that works, though it is a lame “solution” in my opinion.

I found this advice and it worked for me. Insert the following in wp-includes/ms-files.php

adding ob_clean() before the readfile in ms-files.php

So you end up with:

// If we made it this far, just serve the file
ob_clean();
readfile( $file );
?>

Caching seemed to make the images “disappear” slowly (they didn’t all go at once and using different browsers they “disappeared” at different times). So when I fixed it they “reappeared” at different times (it seemed like cached ones stayed disappeared for awhile).

The advice mentions the image files being seen as “corrupted.” The files seem unlikely to actually be “corrupt” because these are images that worked for years and then all of a sudden every image for the sites “disappeared” (wouldn’t be displayed). I suppose it is possible the are corrupt in some detailed way and some update no longer allowed some previous “lax” checking to allow the non-compliant files but it seems more likely that some of thing has gone wrong (but somehow ob_clean takes care of it).

Related: WordPress error: Image could not be processed. Please go back and try again.Overcoming Conflicts Multiple Blog WordPress Network on One ServerBash Profile Adjustments, Scrolling History

Rsync to copy Files Between Servers and Computers

I found rsync when I wanted to use scp to copy files to a server but not overwrite files already there. Rsync is actually more efficient no matter what (it seems) but it is really great if there are a bunch of duplicate files (Rsync will just skip them).

To copy files from your current computer to a server:

rsync -azv -r ssh [directory] [username]@remotehost:[location]

$ sudo rsync -azv -e ssh directory-on-my-computer/ john@example.com:~/files/go/here

a = archive mode
z = compress file data
v = verbose
r = recurse through subdirectories and copy all of them

Compressing file data saves bandwidth so if that is an issue it is another big win over scp. And in my reading it seems rsync can restart a broken file transfer in process (while scp you have to redo the whole file transfer).

To copy from the server to your computer just reverse the order of the locations. And you can even just put in two addresses not on your current computer and copy between then.

As a reminder, I realize this blog is made up of stuff that is obvious to a large number of people. It is really aimed at me (so I can quickly find what I found before), and to a lessor extent others like me (who use cli some but are not system administrators or programmers to any significant extent).

Related: Making Sure You Don’t Run Out of Space on Your VPSBash Profile Adjustments (Scrolling History)Customizing the Command Prompt for Terminal in Ubuntu

Bad iTunes Ux and How to Submit a Podcast to iTunes

I find iTunes Ux to be horrible. My guess it is largely driven by people that think the useful web isn’t cool enough so lets break all the things that work in order to be different. Or maybe there is some other reason for the horrible Ux that I don’t understand.

iTunes doesn’t use urls which are super user friendly. And even when urls are lamely named they can be useful.

Trying to figure our using the UI of iTunes how to submit a podcast was impossible. using the iTunes help found nothing for submit podcast? Using Google to overcome Apple’s lame help found web pages on Apple telling you what to do. It read like 1990 UI navigation by text. Go here click here, then click, here then click here. How idiotic. On top of being idiotic and outdated it didn’t work the places to click didn’t exist.

With using 1997 technology (way before really but wasn’t so common long before 1997) just have

itunes.com/podcasts/submit

No help needed. Even if someone goes to help just give that clickable url and done. But no, in 2014 Apple still can’t do that.

Finally I found another url

https://buy.itunes.apple.com/WebObjects/MZFinance.woa/wa/publishPodcast

Super lame and unreadable but guess what clicking it works, unlike the various useless Apple webpages on clicking here and then clicking there and then clicking the next place which doesn’t exist.

Related: NASA You Have a Problem (point hair boss level idiotic Ux at NASA) Goldbergian Flash Fits Rube Goldberg Web Site1 in 7 new phones which are returned have “nothing wrong” with them (other than super bad usability)Customer Focus and Internet Travel Search (usability done well by Hipmunk)Making Life Difficult for CustomersPoor Web Site User Experience (Ux) for stock market dataNSF super lame usability failure

Turn It Off and On Again

I had a WordPress blog lose the database connection. As I tried to troubleshoot it I rapidly got to the point of thinking that maybe just rebooting the server would fix things – since no changes had been made that should cause the database connection to be lost. But also I figured I couldn’t be so lucky that such a simple thing would work.

I tried to log into the database using the cli and it was failing. MySQL was still running – according to top. I have automated security updates setup for the VPS server running Ubuntu. My thought now is those updates somehow messed things up to the extent the server database connections somehow wasn’t working. I sure hoped that was the case.

I rebooted and hoped.

After a bit I was able to see that it was working.

When I went to reboot I noticed I hadn’t rebooted in a long time. My guess is I might have the automated security updates setup wrong on this server (maybe it is installing more than just the security updates), I’ll check. I have not had this problem before with other servers, obviously breaking a database in this way would cause lots of problems on production machines so I tend to think it is more likely I messed something up than this is a likely outcome when using automated security updates (but such things are possible which is why I think places with full time system admins and important servers rely on manual updates with professionals watching everything to be sure nothing obvious breaks).

My feeling is the longer you go with not rebooting the server the more likely some issues are to crop up (but also every reboot is more like to result in some broken thing, right then, than doing nothing so it is a tradeoff). And I could be wrong in that feeling, it is just what I guess without much evidence to support my guess.

Anyway I was very happy turning it off and on again worked. Honestly I went ahead and updated the server before I rebooted, but my guess is just turning it off and on again would have worked.

Related: Making Sure You Don’t Run Out of Space on Your VPSBasic MySQL Performance MonitoringWordPress error: Image could not be processed. Please go back and try again.

Top with Better Display Options

Scout Realtime is a Ruby Gem that allows you to view top in the browser. One huge advantage is to view charts of activity over time.

Scout Realtime is open source and free.

Related: System Monitoring Tools for VPSBasic MySQL Performance MonitoringKeeping Your Hosted Ubuntu Web Server Software Up to Date

Make Your Bounce Rate More Meaningful

Nice post on what the operational definition (though it doesn’t use that term) of bounce rate is in Google Analytics and some ideas on how to fix bounce rate to be more meaningful (better fit your desired operational definition).

We often make mistakes because we use the data we have assuming it means something other than what it actually means. There suggestions let you make Google Analytics collect the data so it is more aligned to what you want to know.

Essentially bounce rate without any adjustment is operationally defined by Google to mean the visitor doesn’t click a link to another page on your site that has Google Analytic code (so it can register the click).

They suggested using

setTimeout("_gaq.push(['_trackEvent', '15_seconds', 'read'])", 15000);

inside your Google analytic tracking code. They also updated the post with an idea from a Hacker News comment to use scroll tracking as a better option than just timing if they stay on the page at least 15 seconds. The scroll tracking lets you count it as a bounce only if they don’t scroll down and they don’t click to a new page on your site.

Which data is more useful depends on your situation, what you want to know and what you want to do with the data you collect.

Related: Setting Up Multiple WordPress Blog Network on One Server, How To Overcome ConflictsWordPress error, Image could not be processed. Please go back and try again.ModSecurity Adjustments for WordPressSimple Process to Claim Authorship of Pages with Google

Don’t Copy-Paste Directly from Website to Terminal

Good reminder and discussion on Hacker News about the danger of copy-paste from a website into terminal.

When copying from a website it is easy for hidden text to be included in the clipboard. If you paste that into terminal it can be executed before you see the code. Including new line commands in the copied text will automatically execute the commands (in most terminal applications).

The best solution is to paste the clipboard content into something to view the text before pasting into clipboard. The best way is probably to recopy it. I use this double copy method to get rid of formatting I don’t want (when the clipboard includes things like font styling info you don’t want – not relavent when pasting into terminal but the pasting [into say vim, which won't include the extra formatting details] and recopying part is similar). It may be possible to have text hidden (write it in a way where it won’t show up on the screen but is in the clipboard – using some tricky unicode characters or something).

Some people put # before pasting into clipboard but that only protects the first “line.” Any new lines could still be run without you seeing them.

The discussion warns against a malicious website intentionally creating a problem but if you paste in multiple lines there is also just the danger from the lines executing immediately when you wanted to edit the line before it was executed.

Related: Customizing the Command Prompt for TerminalBash Profile Adjustments, Scrolling HistoryAdding a Key to Your Server for SSH Login