Postfix restart in Mac OS X 10.8 Mountain Lion

- by admin

Actually it is no so easy to restart Postfix in Mac OS X 10.8 Mountain Lion like in Linux :-)

However you can restart Postfix by plist unload / reload:
sudo launchctl unload /System/Library/LaunchDaemons/org.postfix.master.plist
sudo launchctl load System/Library/LaunchDaemons/org.postfix.master.plist

By the way, if you have a problem with Postfix, look at this article: Cannot send mail in Mac OS X 10.8 Mountain Lion

How to Flush DNS Cache in Mac OS X

- by admin

After upgrading to 10.8.2 I have got a problem with adding a new entry into hosts file: the update was recognised only after system restart. But a more easy solution was just to flash DNS cache! So,

... flushing your DNS cache in Mac OS X is actually really easy, and there are two different commands to use, one for Leopard and for Tiger. Depending on your version of OS X, open your Terminal and follow the appropriate directions below:

Flushing DNS Cache in OS X Lion (10.7) and OS X Mountain Lion (10.8)


Launch Terminal and enter the following command, you will need to enter an administrative password:
sudo killall -HUP mDNSResponder

Note the dscacheutil still exists in 10.7 and 10.8, but the official method to clear out DNS caches is through killing mDNSResponder. You can also find that process running in Activity Monitor.

Flush DNS Cache in Mac OS X 10.5, Mac OS X 10.6


Launch Terminal and issue the following command:
dscacheutil -flushcache

All done, your DNS has been flushed. On a side note, the dscacheutil is interesting in general and worth taking a look at, try the -statistics flag instead for some stats.

Flush your DNS Cache in Mac OS X 10.4 Tiger


Type the following command in the Terminal:
lookupd -flushcache

That’s it - now your DNS settings should be as you intended them to be :-)

Find duplicate records in MySQL

- by admin

So, the task is to get duplicate records from a MySQL database.

The easy way:
SELECT COUNT(*), column1, column2 FROM tablename
GROUP BY column1, column2
HAVING COUNT(*)>1;

More complex case: shows each duplicated row:

It can be done using subquery:
SELECT firstname, lastname, list.address FROM list
INNER JOIN (SELECT address FROM list
GROUP BY address HAVING count(id) > 1) dup ON list.address = dup.address

or with INNER JOIN:
SELECT a.firstname, a.lastname, a.address
FROM list a
INNER JOIN list b ON a.address = b.address
WHERE a.id <> b.id

If the same 'address' exist more than two times, then DISTINCT is needed.

Cannot send mail in Mac OS X 10.8 Mountain Lion

- by admin

There is an error while sending mail in Mac OS X Mountain Lion 10.8:

send-mail: fatal: chdir /Library/Server/Mail/Data/spool: No such file or directory

To fix just run in terminal:
sudo mkdir -p /Library/Server/Mail/Data/spool
sudo /usr/sbin/postfix set-permissions
sudo /usr/sbin/postfix start

The solution also fixes an error in 10.8 where the fax service has stopped sending emails after the 10.8 upgrade.

Another Postfix Mac OS X issue solution can be found here: Mac OS, MySQL: No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)

