Wednesday, October 22, 2014

How to auto-load MySQL on startup on OSX 10.10 (Yosemite)

Problem
After upgrading to OSX Yosemite(10.10), MySQL stopped loading on startup.
The MySQL documentation says:
The Startup Item installation adds a variable MYSQLCOM=-YES- to the system configuration file /etc/hostconfig. If you want to disable the automatic startup of MySQL, change this variable to MYSQLCOM=-NO-.

So, I opened that file and it says:
# This file is going away

AFPSERVER=-NO-
AUTHSERVER=-NO-
TIMESYNC=-NO-
QTSSERVER=-NO-
MYSQLCOM=-YES-
As OSX mentioned this file is going away.

Solution
You need to create a Property List file (.plist) to start MySQL automatically on system startup.
First, create a new property list file:
sudo vim /Library/LaunchDaemons/com.mysql.mysql.plist
Add the following lines to it:
<!--?xml version="1.0" encoding="UTF-8"?-->
<plist version="1.0">
  <dict>
    <key>KeepAlive</key>
    <true />
    <key>Label</key>
    <string>com.mysql.mysqld</string>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/local/mysql/bin/mysqld_safe</string>
      <string>--user=mysql</string>
    </array>        
  </dict>
</plist>
Then update permissions and add it to launchctl:
sudo chown root:wheel /Library/LaunchDaemons/com.mysql.mysql.plist
sudo chmod 644 /Library/LaunchDaemons/com.mysql.mysql.plist
sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysql.plist
That's it. Now the MySQL service will be started on OS X startup.

Monday, May 26, 2014

Ubuntu 14.04 release party in Tehran, Iran

Ubuntu 14.04 Release Party
There was a release party of the Ubuntu 14.04 in Tehran. It was a great opportunity to see those young geeks there all together. Workshops, presentations and open ideas were the three sections of the festival.
The workshops were about introducing Ubuntu 14.04 features, FOSS business models, programming (Python, Ruby) and embedded Linux. Unlike the previous festivals, many people take part in workshops. The FOSS business models workshop was the most interesting one. Seems people are very curious to learn how to make money in the FOSS territory.
Presenting the new features in Ubuntu 14.04 was the next part of the festival. Danial Behzadi made a perfect presentation. The other presentations were about GNU/Linux news, OpenStack, Using Linux Everywhere and Salam-Donya magazine.
The open idea was the last part of the festival. Five ideas were presented and the participants voted them. The winner got 5,000,000 rial. It was about a network grid computing solution.
Photos by Negar Najimi:

Monday, October 24, 2011

Free Software / Open Source Business Models

The 2nd Technical Conference of The Free Software / Open Source held on Sep 8-9, 2011 in Zanjan, Iran. I had a conference about Free Software and Open Source Business Models. I've uploaded my presentation on slideshare.net. I hope this help newbies to understand Open Source business models.




Download Free Software / Open Source Business Models (PDF, 2.3 MB)

Monday, March 14, 2011

How to find IP locations using MySQL

Introduction
Here we learn how to use MySQL for locating an IP address. At first we create a table that contains the name of the country and its IP address range. Then define a function to find country name of IP Address.

What's Ip Address?

Quoting from the "IP address" page in Wikipedia:

An Internet Protocol address (IP address) is a usually numerical label assigned to each device (e.g., computer, printer) participating in a computer network that uses the Internet Protocol for communication. An IP address serves two principal functions: host or network interface identification and location addressing. Its role has been characterized as follows: "A name indicates what we seek. An address indicates where it is. A route indicates how to get there."


Table (database)
I created the ip_location table to save countries and IP Address ranges. Enter the following command:
CREATE TABLE `ip_location` (
`from_ip` int(15) DEFAULT NULL,
`to_ip` int(15) DEFAULT NULL,
`country` varchar(32) DEFAULT NULL,
KEY `from_ip` (`from_ip`,`country`),
KEY `to_ip` (`to_ip`,`country`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

Then you need to import ip_location data. I exported my table with data via mysqldump. You should download it and use mysql command to restore it.
Download ip_location.sql.zip (165KB)

getIpCountry() function
Now you need to create the function to extract country name:
DELIMITER $$
CREATE FUNCTION getIpCountry(ip varchar(15)) RETURNS varchar(64)
BEGIN
declare a tinyint unsigned;
declare b tinyint unsigned;
declare c tinyint unsigned;
declare d tinyint unsigned;
declare total bigint;
declare result varchar(64);
select substring_index(ip, '.', 1 ) into a;
select substring_index(substring_index(ip , '.', 2 ),'.',-1) into b;
select substring_index(substring_index(ip , '.', -2 ),'.',1) into c;
select substring_index(ip, '.', -1 ) into d;
set total := (a*256*256*256) + (b*256*256) + (c*256) + d;
select SQL_CACHE country into result from ip_location where total between from_ip and to_ip limit 1;
if (result is null) or (result = '') then
set result := 'unknown';
end if; 
return result;
END$$
DELIMITER ;
And done! You just need to use select command in MySQL cli:
mysql> SELECT getIpCountry('79.175.165.171');
+--------------------------------+
| getIpCountry('79.175.165.171') |
+--------------------------------+
| IRAN (ISLAMIC REPUBLIC OF)     |
+--------------------------------+
1 row in set (0.03 sec)

mysql> SELECT getIpCountry('4.2.2.4');
+-------------------------+
| getIpCountry('4.2.2.4') |
+-------------------------+
| UNITED STATES           |
+-------------------------+
1 row in set (0.00 sec)
Let me know if you have other way :)