Friday, November 8, 2013

Install MySQL 5.6.14 on Ubuntu 12.04 from Source Code

Install MySQL 5.6.14 on Ubuntu 12.04 from Source Code

by juniway

Operating System:Ubuntu 12.04 LTS 64 bits
MySQL Version:5.6.14
Installation File typeSource Code, compressed in mysql-5.6.14.tar.gz
Downloadhttp://dev.mysql.com/downloads/mysql/#downloads

Installing MySQL in other versions of ubuntu should be similar or exactly same, like ubuntu 13.04, etc.

In most of occasions, build a tool/software from the source code enables us to access to the latest version and the latest features, and also it enables us to customize build parameters, compiler optimizations, and installation location. 

Before you build MySQL from source code, you may be better to check if there is a pre-complied binary package distribution available already because a binary package is usually built with the best possible options for optimal performance, if a binary package distribution is available, you may not need to build MySQL from source code because it can save you a lot of pain.

Here we go, let's start to install MySQL from source code, I break down the procedure to a lot of easy understand steps as following:

Step 1. Get the source code package from the official site, 
            for ubuntu, the file you get will shoud be mysql-5.6.14.tar.gz

Step 2. You may put it in /tmp or /home/username/Download, but it doesn't matter too much whether you put it here or there, because eventually you need to compile it to binary so that you can install it in a location that you specified.

Step 3. Prerequisite tools to be installed before you are able to compile the MySQL source code files

According to the official documents, to install MySQL from source, your system must have the following tools, regardless of installation method:
  • CMake, which is used as the build framework on all platforms. CMake can be downloaded fromhttp://www.cmake.org.
  • A good make program. Although some platforms come with their own make implementations, it is highly recommended that you use GNU make 3.75 or newer. It may already be available on your system as gmake. GNUmake is available from http://www.gnu.org/software/make/.
  • A working ANSI C++ compiler. GCC 4.2.1 or later, Sun Studio 10 or later, Visual Studio 2008 or later
  • Perl is needed if you intend to run test scripts. Most Unix-like systems include Perl. On Windows, you can use a version such as ActiveState Perl.

Step 4.  Create user and group.
You need to create a group and a user for MySQL(for ownership only, not for login permission), let's just call the group name and the user name both mysql, you can also choose other names if you like.

sudo groupadd mysql
sudo useradd -r -g mysql mysql

Step 5. Compile the source files and Install

cd /tmp/mysql-5.6.14
cmake .
make
make install

Note: Usually when we install something from source code, we run ./configure first and then make, make install, but as directed by official document, here we run cmake . to do the pre-configuration job.
To prevent old object files or configuration information from being used, run these commands on Unix before re-running CMake:
make clean
rm CMakeCache.txt

To install in a specific directory, add a DESTDIR parameter to the command line:
make install DESTDIR="/opt/mysql"

For more information about CMake, check http://dev.mysql.com/doc/refman/5.6/en/source-configuration-options.html

Step 6. Installation complete
By default, MySQL is installed in /usr/local/mysql directory, if everything goes fine there should be no error messages, make sure you have already installed the dependency library libiao1.

Now we need to configure the MySQL settings after the above installation, we call this step as post installation

Step 1.
cd /usr/local/mysql
chown -R mysql .
shgrp -R mysql .

Step 2.
run the mysql_install_db program to set up the initial MySQL grant tables containing the privileges that determine how users are permitted to connect to the server. You will need to do this if you used a distribution type for which the installation procedure does not run the program for you.
scripts/mysql_install_db --user=mysql
Typically, mysql_install_db needs to be run only the first time you install MySQL, so you can skip this step if you are upgrading an existing installation, However, mysql_install_db does not overwrite any existing privilege tables, so it should be safe to run in any circumstances.
It might be necessary to specify other options such as --basedir or --datadir if mysql_install_db does not identify the correct locations for the installation directory or data directory. For example:
scripts/mysql_install_db --user=mysql \
         --basedir=/opt/mysql/mysql \
         --datadir=/opt/mysql/mysql/data
