Tuesday, April 26, 2011

Mysql your way out of a wet paper bag.

For someone who doesn't have to deal with the subject in depth, I have found myself dealing with mysql very often.  Getting servers migrated and creating services for users requires me to have at least a very basic tool kit for dealing with mysql.

This could be a road map for starting administrators that haven't dealt with mysql.  I never have to deal with tables, but I do need to create databases, make users that have permissions on those databases (and not the whole sql server), back up databases, and restore databases; the following represents my expertise on this subject.

This example assumes a unix-based system with a current (5.X) mysql installation and system root access.  If you want to know how to actually set the server up send me a message; sooner or later I'll post something like that.

1. Creating databases.

Before you can create a database you have to log in to the database server as the root mysql user.  This is not the same as the root user for the host system.  If this is a fresh install, the root account for mysql has no password, and you can omit the '-p' switches.  Mysql has to be running before you can log in.  Each system has different scripts for starting the mysql service, but if you run a 'top' or 'ps aux| grep mysqld' you should be able to see if the 'mysqld' process is running.  To log in type:
mysql -u root -p
The system will then prompt you for the root password.  If you have never set a root password for mysql, leave out the -p.  You'll be greeted with the 'mysql>' prompt instead of your regular system prompt.  You can see what's there by typing the following:
mysql> show databases;
It will most likely show you a list like this:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| intranet           |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
You might not have one that says 'intranet' if you do, then we have something in common.  If you didn't and really wanted one, type:
mysql> create database intranet;
Notice on both commands there is a semi-colon ';' at the end of each command; those are important.

2. Creating users.

