Ramblings from MostlyChris

Tech stuff and a bit more

I ran into an issue in which I needed to test whether sqlite and the pdo plugin had support for UTF-8. From the documentation, sqlite3 should be able to handle this and other charsets. Further, the pdo should be charset agnostic. The following script should return UTF-8 because of the 'pound' symbol if indeed both items above are true.

<?php
try {
       $dbh = new PDO("sqlite::memory:", "charset=UTF-8");
} catch(PDOException $e) {
    echo $e->getMessage();
}

$sql = "create table t1 (id int, field1 varchar(255))";

try {
    $dbh->exec($sql);
} catch(PDOException $e) {
    echo $dbh->getMessage();
}

$sql = 'insert into t1 (id, field1) values (1, "A bit of text £")';
try {
    $dbh->exec($sql);
} catch(PDOException $e) {
    echo $dbh->getMessage();
}

$sql = 'select field1 from t1 where id=1';
try {
    foreach ($dbh->query($sql) as $row) {
        print "field1: " . $row['field1'] . "\n";
        print "Encoding: " . mb_detect_encoding($row['field1']) . "\n";
    }
} catch(PDOException $e) {
    echo $dbh->getMessage();
}

?>

Output of the above script should look similar to the following:

[rack@186451-db1 ~]$ php test.php
field1: A bit of text £
Encoding: UTF-8

Notice the UTF-8 output due to the pound sign being used in the text string.

I recently had to create a number of home directories from the user accounts in the /etc/passwd file because Webmin didn't create them when the user was created. Don't ask me why. Webmin is a mystery and only does things it feels like doing at the time it does them. But I digress..

The first thing I did was to create file with all users that have directories.

cat /etc/passwd | sed 's/:/ /g' | awk '($5 ~ /\//) { print $1 " " $5}' > file.txt

Edit the file you created and remove things like the postfix directory and other non-user account directories.

Next, run the command to create all of the home directories and then change their ownership to the user.

while read line; do mydir=`echo $line |awk '{print $2}'`; myuid=`echo $line| awk '{print $1}'`;  mkdir $mydir 2>/dev/null; chown $myuid:$myuid $mydir; done < file.txt

Follow these simple steps to setup an NFS client/server.

First, some housekeeping for this example.
Server machine (server) = 192.168.1.1
Client machine (client) = 192.168.1.2
Exported directory /nfs/export

### The server steps ###
1. Create an entry in /etc/exports with the exported directory and client IP.

/nfs/export 192.168.1.2(rw)

The (rw) sets the directory to be read and writeable. There are other options that can be specified here. Check out the NFS docs for more.

2. Edit /etc/hosts.deny and /etc/host.allow
In /etc/hosts.deny

portmap: ALL

In /etc/hosts.allow

portmap: 192.168.1.2

3. Start up portmap, nfs, nfslock, netfs on the server. These can most likely be found in /etc/init.d in the form of init scripts. Make sure they are set to start at boot with chkconfig (Redhat servers).

4. Run rpcinfo and make sure at least nfs, portmap and mountd are listed.

rpcinfo -p localhost



### The client steps ###

5. Make a directory that you you are going to use to mount the NFS share. Call it whatever you want.

mkdir /mount/nfsdir

6. Start portmap, nfs, nfslock, netfs on the client as you did on the server.

7. Mount the share.

mount 192.168.1.1:/nfs/export /mnt/nfsdir

8. Add an entry to /etc/fstab in order to have the share mount after a reboot.

192.168.1.1:/nfs/export  /mnt/nfsdir   nfs  rw  0 0

9. Test that you can create/delete/edit files and needed by your application.

Optimize Mysql

No comments

The following command will optimize all databases in mysql (excpect those that don't support it due to their storage engine).

mysqlcheck -Aao

You can throw the

--auto-repair

flag in if you are brave.

If you need to change the TTLs on all domains in Plesk at once, you can massage the database with the following command:

mysql> UPDATE `dns_zone` SET `ttl` = '300', `ttl_unit` = '60' WHERE `id` >1;

Substitute the TTL values for what you need. Since the flat files are still used by named to provide DNS resolution, those will need to be updated as well. This command will do the trick:

mysql -Ns -uadmin -p`cat /etc/psa/.psa.shadow` -D psa -e 'select name from domains' | awk '{print "/usr/local/psa/admin/sbin/dnsmng update " $1 }' | sh