The mysql_install_db script creates the server's data directory with mysql as the owner.
 Under the data directory, it creates directories for the mysql database that holds the grant tables and the test database that you can use to test MySQL. The script also creates privilege table entries for root and anonymous-user accounts. The accounts have no passwords initially.

Step 3.
Most of the MySQL installation can be owned by root if you like. The exception is that the data directory must be owned by mysql. To accomplish this, run the following commands as root in the installation directory:
chown -R root .
chown -R mysql data

If you want MySQL to start automatically when you boot your machine, you can copy support-files/mysql.server to the location where your system has its startup files. in ubuntu, it is /etc/init.d
or you can create a symbolic link point to support-files/mysql.server in /etc/init.d

Step 4.
It is important that the MySQL server be run using an unprivileged (non-root) login account. To ensure this if you run mysqld_safe as root, include the --user option as shown. Otherwise, you should execute the script while logged in as mysql, in which case you can omit the --user option from the command.

bin/mysqld_safe --user=mysql &


Step 5.
Use mysqladmin to verify that the server is running. The following commands provide simple tests to check whether the server is up and responding to connections:
bin/mysqladmin version
bin/mysqladmin variables

Step 6.
shut down the server and start the server again
bin/mysqladmin -u root shutdown
bin/mysqld_safe --user=mysql &


Step 7.

Run some simple tests to verify that you can retrieve information from the server. The output should be similar to what is shown here:
bin/mysqlshow
+--------------------+
|     Databases      |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
bin/mysqlshow mysql
Database: mysql
+---------------------------+
|          Tables           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| plugin                    |
| proc                      |
| procs_priv                |
| servers                   |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
bin/mysql -e "SELECT Host, Db, User From db" mysql
+------+--------+------+ | host | db | user | +------+--------+------+ | % | test | | | % | test_% | | +------+--------+------+

You should be able to see use MySQL now.



Reference: http://dev.mysql.com/doc/refman/5.6/en/index.html



















Wednesday, November 6, 2013

Linux: Uninstall software installed from a source tar-ball

Installing software from a source code is common practice in UNIX and Linux world. Some time this is preferred method because it gives all power and flexibility you need to optimize your software such as MySQL, PHP, and Apache etc. However, uninstalling files installed from a source code tar ball is a big headache.
Two methods can be used to uninstall files:

Method # 1: make command

Use command make uninstall or equivalent supported command, Read INSTALL or README file in source code file to find out more about this method.
# make uninstall
Sure, this method sounds very easy but not supported by all tar balls.

Method # 2: find command

(a) Make a list of all files on the system before installing software i.e. a pre-installation list of all files on your system.
find /* > packgetlist.b4
(b) Now install the software (use configure & make to compile it)
make
make install
(c) Now make a list of all files on the system after installing software i.e. postinstall list
find /* > packagelist.after
(d) Next, compare both lists using the diff utility to find out what files are placing where. This list can be use to uninstall all files installed using source tar ball.
diff packagelist.b4 packagelist.after > package.uninstall.list
(e) After some time if you wish to uninstall files then you need to get list of files from package.uninstall.list file. Use following small for loop at shell prompt to remove all files:
for i in $(grep ">" package.uninstall.list | awk '{ print $2 }')
do
/bin/rm -fi $i
done

A note about software installed by using apt-get

If you are using Debian / Ubuntu Linux, use following command to uninstall binary packages:
sudo apt-get remove {package-name}

An example of removing MySQL from ubuntu:


completely remove and clean MySQL installation ubuntu

sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo apt-get autoremove
sudo apt-get autoclean

see if anything depends on the installed packages
apt-cache rdepends mysql-server
apt-cache rdepends mysql-client


delete preferences(the next find command will delete everything):
rm -rf /etc/mysql

check to see if mysql is running:
service mysql status

Want to know service status;
service packagename status 

-----------------installation of mysql--------------

First, install the MySQL server and client packages:
sudo apt-get install mysql-server mysql-client
 
When done, you have a MySQL database read to rock 'n roll. However, there's more to do.
You need to set a root password, for starters. MySQL has it's own user accounts, which are not related to the user accounts on your Linux machine. By default, the root account of the MySQL Server is empty. You need to set it. Please replace 'mypassword' with your actual password and myhostname with your actual hostname.

sudo mysqladmin -u root -h localhost password 'mypassword'
sudo mysqladmin -u root -h myhostname password 'mypassword'
 
Now, you probably don't want just the MySQL Server. Most likely you have Apache+PHP already installed, and want MySQL to go with that. Here are some libraries you need to install to make MySQL available to PHP:

sudo apt-get install php5-mysql

You can now access your MySQL server like this:

mysql -u root -p


Happy coding...;-)

Thursday, October 31, 2013

Use Priority Queue to find k largest/smallest elements from a sequence

/*
 * Internally Priority queue is implemented as a min_heap
 * so we can utilize the min_heap's properties to implement our code
*/

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