In order for the database to be of any use to anyone except the root mysql user, we need to add a mysql user with privileges to that database.  If you're bringing the database over from another machine (we'll get to the restore in a moment) you'll need to make sure that the mysql user name and passwords match exactly.  How we do this is with the following statement:
mysql> grant all privileges on databasename.* to 'mysqluser'@'localhost' identified by 'mysqlpassword';
For our specific example it would look like:
mysql> grant all privileges on intranet.* to 'intranetguy'@'localhost' identified by 'd0ntTe3ll';
That will give the user 'intranetguy' access to write to the database 'intranet' and all of its tables as long as the user logs in from the machine 'localhost' (which usually means it's a process running already on the local box, like a web-server environment would be) with a password of 'd0ntTe3ll'.

It is a good idea to flush the privileges after creating users or databases; then exit the mysql shell.
mysql > flush privileges;
mysql > exit

3. Backing up databases.

I'm a passionate guy, and that passion extends to backing up data.  Back up your databases.  I use 'mysqldump' to make an easy to move sql file that we can use as a backup, or a package to move the database to a new server.  The 'mysqldump' command is run on the host system shell, NOT the mysql> shell.  The statement would look something like this:
# mysqldump -hlocalhost -uintranetguy -pd0ntTe3ll intranet > /path/to/sqlbackupfolder/intranet.sql
Without spaces, the switches are '-h' host - almost always localhost -, '-u' the user you specified earlier, and '-p' the password.  I know what you're thinking: 'I have to type that all the time?' No, we're going to add it to the system cron.  As the system (not mysql) root user, type 'crontab -e' to edit the cron daemon's task list.  This will bring up the cron file in the editor 'vi'; if you don't know how to use 'vi' then you need to go learn.  We need to find the path of 'mysqldump' with the 'which' command, create the backup directory, then add the following line after you get into 'crontab -e':
# which mysqldump
/usr/local/bin/mysqldump

# mkdir /var/sqlbackup
# crontab -e
0 3 * * * /usr/local/bin/mysqldump -hlocalhost -uintranetguy -pd0ntTe3ll intranet > /var/sqlbackup/intranet.sql
And every morning at 3am, you'll be backing up the intranet database.

4. Restoring databases.

So now you have to restore the database.  Super easy. From the host system shell:
$ mysql -u intranetguy -p intranet < /var/sqlbackup/intranet.sql
The system will prompt you for the intranetguy's password and the intranet database will be overwritten with the contents of the backup sql file.

Monday, April 25, 2011

Change in IP, reboot? Not with FreeBSD.

There are times when I need to swap a server replacement in for a machine that's gone down.  After changing the IP address in /etc/rc.conf there is a simple set of commands that can be strung together that will keep you from having to wait for a whole system reboot.

Step 1: change the IP address in /etc/rc.conf
ifconfig_re0="inet 10.10.10.49  netmask 255.255.255.0"
becomes
ifconfig_re0="inet 10.10.10.50  netmask 255.255.255.0"
As long as we didn't have to adjust the default router (which is just one other line in the same file) we can save the /etc/rc.conf file and issue these commands:
/etc/rc.d/netif restart && /etc/rc.d/routing restart
Your network will reset and start up with the new settings in the /etc/rc.conf file.  This will terminate any sessions currently open, including your SSH session, if that's how you're connected.

Fun with 'find'.

Here are some basic examples of the UNIX 'find' command that I use frequently. 


If you wanted to search for, and erase every file on your system named 'bahdouche' execute:

find / -name bahdouche -type f -exec rm -f {} \;

If you wanted to do a 'test run' before you actually killed them all, use the 'echo' command nested in the command line:

find / -name bahdouche -type f -exec echo rm -f {} \;

To find every file owned by the user named 'bahdouche' execute:
find / -user bahdouche -print
The -print flag is optional on some systems.

Find every file that has no user associated:


find / -nouser
Let's say you run a mail server with pam accounts and maildirs and you wanted to have a cron job that deletes their Spam folders every 30 days; add this to crontab:

find /home/*/Maildir/.Spam -type f -mtime +30 -exec rm -f {} \;
This obviously would have to be modified if the Spam folders were named differently.

Migrating user accounts in FreeBSD.

I make a lot of servers.  When it comes time for backups, upgrades, updates and general data security, I find myself moving users from one box to another quite frequently.  Typing in all of those usernames and passwords (that you probably have no way to know) might present a problem.  Luckily FreeBSD makes this process quite simple.  The first step (in anything computer-related) is to back up what you're about to touch.  This is as simple as these 3 commands on the server that is to host the new accounts:
cp /etc/master.passwd /etc/master.passwd.orig
cp /etc/passwd /etc/passwd.orig
cp /etc/group /etc/group.orig

Then copy these same files from the 'old' machine, you can use 'scp' or whatever to copy them.  If you're unsure how to do this, send me a message; that would be a whole separate post.  Once you get the files to the 'new' machine, copy them to the /etc folder overwriting what is there.  Type the following command to import all of the users:

pwd_mkdb /etc/master.passwd
(NOTE: THIS WILL MAKE THE USER LIST THE SAME AS THE 'OLD' MACHINE; ANY ACCOUNT THAT IS NOT ON THE OLD MACHINE WILL NOT EXIST; ROOT PASSWD WILL CHANGE TO WHAT 'OLD' IS SET TO.)

That is it. All of the users that could log into the 'old' server can now log onto the 'new' server with their same passwords.

FreeBSD and the sound card.

If you want to get your sound set up in FreeBSD, the handbook is an excellent place to start.  I highly suggest anyone using FreeBSD to familiarize yourself the handbook.  I wont repeat the steps on getting the card to work; after you do, if you still are not getting sound out of your speakers you might want to try some different mixer settings.

For whatever reason, it seems that some of the switches compete with each other not allowing for the sound to actually work.  What I did was a trial and error approach and landed on a setting that works for my hardware configuration.  Basically, you have to mute/unmute things that might or might not conflict with the accessories (mic, speakers, etc) you have plugged in.

First, type 'mixer' without any switches and see where you're at.  You will get a listing similar to the following output:

Mixer vol      is currently set to  78:78
Mixer pcm      is currently set to  75:75
Mixer speaker  is currently set to  75:75
Mixer line     is currently set to  75:75
Mixer mic      is currently set to   0:0
Mixer mix      is currently set to   0:0
Mixer rec      is currently set to  75:75
Mixer igain    is currently set to   0:0
Recording source: mic

I was able to get sound out by typing
mixer line 0:0
 or
mixer line 75:75
I kept some music playing while I changed settings for 'line' 'mic' 'pcm' 'speaker' , etc. until I found one that worked.  I'm sorry if this isn't the scientific answer you may have been searching for, but this is what worked for me on a few different machines.  Depending on your setup, what you mute/unmute might vary from my example.  What I landed on was this:
Mixer vol is currently set to 77:77
Mixer pcm is currently set to 75:75
Mixer speaker is currently set to 0:0
Mixer line is currently set to 75:75
Mixer mic is currently set to 75:75
Mixer mix is currently set to 0:0
Mixer rec is currently set to 0:0
Mixer igain is currently set to 0:0
Recording source: mic
Hope that saves someone the headache I had.

Saturday, April 23, 2011

Hello world

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}