Restoring broken MySQL Replication

August 18th, 2010

Sooner or later your MySQL replication will probably go boom. Here’s how to fix it:

  1. Stop Slave;
  2. SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;
  3. Start Slave;
  4. Show slave Status;

If this is a constant issue, you can use the slave-skip-errors in the my.cnf file, skipping either specific errors or all errors.

Getting Lost MySQL Root Password

August 18th, 2010

Its probably happened to everyone at one time or another, in the set up of a new box, forgetting what the heck you set that MySQL root password to. All is not lost, as long as you have root on your linux box! This is on a Red Hat box, your mysql start/stop commands may very by distro.

How to reset that root password:

Switch to root on box

Verify MySQL is running

ps aux | grep mysqld

If it is running kill it using the command below as it should provide a cleaner exit than killing the pid

/sbin/service mysqld stop

Now restart the mysqld process skipping over the grant tables

mysqld_safe –skip-grant-tables

Now verify mysqld started up again, if it didn’t you have bigger problems

ps aux | grep mysqld

If it is running connect as root, no password needed since we skipped the grant tables

mysql –user=root mysql

Now update the root password

UPDATE user SET Password = PASSWORD(‘new-password’) WHERE User = ‘root’;

Flush the privileges and exit

Flush privileges

Exit

Now kill the mysqld process

/sbin/services mysqld stop

Verify it stopped

ps aux | grep mysqld

Now start it back up again

/sbin/services mysqld start

Verify your new password

mysql –u root -p

If it is running kill it using the command below as it should provide a cleaner exit than killing the pid

/sbin/service mysqld stop

Now restart the mysqld process skipping over the grant tables

mysqld_safe –skip-grant-tables

Now verify mysqld started up again, if it didn’t  you have bigger problems

ps aux | grep mysqld

If it is running connect as root, no password needed since we skipped the grant tables

mysql –user=root mysql

Now update the root password

UPDATE user SET Password = PASSWORD(‘new-password’) WHERE User = ‘root’;

Flush the privileges and exit

Flush privileges

Exit

Now kill the mysqld process

/sbin/services mysqld stop

Verify it stopped

ps aux | grep mysqld

Now start it back up again

/sbin/services mysqld start

Verify your new password

mysql –u root -p

Now you should be in business!!

If it is running kill it using the command below as it should provide a cleaner exit than killing the pid

/sbin/service mysqld stop

Now restart the mysqld process skipping over the grant tables

mysqld_safe –skip-grant-tables

Now verify mysqld started up again, if it didn’t you have bigger problems

ps aux | grep mysqld

If it is running connect as root, no password needed since we skipped the grant tables

mysql –user=root mysql

Now update the root password

UPDATE user SET Password = PASSWORD(‘new-password’) WHERE User = ‘root’;

Flush the privileges and exit

Flush privileges

Exit

Now kill the mysqld process

/sbin/services mysqld stop

Verify it stopped

ps aux | grep mysqld

Now start it back up again

/sbin/services mysqld start

Verify your new password

mysql –u root -p

Linux WPA

August 16th, 2010

This was an issue I ran into recently, setting up wpa wireless on my slackware 13.1 box. Here’s how I got it going:

First off you’ll need your wireless network name (SSID), and the passphrase. To generate the psk, you’ll need to run:
wpa_passphrase YOURSSID passphrase

Now you should get a nice little box returned from the above command, copy that information.

Now edit your wpa_supplicant file, found in /etc/wpa_supplicant.conf

You’ll want to paste your info into the box, and overall your end result file should like something like:
ctrl_interface=/var/run/wpa_supplicant

network = {
ssid=”myNetwork”
scan_ssid=1
proto=WPA
key_mgmt=WPA-PSK
psk=SECRETKEY
}

Save that and run:
wpa_supplicant -B -Dwext -iwlan0 -c/etc/wpa_supplicant.conf
That should turn on your wireless card
Now run:
dhcpcd wlan0
this will give you an ipaddress
You should be good to go, try to ping google.com

As each network set up is different, some tips on attempting to debug:
In the wpa_supplicant command switch -B to -dd for verbose debugging.

LAMP Set Up

August 14th, 2010

A few quick notes on turning up a LAMP environment (Linux, Apache, MySQL, PHP).

First off for those looking for a development box, and don’t care much for installing, check out XAMPP. Its a quick and dirty way to get up and running fast, however, not overly recommended for a production environment.

For a bit more control, go hand by hand. This guide is for hand compiling, if your Linux flavor has a package manager such as YUM, that is not covered here.

I’m attempting to give enough information to provide a ballpark idea of what’s going on. The details are for googling, the best way to learn.

1. We’ll start with apache, this does need to be done before the PHP install. Go download HTTP server from apache. To go directly to your box, you can copy the download location and run a wget to grab the tarball.

2. Put your tarball someplace consistent, such as /usr/local/src. Unpack the tarball, tar -zxvf tarName

3. Now you’ll have to compile apache, first off run the configure, ./configure, be sure to use the –prefix to put apache some place better than /usr/local/src, such as /usr/local/apache2. Making sure to add in all your desired modules, now run make followed by make install.

4. If you run into an issue of a forgotten module,:

  1. go to the src directory
  2. /usr/local/apache2/bin/apxs -c mod_crap.c
  3. cp .libs/mod_crap.so /usr/local/apache2/modules

5. Not to get lazy, typing, but to do PHP and MySQL is pretty much the same steps.

6. Go download the source files, run ./configure, make, make install

7. Setup httpd.conf

    • LoadModule php5_module modules/libphp5.so
    • AddHandler php5-script php
    • NameVirtualHost *:80
    • Include /usr/local/apache2/conf.d/*.conf
    • DirectoryIndex index.php index.html index.html.var
    • make sure all path to log files point to /var/log/httpd
    • Set up logrotate: edit /etc/logrotate.conf : edit /etc/logrotate.d/httpd (sample files below)
  1. mkdir /usr/local/apache2/conf.d
    1. Add the conf.d files provided

Hope that gives everyone a little bit of a start.

PHP Command Line Process

August 10th, 2010

As a PHP developer, this question has come to me a few times, how do I go about creating a command in Linux that I can write in PHP? Very handy for PHP developers who don’t want to worry about shell scripting…..

1. Write your PHP script, remember it can be executed from any directory, so keep that in mind when writing.
2. Make the first line in your script #!/usr/local/bin/php -q That line tells the computer to use PHP in quiet mode to execute the script.
For example:
#!/usr/local/bin/php -q
<?php
//PHP code Here
?>
3. Now you have two options:
a. Go to /usr/local/bin and save your script there, with no .php, so if you have script.php make it script
b. Go to /usr/local/bin and create a sym link from that directory to your script, so ln -s /path/to/script.php script
4. Set your permissions on the script with the chmod command. Type man chmod if you are not familiar.