void k_largest(vector<int> a, int n, int k){  
    int i;
    vector<int> ans;
    priority_queue<int, vector<int>, greater<int>> p;     // this is a min_heap
 
 
    for(i=0;i<k;i++)
        p.push(a[i]);
    //cout<<p.top()<<endl;
    for(i=k; i<n; i++)
        if(a[i]>(p.top())){
            p.pop();
            p.push(a[i]);
       }
 
    for(; !p.empty(); p.pop())
        ans.push_back(p.top());
     
 
    for(i=0;i<(int)ans.size();i++)
        printf("%d ",ans[i]);
    puts("");
}

int main(){
    vector<int> a{6, 7, 2, 3, 5, 1, 8, 4, 10};
    int len = a.size();
    int k = 3;
    k_largest(a, len, k);
    return 0;
}

Wednesday, October 16, 2013

Ubuntu 中文输入法安装 ibus

Ubtuntu 12.04 中自带了中文输入法,在英文系统中同样已经预装了ibus,只需要下载一下简体中文语言包,即可通过Ctrl+Space进行输入法到切换。
如何安装简体中文语言包?
依次选择 Sytem Setting --> Language Support --> Install/Remove Languages后,将出现如下图所示窗口:
将右侧 Installed 栏的选择框勾选后,单击按钮 Apply Changes,之后会要求你输入密码,输入完成之后,系统将会自动下载并安装简体中文语言包。
一般情况下Ubuntu12.04中已经预装了ibus,如果你在进行下一步的时候提示了错误,在终端输入以下指令安装一些必须的库文件:
1sudo apt-get install ibus ibus-clutter ibus-gtk ibus-gtk3 ibus-qt4
如何设置ibus框架?
设置ibus框架请在终端输入以下指令:
1im-switch -s ibus
完成以上步骤后建议注销系统(亲测12.04 时未注销也可正常执行后面步骤,如果后面步骤无效,请尝试注销)
如何安装拼音引擎?
在Ubuntu12.04中已默认安装拼音引擎,如果你在进行下一步的时候未发现拼音(或其它)选项,请在终端输入以下相关指令:
1ibus拼音:      sudo apt-get install ibus-pinyin
2ibug五笔:      sudo apt-get install ibus-table-wubi
3谷歌拼音输入法: sudo apt-get install ibus-googlepinyin
4Sun拼音输入法:  sudo apt-get install ibus-sunpinyin
如何启动ibus框架?
启动ibus框架请在终端输入以下指令:
1ibus-setup
此时,IBus Preferences 设置会打开,可能会提示监控程序未启动,单击 Yes 即可,切换至 Input Method 选项卡中,选择自己喜欢的输入方式,并可自定义切换快捷键,界面应当大致如下图:
通常情况下,ibus图标(一个小键盘)会出现在右上角的任务栏中,大致如下图所示:

有时候它会开个小差跑出去玩,可以通过在终端输入以下指令叫它回家吃饭:
1ibus-daemon -drx
更多细节设置可自行研究,全都在ibus的设置面板里。

如果想安装最新的gcin中文输入法,请参考: