Thursday 17 October 2013

Install MYSQL 5.6 in ubuntu 12.04 and set password

Part 1 : The steps to install mysql 5.6 in ubuntu 12.04

1. check the system OS version uname -i

2.If there is an old mysql version running on your system and you need to uninstall it, and if you also install a .deb version, you need to use the "dpkg" one to uninstall:

sudo apt-get remove mysql-server mysql-server-5.5 mysql-server-core-5.5
sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo dpkg --pure mysql-server mysql-client mysql-common
sudo 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##
sudo find /var/lib/mysql/ -type f -delete ##delete all the database## 
sudo rm -r /opt/mysql ##delte all the mysql lib##

3. download mysql 5.6 package
Then based on the OS version result in 1, you can download the corresponding mysql package in the mysql official website http://dev.mysql.com/downloads/mysql/.

I use the 32bit version, you can also use the wget in the ubuntu terminal to download the package:
wget http://www.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.12-debian6.0-i686.deb/from/http://cdn.mysql.com/ -O mysql-5.6.12-debian6.0-i686.deb

4. Install the .deb file
dpkg -i mysql-5.6.12-debian6.0-i686.deb

5. unique and modify my.cnf
Defaultly, the my.cnf is located in /etc/mysql/my.cnf, here we need to move it:
mv /etc/mysql/my.cnf /etc/my.cnf
Change the parameters in my.cnf, you can also select the files' paths  that you needed:
basedir = /opt/mysql/server-5.6
lc-message-dir = /opt/mysql/sever-5.6/share

6. locate startscript
cp /opt/mysql/server-5.6/support-files/mysql.server /etc/init.d/mysql.server

**If you need  mysql to autostart when the system start up:
update-rc.d mysql.server defaults

7. Install libaio1
As libaio1 is probably not installed:
apt-get install libaio1

8. Create user and Change owner of the files
groupadd mysql
useradd -r -g mysql mysql
chown -R mysql /opt/mysql/server-5.6
chgrp -R mysql /opt/mysql/server-5.6

9 update mysql information schema info and remove confusing my.cnf:
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 as discussed in 2. 
sudo /opt/mysql/server-5.6/scripts/mysql_install_db --user=mysql --datadir=/var/lib/mysql
rm /opt/mysql/server-5.6/my.cnf

10. start mysqld
sudo service mysql.server start
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

11. Now you can use "mysql" to enter your mysql client, but will get many rights/access issues, we need to set the password for user, this is discussed in Part 2 blelow.
shell>mysql
or
use mysql -u root -p 
then passwors you will get
shell>mysql


Then the installation process is finished. We will set the password for the root user.

Part 2: The steps to set password for root. 
Detail explanation can be found in mysql official website.
1. Stop mysqld:
sudo service mysql.server stopMysql official website

2. Start mysqld with necessary options
shell>sudo /opt/mysql/server-5.6/bin/mysqld Link
If your mysql server is in a risk of remote client access, then it is better to use --skip-networking.

3. Connect to the mysql-client
shell>mysql

4. set password:
mysql> UPDATE mysql.user SET Password=PASSWORD('MyNewPass')WHERE User='root';
mysql> FLUSH PRIVILEGES;

5. Stop mysql-client and start it again without specific options:
sudo service mysql.server stop
sudo service mysql.server start

6. Connect to the mysql-client using root with password:
shell>mysql -u root --password=abc --port=3306 --pager="less -SFX"
or
shell>mysql -u root -p'abc' --port=3306 --pager="less -SFX"
Pay attention to the password format if you use "-p" instead of "--password" option. You can change the port number in my.cnf.

Part 3: Some 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.
shell>/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...
shell>

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)

3. Remote access setting
If the database server needs to be accessed by other remote servers, then needs 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: