Linux源碼編譯安裝MySQL5.7.9

來源:互聯網
上載者:User

Linux源碼編譯安裝MySQL5.7.9

一、環境準備:

我嘗試過以下環境都是能成功的:
1、CentOS6.7 minimal鏡像最小化預設安裝;
2、RedHat6.5 DVD基本伺服器安裝;
3、其他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和主機名稱(需要根據實際情況修改IP和主機名稱)
# vi /etc/hosts +
192.168.1.2      bkjia
7、安裝編譯源碼所需的工具和庫(如果不能線上安裝,需要提前配置好本地源,本地源配置方法度娘吧,以後有時間)
# yum -y install wget gcc-c++ ncurses-devel cmake make perl
注意:如果是RedHat5.4,其ISO源中沒有cmake,可以手動編譯安裝cmake,或者升級到RedHat5.9之後再從源中yum安裝cmake。
8、通過FTP或SFTP將mysql-5.7.9.tar.gz源碼包上傳到/usr/local/src路徑下。
如果伺服器能上網,也可以通過wget下載mysql-5.7.9.tar.gz。如下:
# cd /usr/local/src
# wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.9-linux-glibc2.5-x86_64.tar.gz

四、開始編譯安裝mysql-5.7.9:

1、進入源碼壓縮包下載目錄
# cd /usr/local/src
2、解壓縮源碼包
# tar -zxvf mysql-5.7.9.tar.gz
3、進入解壓縮源碼目錄
# cd mysql-5.7.9
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.7.9在執行下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@bkjia etc]# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)——沒有密碼無法登入,說明密碼修改成功了。
[root@bkjia ~]# 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.7.9-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@bkjia ~]# /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@bkjia ~]# reboot

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.