Saturday, September 28, 2013

Set Up PHP 5.5 on Ubuntu 12.04


1. First of all, we need install php dependency library, they are:
(1) jpeg library: jpegsrc.v9.tar.gz
(2) png library: libpng-1.6.6.tar.gz
(3) freetype: freetype-2.5.0.1.tar.gz
(4) mcrypt: libmcrypt-2.5.8.tar.gz
mcrypt-2.6.8.tar.gz
(5) mhash-0.9.9.9.tar.gz


1. libxml2-(version).tar.gz ---> http://ftp.gnome.org/pub/GNOME/sources/libxm2
2. curl-(version).tar.gz ---> http://curl.haxx.se/download
3. gd-(version).tar.gz ---> http://libgd.bitbucket.org/
4. freetype-(version).tar.gz ---> http://sourceforge.net/projects/freetype
5. jpegsrc.v6b.tar.gz ---> http://ijg.org/files/jpegsrc.v6b.tar.gz
6. libpng-(version).tar.gz ---> http://www.libpng.org/pub/png/libpng.html
7. zlib-1.2.3.tar.gz ---> http://www.zlib.net

Now i have both libmcrypt and mcrypt compiled from source.I know it can be also installed over yum, but that is not always reliable and i more prefer to have native installs.Anyway solution to this problem is to simply add location of libmcrypt.so.4 file.My libmcrypt.so.4 was located on /usr/local/lib/ ,which i found it by command locate libmcrypt.so.4.(run command updatedb before for every case)Then go to directory /etc/ld.so.conf.d/ ,and there you will see conf files.You can either create new conf file called mcrypt.conf or simply add /usr/local/lib/ (or whatever is file libmcrypt.so.4 located)to new line,save file and 
then run ldconfig.And it will work then.

注:安装libxml2的时候有碰到一个编译无法通过的错误,
{
usr/include/bits/fcntl2.h:51: 错误: 调用‘__open_missing_mode’,声明有错误属性:open with O_CREAT in second argument needs 3 arguments
此处是因为gcc-4.3对语法检查严格的原因,我们找到出错的nanohttp.c文件,将出错的1588行的
int fd = open(fn.c_str(), O_WRONLY | O_CREAT | O_TRUNC) 改为
int fd = open(fn.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0777)即可
}


2. get php source: php-5.5.4.tar.bz2

3. unzip all the lib files to /usr/local/lib, and then configure and install them.


(1) libipeg
wget "http://www.ijg.org/files/jpegsrc.v9.tar.gz"
tar zxvf /tmp/jpegsrc.v9.tar.gz -C /usr/local/lib
cd /usr/local/lib/jpeg-9/
./configure --prefix=/usr/local/lib --enable-shared --enable-static --prefix=/usr/local/lib
make && make install

(2) libpng
wget http://prdownloads.sourceforge.net/libpng/libpng-1.6.6.tar.gz
tar zxvf /tmp/libpng-1.6.6.tar.gz -C /usr/local/lib
cd /usr/local/lib/libpng-1.6.6/
./configure --prefix=/usr/local/lib
make && make install

Note: if you get an error message saying: "configure: error: zlib not installed", that indicates that you may need to install zlib1g-dev, use:
apt-get install zlib1g-dev


(3) freetype
wget http://download.savannah.gnu.org/releases/freetype/freetype-2.5.0.1.tar.gz
tar zxvf /tmp/freetype-2.5.0.1.tar.gz -C /usr/local/lib
cd /usr/local/lib/freetype-2.5.0.1/make
./configure --prefix=/usr/local/lib
make && make install


Friday, September 27, 2013

Set up Apache2.4.6 from source



Apache 2.4.x is probably even faster than those web servers that are well-known by their speed. for example: Nginx。
The difference between apache-2.4 and apache-2.2 is that: apache-2.4 does not include apr along with the installation package, therefore, before we install apache-2.4之, we need to download and install apr first 

