A script to reset the MySQL root password

It’s a pain if you ever forget your MySQL root password. Fortunately it’s a fairly straightforward process to reset it, here’s how:

pkill -9 mysqld;
echo "UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
FLUSH PRIVILEGES;" > /tmp/reset-pass.sql
mysqld_safe --init-file=/tmp/reset-pass.sql &
sleep 10
pkill -9 mysqld;

A bash script to reset the mysql root password

To make the process easier, I’ve wrapped these commands up in a script and put it on our open source respository here:

mysql_reset_root_password.sh script

Update: This script has been improved with Andy’s suggestion in the comments,  which is a simpler and more secure method.

killall -15 mysqld
read -s -p 'Enter a new root password: ' MYSQL_ROOT_PASSWORD
echo "UPDATE mysql.user SET Password=PASSWORD('$MYSQL_ROOT_PASSWORD') WHERE User='root';" | mysqld --bootstrap

Great British Businesses

With all the bankruptcies and job losses of recent months, and as we watch sterling slide against all major currencies, you’d be forgiven for thinking that UK plc is heading for the economic scrap heap. And in many ways it is – just look at our total government and personal debt, and the predicted budget deficits over the next few years. Yet, despite all this doom and gloom, there are some areas of the economy that have a bright future.

It may seem that all the UK has exported in the past few years is an army of borrow-to-let investors, but rather than focus on everything that was wrong with that, I’d like to look at what we’ve done right – and to celebrate some of the successful businesses that have been created by the real entrepreneurial spirit that exists in our country.

So, here are some world class companies that I’m proud to say are British:

  • Dyson – Reinvented the Vacuum Cleaner.
  • Imagine Technologies – Makes graphics chips for mobile devices, including the iPhone.
  • ARM – Makes microprocessors. Has gone from powering the humble Acorn computer, to nearly a quarter of all electronic devices in the world, including the iPhone.
  • HSBC – Although a multi-national bank, they are headquartered and listed in the UK, and have British heritage (founded by a Scot). The only major UK bank not to need bailing out as a result of the financial crisis. They even sold their Canary Warf offices at the peak of the boom, and bought them back recently for alleged £250 million profit.
  • Tesco – Like marmite, you’ll either love them or hate them, but there is no doubt they are good at what they do! Now the world’s fourth third biggest retailer, they have led the way in analysing their customer data (club card scheme) and retailing via the Internet. Watch out Wal-Mart!.
  • BBC – Although owned by the British tax payer, and some might say a little on the bloated side, they certainly produce some world leading content (Top Gear, Blue Planet, BBC News Online) and have pushed out some innovative technology over the years (BBC Micro, BBC iPlayer, Dirac Codec).
  • Rolls Royce – Have mastered manufacturing as a service by leasing their engines by the hour, and providing lucrative maintenance and repair contracts.

The list is going to start small, so please send me your suggestions and I’ll try to add them. I’d like to stick to British firms who have been truly innovative in recent years, rather than just listing out the FTSE 100.

Howto: Setup CloudFront as a Content Delivery Network

It’s actually incredibly easy to begin using a Content Delivery Network (CDN) such as Amazon’s new CloudFront service, and in this post I’m going to show you how.

Background

So what is a CDN and why use one? Well CDNs are essentially a global network of file servers that work together to serve static content such as images, flash, css and javascript files. They are useful if you want to serve up content faster to your users as the servers are strategically placed at edge locations all around the world and incoming requests are automatically routed to the server closest to the user. This reduces the latency of HTTP requests and makes pages feel “snappier”. They can also be useful to reduce load to your core servers.

Update!

Since writing this post I found there is a firefox extension which gives you a GUI interface into CloudFront. I haven’t tried it yet, but you can read about it here.

How to use Amazon’s S3 and CloudFront CDN

Simple Storage Service

If you haven’t already, signup for a CloudFront account with Amazon Web Services. You’ll also need an S3 account subscription, as the two work hand in hand, but Amazon should set this up automatically.

Download an S3 client / GUI such as:

Login to Amazon Web Services and download your access keys. There are two you need, the access key id and the secret access key. You can find these in Your Account > Access Identifiers.. You’ll then need to configure your chosen client to use these keys.

Open up our S3 client and create a new bucket. You should avoid using underscores in your bucket names (although they will technically work, you won’t be able to create a distribution later via CloudFront). You might want to read the full restrictions on bucket names first. I recommend you follow the additional instructions to conform with the DNS requirements.

Using the S3 client, upload some files. You should then be able to access them at either of the following urls (substituting your bucket name and filename as appropriate):

  • http://bucket-name.s3.amazonaws.com/filename.jpg [example]
  • http://s3.amazonaws.com/bucket-name/filename.jpg [example]

Cloud Front CDN

Now that your files are accessible on S3 the final step is to link your S3 bucket to a CloudFront “Domain Name”. This process is known as creating a distribution, and is actually pretty simple.

First download the CloudFront Curl Perl Script from here. Then set up a .aws-secrets file in your home directory that contains your account keys. Make sure it’s has 600 permissions. The contents of the file will look something like:

%awsSecretAccessKeys = (
    # primary account
    primary => {
        id => '<Your primary AWS Access Key ID>',
        key => '<Your primary Secret Access Key>',
    },
							
    # secondary account
    secondary => {
        id => '<Your secondary AWS Access Key ID>',
        key => '<Your secondary Secret Access Key>',
    },
);

Next create a text file with the XML instructions needed to create a distribution, it should look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<DistributionConfig xmlns="http://cloudfront.amazonaws.com/doc/2008-06-30/">
   <Origin>mybucket.s3.amazonaws.com</Origin>
   <CallerReference>20080930090000</CallerReference>
   <Comment>Creating my first distribution</Comment>
   <Enabled>true</Enabled>
</DistributionConfig>

Replace the origin with your bucket’s url. You’ll need to use the bucket-name.s3.amazonaws.com format. The caller reference is just a timestamp.

Save this file as create_request.xml and then run the following command to execute it:

./cfcurl.pl --keyname  -- -X POST -i -H "Content-Type:text/xml; charset=UTF-8" --upload-file create_request.xml https://cloudfront.amazonaws.com/2008-06-30/distribution

This command will return some XML, which, if successful, will contain the domain name you can use to access your files via the CloudFront CDN. NB: It can take a few minutes for this domain to become active in the DNS and so you should wait a while before trying it.

You can then access your files at http://unique-id.cloudfront.net/filename.jpg [example]

And that’s it! You’re now ready to use this domain to host your static files for your sites. You could go a step further by pointing a subdomain of your site as a CNAME record to this domain.