Find (search) and replace text from command line in multiple files (Linu

- by admin

Another (and more easy) way to change text in multiple files is to use grep:
grep -lr -e 'oldtext' * | xargs sed -i 's/oldtext/newtext/g'

or to use PERL:
perl -p -i -e ’s/oldtext/newtext/g’ *

 

Find (search) and replace text from command line in multiple files (Linu

- by admin

Just after I posted this article the second more easy solution has been found. Here it is:

Find (search) and replace text from command line in multiple files (Linux) #2

When you are working on the Linux command line and you come across a large file or a large number of files in which you need to replace a certain text with another, finding and pasting over each instance of the text can be a bit time consuming. Well, worry no more. Linux has just the solution for you. Here’s a way to find and replace a string of text in one or more files automatically.

For the purpose of this exercise we will use a Linux command line tool called “sed”.  ”sed” is a very powerful and versatile tool, and a lot can be written about its capabilities. We are using a very limited aspect of “sed” here. I would definitely recommend that you read up a little more on “sed” if you find this aspect of it interesting.

We are going to use the following syntax to find and replace a string of text in a file:
# sed -i 's/[orginal_text]/[new_text]/' filename.txt

Say you have a file called “database.txt” with numerous instances of the IP address of your database server in it. You have just switched to a new database server and need to update it with the new server’s IP address. The old IP address is 192.168.1.16 and the new one is 192.168.1.22. Here’s how you go about it:
# cat database.txt
LOCAL_DATABASE = 192.168.1.16
LOCAL_DIR = /home/calvin/
PROD_DB = 192.168.1.16

# sed -i 's/192.168.1.16/192.168.1.22/g' database.txt
# cat database.txt
LOCAL_DATABASE = 192.168.1.22
LOCAL_DIR = /home/calvin/
PROD_DB = 192.168.1.22

Now open the file “database.inc” and check to see if the new IP address has taken place of your old one. Here’s the breakup of the above command. First you call the “sed” command. Then you pass it the parameter “-s” which stands for “in place of”. Now we use a little bit of regular expressions, commonly known as “regex”  for the next bit. The “s” in the quoted string stands for “substitute”, and the “g” at the end stands for “global”. Between them they result in a “global substitution of the the string of text you place in between them.

You can optionally skip the “g” at the end. This means that the substitution will not be global, which practically translates to the substitution of only the first instance of the string in a line. So if you had a line with multiple instances of the text you are trying to replace, here’s what will happen
# cat database.txt
LOCAL_DATABASE = 192.168.1.16
LOCAL_DIR = /home/calvin/
PROD_DB = 192.168.1.16, 192.168.1.16

# sed -i 's/192.168.1.16/192.168.1.22/' database.txt
# cat database.txt
LOCAL_DATABASE = 192.168.1.22
LOCAL_DIR = /home/calvin/
PROD_DB = 192.168.1.22, 192.168.1.16

Here comes the real magic. Now, say you want to change a string of text not just in a single file, but in the entire directory you are in. There are a number of text files in which you need to find and replace the “wine” with “champagne”.
# find . -maxdepth 1 -name "*.txt" -type f -exec sed -i 's/wine/champagne/' {} \

We use the find command to get a list of all the text files in the current directory. That’s the “find . -maxdepth 1 -name “*.txt” -type f” part. “find . maxdepth 1″ tell the computer to look in the current directory and go no deeper than the current directory. The ‘-name  ”*.txt”‘ part tells find to only list files with the extension of “.txt”. Then the “-type f” section specifies that “find” should only pick exactly matching files. Finally the “-exec” part tells “find” to execute the command that follows, which, in this case, is the “sed” command to replace the text – “sed -i ‘s/wine/champagne/’ {} \”.

I realize that the above command seems complicated. However, once you use it a little bit you will realize that it is probably worth noting it down and using it. Now try changing a string of text in multiple levels of directories.

Disable automatic unzipping on file download in Safari (Mac OS X)

- by admin

After file downloading a ZIP file with Safari this file is unzipped automatically. The original ZIP file appears to be deleted. Not so good!

To fix this behaviour just go to
Safari->Preferences->General

and uncheck "Open Safe Files After Downloading".

Date format validation in PHP

- by admin

In case of date format checking in PHP just use this function:
function checkDateFormat($date)
{
  //match the format of the date
  if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts))
  {
    //check weather the date is valid of not
        if(checkdate($parts[2],$parts[3],$parts[1]))
          return true;
        else
         return false;
  }
  else
    return false;
}

Example:
echo checkDateFormat("2008-02-29"); //return true
echo checkDateFormat("2007-02-29"); //return false

Mac OS, MySQL: No such file or directory (trying to connect via unix:///

- by admin

Error on attempt to connect locally to MySQL server DB with PHP on MacOS X Lion 10.7:

No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)

Solution 1:

instead of localhost use 127.0.0.1:
mysql_connect ('127.0.0.1', $user, $password);

Solution 2:

In /etc/php.ini change
pdo_mysql.default_socket = /var/mysql/mysql.sock
mysql.default_socket = /var/mysql/mysql.sock
mysqli.default_socket = /var/mysql/mysql.sock

to
pdo_mysql.default_socket = /tmp/mysql.sock
mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock

Do not forget to restart Apache after update :-)

FYI: If no /etc/php.ini found just copy /etc/php.ini.default to /etc/php.ini

MySQL: ERROR 2006 (HY000) at line: ### MySQL server has gone away

- by admin

In case of MySQL error 2006 (HY000) at line: ### MySQL server has gone away which may occur while restoring a big DB dump just add (or increase, if exists) max_allowed_packet parameter of MySQL config (usually in /etc/my.cnf).

For example:
[mysqld]
max_allowed_packet=128M

And do not forget to restart MySQL server :-)

« Newer posts

Older posts »