Source packages we need:

/usr/local/src/Apache-2.4.6/apr-1.4.8.tar.gz
/usr/local/src/Apache-2.4.6/apr-util-1.5.2.tar.gz
/usr/local/src/Apache-2.4.6/httpd-2.4.6.tar.gz

According to the apache requirement, we MUST put the source files of apr,apr-util in the directory apache2.4.6/srclib, and we need to rename the source diretories' names to apr and apr-util.
cp -rf apr-1.4.3 ./srclib/apr
cp -rf apr-util-1.4.1 ./srclib/apr-util

Install Apache dependency library
(1) Install apr
tar -xzvf /usr/src/apr-1.4.8.tar.gz -C /usr/src/httpd-2.4.6/srclib/apr
cd /usr/src/httpd-2.4.6/srclib/apr/apr-1.4.8
./configure --prefix=/usr/local/apr
make && make install

(2) Install apr-util
tar -xzvf /usr/src/apr-1.5.2.tar.gz -C /usr/src/httpd-2.4.6/srclib/apr-util
cd /usr/src/httpd-2.4.6/srclib/apr-util/apr-util-1.5.2
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config
make && make install

(2) Install pcre
tar -xzvf pcre-8.33.tar.gz -C /usr/src
cd /usr/src/pcre-8.33
./configure --prefix=/usr/local/pcre --with-apr=/usr/local/apr/bin/apr-1-config
make && make install


Install Apache2.4.6
Switch to the apache source directory
cd /usr/src/httpd-2.4.6

Configure:
./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre --enable-so --enable-deflate=shared --enable-expires=shared --enable-ssl=shared --enable-headers=shared --enable-rewrite=shared --enable-static-support --with-mpm=prefork

Compile:
make && make install 

compile arguments description:
--prefix=/usr/local/apache :specify the installation directory
--with-apr=/usr/local/apr : apr library
--with-apr-util=/usr/local/apr-util :apr-util library
--with-pcre=/usr/local/pcre : pcre library
--enable-so : allow load DSO while running (Notice: so needs to be statically compiled)
--enable-deflate=shared :compile Module deflate to DSO
--enable-expires=shared : compile Module expires to DSO
--enable-ssl=shared : compile Module ssl toDSO
--enable-headers=shared : compile Module headers to DSO
--enable-rewrite=shared : compile Module rewrite to DSO
--enable-static-support : use static link(dynamic by default) to compile all the binary supporting routines
--with-mpm=prefork : use mpm in the form of prefork

cp ./build/rpm/httpd.init /etc/init.d/httpd #使用init脚本管理httpd
chmod 755 /etc/init.d/httpd #change operation rights
update-rc.d httpd defaults
ln -s /usr/local/apache /etc/httpd  #create a link

the configuration file is:  /etc/httpd/conf/httpd.conf, the real path is /usr/local/apache/conf/httpd.conf

rm -rf /var/log/httpd/
ln -s /usr/local/apache/logs /var/log/httpd

groupadd apache #add apache group and user
useradd -g apache -s /usr/sbin/nologin apache
chown -R apache:apache /usr/local/apache

#configure firewall, open port 80
vim /etc/sysconfig/iptables 

# add a rule to port 22
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

#restart firewall
service iptables restart # 或 /etc/init.d/iptables restart

#start Apache
service httpd start # 或 /etc/init.d/httpd start





//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Solutions to some errors:


(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80


do the following :

vi /etc/httpd/conf/httpd.conf

and replace the port 80 and port 443 with 
port 80--->88
port 443 --> 448 for the timebeing.

to do quick find vi the file and than presh escap key and than put the ? question mark and than type 80 or 443 . to fine other 88 entry press n on the same time. anyhow let me kick ur ass now bcoz not suppose to teach all these things now..

well when u replaced them than u simply run the command

service htttpd restart
again to confirm service httpd restart

than see if it has started 

service httpd status...
than u will see that ur webserver is started on port 88 and 448 for ssl. hummmmm

now try to browse http://ipaddressof ur linuxbox:88
http://ipadr:443

u will see that ur page is cameup...hummmmmmm but problem is not solved yet. 
this teaches us that the prblem is with the port conflict..how can u track it..

put the command

lsif :80

u will see the several process are already running on port 80 so apachea can't start on the same port. so u have to kill all these apachea process using kill -9 <processid>

my logs:
=======
[root@ns root]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
xfs 19085 apache 18u IPv4 127053 TCP *:http (LISTEN)
sh 19086 apache 18u IPv4 127053 TCP *:http (LISTEN)
sh 19087 root 18u IPv4 127053 TCP *:http (LISTEN)
su 19088 root 18u IPv4 127053 TCP *:http (LISTEN)
bash 19089 root 18u IPv4 127053 TCP *:http (LISTEN)
[root@ns root]# kil -9 19085 19086
bash: kil: command not found
[root@ns root]# kill -9 19085 19086
[root@ns root]# lsof -i :80
[root@ns root]# lsof -i :443
[root@ns root]# lsof -i :443
[root@ns root]#
[root@ns root]#
[root@ns root]#
[root@ns root]#
[root@ns root]#
[root@ns root]# apachectl sto
usage: /usr/sbin/apachectl (start|stop|restart|fullstatus|status|graceful|configtest|help)

start - start httpd
stop - stop httpd
restart - restart httpd if running by sending a SIGHUP or start if
not running
fullstatus - dump a full status screen; requires lynx and mod_status enabled
status - dump a short status screen; requires lynx and mod_status enabled
graceful - do a graceful restart by sending a SIGUSR1 or start if not running
configtest - do a configuration syntax test
help - this screen

[root@ns root]# apachectl stop
/usr/sbin/apachectl stop: httpd (no pid file) not running
[root@ns root]# apachectl start
/usr/sbin/apachectl start: httpd started
[root@ns root]# lsof -i :443
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
httpd 24957 root 19u IPv4 479222 TCP *:https (LISTEN)
httpd 24958 root 19u IPv4 479222 TCP *:https (LISTEN)
httpd 24959 root 19u IPv4 479222 TCP *:https (LISTEN)
httpd 24960 root 19u IPv4 479222 TCP *:https (LISTEN)
httpd 24961 root 19u IPv4 479222 TCP *:https (LISTEN)
httpd 24962 root 19u IPv4 479222 TCP *:https (LISTEN)
httpd 24963 root 19u IPv4 479222 TCP *:https (LISTEN)
httpd 24964 root 19u IPv4 479222 TCP *:https (LISTEN)
httpd 24965 root 19u IPv4 479222 TCP *:https (LISTEN)
[root@ns root]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
httpd 24957 root 18u IPv4 479221 TCP *:http (LISTEN)
httpd 24958 root 18u IPv4 479221 TCP *:http (LISTEN)
httpd 24959 root 18u IPv4 479221 TCP *:http (LISTEN)
httpd 24960 root 18u IPv4 479221 TCP *:http (LISTEN)
httpd 24961 root 18u IPv4 479221 TCP *:http (LISTEN)
httpd 24962 root 18u IPv4 479221 TCP *:http (LISTEN)
httpd 24963 root 18u IPv4 479221 TCP *:http (LISTEN)
httpd 24964 root 18u IPv4 479221 TCP *:http (LISTEN)
httpd 24965 root 18u IPv4 479221 TCP *:http (LISTEN)
[root@ns root]# service httpd status
httpd (pid 24965 24964 24963 24962 24961 24960 24959 24958 24957) is running...
[root@ns root]# ls
anaconda-ks.cfg dead.letter Linux_Backup mail Mail nsmail
[root@ns root]# cd /etc/httpd/config
bash: cd: /etc/httpd/config: No such file or directory
[root@ns root]# ls
anaconda-ks.cfg dead.letter Linux_Backup mail Mail nsmail
[root@ns root]# cd /etc/httpd
[root@ns httpd]# cd conf
[root@ns conf]# ls


Why this problem comes:
-------------------------------
this problem is comes only if u started the weberver and than made some modification or tried to load the jsp or asp support than u will find http locked when u tried to restart it .

so don't be confussed. just kill th process and than put ur configuration (original file) which has port 80 or 443.

it will work fine......

else mail me on my personal mail ilel 
pushkar_bhatkoti@rediffmail.com
yahooo: pushkarbhatkoti@yahoo.com

My mobile is : 9810774912 
I lives in New Delhi India.
cheers!!




/lib/lsb/init-functions

Thursday, September 26, 2013

Install MySQL 5.6.14 on Ubuntu 12.04 from Binary package



Operating System:Ubuntu 12.04 LTS 64 bits
MySQL Version:5.6.14
Installation File typeBinary, compressed in mysql-5.6.14-linux-glibc2.5-x86_64.tar.gz
Downloadhttp://fossies.org/linux/misc/mysql-5.6.14-linux-glibc2.5-x86_64.tar.gz/cloc.html

I believe that installing MySQL in other versions of ubuntu should be similar or exactly same, like ubuntu 13.04, etc

The file you download is a compressed package that include all the files that are necessary for the installation, the only thing you need to do is to unzip the files into a location that you want the MySQL be installed in. Remember, these are compiled files and they are different from a source package which requires you to use make command to compile and use make install command to install.

If you install MySQL from source code, the only difference is that you need to compile those source files and use make install to install them as specified in ./configure command before you compile. While in here, you only need to unzip the package to a directory, and the installation is completed after you done that.

1. Unzip mysql-5.6.14-linux-glibc2.5-x86_64.tar.gz into /usr/local/mysql-5.6.14/ directory

sudo tar zxvf mysql-5.6.14-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
(Note: after you done the above command, you should be able to see there is mysql-5.6.14 directory in /usr/local)

Next, you probably will need to create a symbolic link to that directory so that you can refer to mysql more easily regardless of its version number, you do like this:

cd /usr/local
ln -s /usr/local/mysql-5.6.14 mysql
(so now you create a symbolic link mysql in /usr/local to point to the /usr/local/mysql-5.6.14 directory)

The /usr/local/mysql-5.6.14 directory after unzip:


2. Create user and group.
you need to create a group and a user for MySQL, let's just call the group name and the user name both mysql

Create group:
sudo groupadd mysql
Create user:
sudo useradd -r -g mysql mysql
(Note: sudo useradd -r -g groupname username, the user mysql is created for the purpose of controlling the right of files, using '-r' will allow this user no need to login)

3. Change the owner of directory /usr/local/mysql-5.6.14/ to the user mysql
sudo chown -R mysql:mysql /usr/local/mysql-5.6.14
4. Install the shared library libaio1
sudo apt-get install libaio1
5. Use mysql_install_db to initialize the authorization table
sudo scripts/mysql_install_db --user=mysql
6. Configure mysql service

(a) Enter /etc/init.d/
cd /etc/init.d
(b) Create a symbolic link to mysql service
sudo ln -s /usr/local/mysql-5.6.14/support-files/mysql.server mysql.server
or you can copy the mysql.server file into the /etc/init.d directory, this directory controls all the services that are need to start at the boot of the operating system.

(c) Change to ownership of mysql service to the user mysql
sudo chown -R mysql:mysql mysql
(d) Start mysql:
sudo /etc/init.d/mysql.server start
7. Check whether mysql service is running or not
ps -aux | grep mysql
8. Check mysql's version, enter /usr/local/mysql
bin/mysqladmin version


As we can see, MySQL is installed successfully.

Now we need to configure MySQL, we call it post-installation

1. create password for the root user of mysql
First, enter mysql's installation directory, mysql is by default installed in /usr/local/mysql :
cd /usr/local/mysql
Then, type in a new password:
./bin/mysqladmin -u root password '123456'
2. Security configuration, including deleting Test database and anonymous operations.
sudo bin/mysql_secure_installation
Then, do instructions as following:
  1. NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL  
  2.       SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!  
  3.   
  4. In order to log into MySQL to secure it, we'll need the current  
  5. password for the root user.  If you've just installed MySQL, and  
  6. you haven't set the root password yet, the password will be blank,  
  7. so you should just press enter here.  
  8.   
  9. Enter current password for root (enter for none):   
  10. OK, successfully used password, moving on...  
  11.   
  12. Setting the root password ensures that nobody can log into the MySQL  
  13. root user without the proper authorisation.  
  14.   
  15. You already have a root password set, so you can safely answer 'n'.  
  16.   
  17. Change the root password? [Y/n] 
  18.  ... skipping.  
  19.   
  20. By default, a MySQL installation has an anonymous user, allowing anyone  
  21. to log into MySQL without having to have a user account created for  
  22. them.  This is intended only for testing, and to make the installation  
  23. go a bit smoother.  You should remove them before moving into a  
  24. production environment.  
  25.   
  26. Remove anonymous users? [Y/n] 
  27.  ... Success!  
  28.   
  29. Normally, root should only be allowed to connect from 'localhost'.  This  
  30. ensures that someone cannot guess at the root password from the network.  
  31.   
  32. Disallow root login remotely? [Y/n] y
  33.  ... Success!  
  34.   
  35. By default, MySQL comes with a database named 'test' that anyone can  
  36. access.  This is also intended only for testing, and should be removed  
  37. before moving into a production environment.  
  38.   
  39. Remove test database and access to it? [Y/n] y
  40.  - Dropping test database...  
  41.  ... Success!  
  42.  - Removing privileges on test database...  
  43.  ... Success!  
  44.   
  45. Reloading the privilege tables will ensure that all changes made so far  
  46. will take effect immediately.  
  47.   
  48. Reload privilege tables now? [Y/n] y 
  49.  ... Success!  
  50.   
  51.   
  52.   
  53.   
  54. All done!  If you've completed all of the above steps, your MySQL  
  55. installation should now be secure.  
  56.   
  57. Thanks for using MySQL!  
  58.   
  59.   
  60. Cleaning up...  

3. Configure the startups.
sudo update-rc.d mysql.server defaults
4. Set environment variables.
Add the mysql/bin into system PATH, need to edit /etc/profile:
sudo vim /etc/profile
add the following line into /etc/profile
export PATH="$PATH:/usr/local/mysql/bin"
Or, you can edit a /etc/environment file directly to achieve a system wide effect:
sudo vim/etc/environment
Now, save the file and restart the computer, and then you will be able to use mysql in any directory
Configuration complete. now you can use the new installed mysql!

Wednesday, September 25, 2013

Install MySQL 5.6 on Ubuntu 12.04 (Using *.deb)



Install MySQL 5.6 on Ubuntu 12.04 LTS 

by Shi, September 26 2013


This article introduces how to install the lastest version MySql 5.6 on ubuntu 12.04 using *.deb debian packages, since the installer will install the mysql in /opt/mysql, and not the standard directory /usr/local/mysql, the installer does not set up anything so you have to set up manually. It's very very cumbersome, because you may experience all kinds of errors if some steps are missed or inappropriately operated. I have been struggling in this for quite a long time so that's why I come out and write down the processes of how I succeed finally.

Note: This tutorial is oriented to those who want to install MySQL 5.6 on Ubuntu using Debian packages, specifically for x86_64bit machine, and majorly for fresh installation (instructions on upgrade from old version to 5.6 are given accompany) there are some other tutorials using binary or other ways to install you can find on Google, but I won't bother that.


Part I: Install mysql 5.6 in ubuntu 12.04

Step 1.

Disabling AppArmor
Your manually installed MySQL 5.6.14 might refuse to start if AppArmor is running, therefore we disable AppArmor:
sudo /etc/init.d/apparmor stop
sudo update-rc.d -f apparmor remove
sudo apt-get remove apparmor apparmor-utils
Actually, you can do step 1 as the last step if you like, I have tested, it doesn't matter you do it now or do it later.

Step 2.

Download & Install MySQL 5.6 deb package from Official Site

Two ways:
(1) You can get MySQL installers from https://dev.mysql.com/downloads/mysql/#downloads
then choose Debian Linux. the package is about 283MB.

(2) Or, you can  get MySQL 5.6.x using command line

sudo wget -O mysql-5.6.deb https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.14-debian6.0-x86_64.deb/from/http://cdn.mysql.com/

Then install Mysql 5.6.x
sudo dpkg -i  mysql-5.6.14-debian6.0-x86_64.deb
(you can delete the package after you installed, use:  rm mysql-5.6.14-debian6.0-x86_64.deb  )

then you need to install dependency libaio1 before you proceed
sudo apt-get install libaio1

========================================================================
Note: If there is an old mysql version running on your system already then you need to uninstall it before you proceed to install the new version, and you want to back up the old verison first:

(a) Backup old version of MySQL Data
You will need this only if you are upgrading…
service mysql stop
cp -pr /var/lib/mysql/ /var/lib/mysql.old
At this point your previous MySQL is stopped and you have mysql’s data directory and mysqldump output in ~/backup directory. If all goes well you will not need backups!
(b) Remove previous version of MySQL Packages
sudo apt-get remove mysql-server-5.5 mysql-server-core-5.5 mysql-client-5.5 mysql-client-core-5.5 mysql-common
sudo apt-get remove --purge mysql-server-5.5 mysql-server-core-5.5 mysql-client-5.5 mysql-client-core-5.5 mysql-common
sudo dpkg --purge mysql-server-5.5 mysql-server-core-5.5 mysql-client-5.5 mysql-client-core-5.5 mysql-commonsudo apt-get autoremove
sudo apt-get autoclean
deluser mysql
sudo updatedb

Sometimes after above cmds,using "locate mysql" still lists many file related to mysql, that is because the system database is not updated.

Delete all the database
sudo find /var/lib/mysql/ -type f -delete

Delte all the mysql lib
sudo rm -r /opt/mysql

========================================================================

Step 3.  

Setup MySQL 5.6 Startup Script

Move your my.cnf from /etc/mysql/my.cnf which is the Ubuntu default location to /etc/my.cnf which is the one mysql uses for the start scripts. 

sudo mv /etc/mysql/my.cnf /etc/my.cnf
note that the /etc/mysql/conf.d/* files will be included anyway.

To get mysql started you will need to copy the new startscript to /etc/init.d, use cp /opt/mysql/server-5.6/support-files/mysql.server /etc/init.d/mysql.server

As you probably want mysql to start automatically on system startup, use update-rc.d mysql.server defaults to create a standard runlevel configuration.


sudo cp /opt/mysql/server-5.6/support-files/mysql.server /etc/init.d/mysql.server
sudo update-rc.d -f mysql remove
sudo update-rc.d mysql.server defaults
(Note: MySQL 5.6 depends on libaio which we already installed)

Step 4.  

Update configuration & Environment settings

(a)Create user and set owner & group of the mysql files
groupadd mysql
useradd -r -G mysql mysqlchown -R mysql /opt/mysql/server-5.6/
chgrp -R mysql /opt/mysql/server-5.6/

(b) Update mysql config file
By default, for MySql 5.6, the my.cnf is located in /etc/mysql/my.cnf, here we need to move it to /etc/my.cnf:

Use vim or whatever editor to edit my.cnf:
sudo vim /etc/mysql/my.cnf
basedir = /opt/mysql/server-5.6
lc-messages-dir = /opt/mysql/server-5.6/share

You may also need to tweak few other settings.
For example: table_cache has been renamed to table_open_cache

(c) Update envirionment variable: $PATH

Add /opt/mysql/server-5.6/bin into system path and use command source /etc/environment to update immediately
vim /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/opt/mysql/server-5.6/bin"
source /etc/environment

(d) confirm changes

which sql
It should show: /opt/mysql/server-5.6/bin/mysql

(e) Update mysql information schema to version 5.6

/opt/mysql/server-5.6/scripts/mysql_install_db --user=mysql --datadir=/var/lib/mysql

Caution: This will create my.cnf in /opt/mysql/server-5.6/ which sets SQL mode to strict. Your applications may have problems with this mode. For compatibility reasons you should DELETE this confusing my.cnf and only rely on your /etc/my.cnf. Delete it by  rm /opt/mysql/server-5.6/my.cnf. You won’t be able to overwrite this setting with your /etc/my.cnf and remove STRICT_TRANS_TABLES from your sql_mode. Read more about mysql_install_db here.

Also, pay attention that the "datadir" is the database storage path, if you need to re-install the mysql, generally it is better to delete all the database in this folder.
rm /opt/mysql/server-5.6/my.cnf

Step 5.  

Start mysql
service mysql.server start

Or, you can also start it using mysqld, which have no datadir setting:
sudo /opt/mysql/server-5.6/bin/mysqld

You can checking the difference of this two methods using :

ps aux | grep mysqld

Finally, we reach this point, and hopefully everything goes fine. If you encounter any errors please refer to the below Errors part, Now you can use "mysql" to enter your mysql client, but will got many rights/access issues, therefore, we need to set the password for user.


Part II: Set password for root

Note: You may check the official site for detailed explanations.

Step 1.  Stop mysqld:

sudo service mysql.server stop

Step 2. Start mysqld with necessary options:

sudo /opt/mysql/server-5.6/bin/mysqld --skip-grant-tables --skip-networking
If your mysql server is in a risk of remote client access, then it is better to use --skip-networking.

Step 3. Connect to the mysql-client:

mysql

Step 4. Set password:

UPDATE mysql.user SET Password=PAWWSORD('MyNewPasswd') WHERE user='root';
FLUSH PRIVILEGES

Step 5. Stop mysql-client and start it again without specific options:

sudo service mysql.server stop
sudo service mysql.server start

Step 6. Connet to the mysql-client using root with password:

mysql -u root --password=abc --port=3306 --pager="less -SFX"

OR
mysql -u root -p'abc' --port=3306 --pager="less -SFX"
Pay attention to the password format if you use "-p" not "--password" option. You can change the port number in my.cnf.


Part III:  Possible issues

1. After update mysql schema using mysql_install_db, you will find mysql suggests to use
mysql_secure_installation to help to set the password, I tried, then I got info as below and tried many methods suggested in the internet, but had no luck.
/opt/mysql/server-5.6/bin/mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current password for the root user. If you've just installed MySQL, and you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
mysql: unknown option '--connect-expired-password'
Enter current password for root (enter for none):
mysql: unknown option '--connect-expired-password'
Enter current password for root (enter for none):
mysql: unknown option '--connect-expired-password'
Unable to connect to the server as root user, giving up.

Cleaning up...

2. If you forget to start the mysqld and run mysql, then you will get error like below:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

A possible Solution:
modify my.cnf file using sudo vim /etc/mysql/my.cnf, find the line bind-address = 127.0.0.1 and revised it to bind-address = localhost, then restart mysql using sudo /etc/init.d/mysql restart

3. Remote access setting
If the database server needs to be accessed by other remote servers, then you need to configure the setting in the my.cnf file.
Comment these lines in my.cnf:
skip-networking bind-address = 127.0.0.1

4. Fatal error: Call to undefined function mysql_connect()
On ubuntu, this means the php does not have the mysql module, or the module is not correctly connected:
sudo apt-get install php5-mysql