RedHat/CentOS源碼編譯安裝MySQL5.6.12

一、環境準備:
我嘗試過以下環境都是能成功的:
1、CentOS6.4 minimal鏡像最小化缺省安裝;
2、RedHat6.4 DVD基本服務器安裝;
3、RedHat5.4 DVD基本服務器安裝;
4、其他Linux版本未驗證。
注意系統安裝好之後需要配置好網卡,關閉防火牆及SELINUX:

# 關閉Linux防火牆命令 
# chkconfig iptables off
# 修改SELINUX配置
# vim /etc/sysconfig/selinux
SELINUX=enforcing
修改爲:
SELINUX=disabled

修改完成後,保存並退出,然後重啓系統。

二、先使用 yum -y update 指令升級系統到最新版本。
如果服務器在內網,此步驟可略過。

三、做一些準備工作(以下Linux命令均在su到root用戶操作):
1、新增mysql用戶組

# groupadd mysql

2、新增mysql用戶

# useradd -r -g mysql mysql

3、新建數據庫執行文件目錄(後面會把編譯好的mysql程序安裝到這個目錄)

# mkdir -p /usr/local/mysql

4、新建數據庫數據文件目錄

# mkdir -p /home/mysql
# mkdir -p /home/mysql/data
# mkdir -p /home/mysql/logs
# mkdir -p /home/mysql/temp

注意:上面的第3及第4是爲了以後將MySQL的數據文件與執行程序文件分離,如果你打算設置到不同的路徑,注意修改對應的執行命令和數據庫初始化腳本!

5、編輯PATH搜索路徑

# vi /etc/profile +
# 在profile文件末尾增加兩行
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
export PATH

使PATH搜索路徑立即生效:

# source /etc/profile

6、編輯hosts文件,增加一行,加入本機IP和主機名

# vi /etc/hosts +
192.168.100.2      yimiju

7、安裝編譯源碼所需的工具和庫(如果不能在線安裝,需要提前配置好本地源,本地源配置方法度娘吧,以後有時間)

# yum -y install wget gcc-c++ ncurses-devel cmake make perl

注意:RedHat5.4源中沒有cmake,可以手動編譯安裝cmake,或者升級到RedHat5.9之後再從源中yum安裝cmake。

8、通過FTP或SFTP將mysql-5.6.12.tar.gz源碼包上傳到/usr/local/src路徑下。
如果服務器能上網,也可以通過wget下載mysql-5.6.12.tar.gz。下載地址如下:

# cd /usr/local/src
# wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.12.tar.gz/from/http://cdn.mysql.com/

四、開始編譯安裝mysql-5.6.12
1、進入源碼壓縮包下載目錄

# cd /usr/local/src

2、解壓縮源碼包

# tar -zxvf mysql-5.6.12.tar.gz

3、進入解壓縮源碼目錄

# cd mysql-5.6.12

4、使用cmake源碼安裝mysql(如果你打算安裝到不同的路徑,注意修改下面語句中/usr/local/mysql這個路徑!)

#cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/home/mysql/data \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306	\
-DENABLE_DOWNLOADS=1

上面的這些複製完,回車,然後就開始cmake的過程,一般時間不會很長。

5、cmake結束後開始編譯源碼,這一步時間會較長,請耐心等待。

# make

6、安裝編譯好的程序

# make install

注意:如果需要重裝mysql,在/usr/local/src/mysql-5.6.12在執行下make install就可以了,不需要再cmake和make

7、清除安裝臨時文件

# make clean

8、修改目錄擁有者

# chown -Rf mysql:mysql /usr/local/mysql
# chown -Rf mysql:mysql /home/mysql

9、進入mysql執行程序的安裝路徑

# cd /usr/local/mysql

10、執行初始化配置腳本,創建系統自帶的數據庫和表(注意路徑/home/mysql/data需要換成你自定定義的數據庫存放路徑)

# scripts/mysql_install_db --user=mysql --datadir=/home/mysql/data

#初始化腳本在/usr/local/mysql/下生成了配置文件my.cnf,需要更改該配置文件的所有者:

# chown -Rf mysql:mysql /usr/local/mysql

注意:
(1)Tips:在啓動MySQL服務時,會按照一定次序搜索my.cnf,先在/etc目錄下找,找不到則會搜索mysql程序目錄下是否有my.cnf";
(2)需要注意CentOS 6.4版操作系統的最小安裝完成後,即使沒有安裝mysql,在/etc目錄下也會存在一個my.cnf文件,建議將此文件更名爲其他的名字,否則該文件會干擾源碼安裝的MySQL的正確配置,造成無法啓動。修改/etc/my.cnf操作如下:

# mv /etc/my.cnf /etc/my.cnf.bak
# 當然也可以刪除掉/etc/my.cnf這個文件:
# rm /etc/my.cnf

(3)如果你需要用於生產環境,不要急着做下面的mysql啓動操作。建議把上一步驟中mysql初始化生成的/usr/local/mysql/mysql.cnf刪除,然後把你優化好的mysql配置文件my.cnf放到/etc下。(這是我做mysql主從複製和mysql優化的經驗!)

11、複製服務啓動腳本

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

12、啓動MySQL服務

# service mysql start

13、設置開機自動啓動服務

# chkconfig mysql on

14、登錄並修改MySQL用戶root的密碼

# mysql -u root
mysql> use mysql;
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "123456";
mysql> update user set Password = password('123456') where User='root';
mysql> flush privileges;
mysql> exit;

15、檢測下上一步MySQL用戶root密碼是否生效:

[root@yimiju etc]# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)——沒有密碼無法登錄,說明密碼修改成功了。
[root@yimiju ~]# mysql -u root -p
Enter password: ——這裏提示時輸入你設置的mysql root帳號密碼
#登錄成功有如下提示:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 422
Server version: 5.6.12-log Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

16、(可選)運行安全設置腳本,強烈建議生產服務器使用:

[root@yimiju ~]# /usr/local/mysql/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): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n		---------------這裏輸入n
 ... skipping.

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y			---------------這裏輸入Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n			---------------這裏輸入n
 ... skipping.

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y			---------------這裏輸入Y
 - Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
 ... Failed!  Not critical, keep moving...
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y			---------------這裏輸入Y
 ... Success!

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

Cleaning up...

17、重啓服務器,檢測mysql是否能開機自動啓動

[root@yimiju ~]# reboot


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章