Linux源碼編譯安裝MySQL5.7

1、環境準備:

我嘗試過如下環境都是能成功的:
一、CentOS6.7 minimal鏡像最小化缺省安裝;
二、RedHat6.5 DVD基本服務器安裝;
三、其餘Linux版本未驗證。
注意系統安裝好以後須要配置好網卡,關閉防火牆及SELINUX:
# 關閉Linux防火牆命令
# chkconfig iptables off
# 修改SELINUX配置
# vim /etc/sysconfig/selinux
SELINUX=enforcing
修改成:
SELINUX=disabled
修改完成後,保存並退出,而後重啓系統。mysql

2、升級系統:

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

3、作一些準備工做(如下Linux命令均在su到root用戶操做):

一、新增mysql用戶組
# groupadd mysql
二、新增mysql用戶
# useradd -r -g mysql mysql
三、新建數據庫執行文件目錄(後面會把編譯好的mysql程序安裝到這個目錄)
# mkdir -p /usr/local/mysql
四、新建數據庫數據文件目錄
# mkdir -p /home/mysql
# mkdir -p /home/mysql/data
# mkdir -p /home/mysql/logs
# mkdir -p /home/mysql/temp
注意:上面的第3及第4是爲了之後將MySQL的數據文件與執行程序文件分離,若是你打算設置到不一樣的路徑,注意修改對應的執行命令和數據庫初始化腳本!
五、編輯PATH搜索路徑
# vi /etc/profile +
# 在profile文件末尾增長兩行
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
export PATH
使PATH搜索路徑當即生效:
# source /etc/profile
六、編輯hosts文件,增長一行,加入本機IP和主機名(須要根據實際狀況修改IP和主機名)
# vi /etc/hosts +
192.168.1.2      yimiju
七、安裝編譯源碼所需的工具和庫(若是不能在線安裝,須要提早配置好本地源,本地源配置方法度娘吧,之後有時間)
# yum -y install wget gcc-c++ ncurses-devel cmake make perl
注意:若是是RedHat5.4,其ISO源中沒有cmake,能夠手動編譯安裝cmake,或者升級到RedHat5.9以後再從源中yum安裝cmake。
八、經過FTP或SFTP將mysql-5.7.9.tar.gz源碼包上傳到/usr/local/src路徑下。
若是服務器能上網,也能夠經過wget下載mysql-5.7.9.tar.gz。下載地址以下:
# cd /usr/local/src
# wget http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.9.tar.gzc++

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

一、進入源碼壓縮包下載目錄
# cd /usr/local/src
二、解壓縮源碼包
# tar -zxvf mysql-5.7.9.tar.gz
三、進入解壓縮源碼目錄
# cd mysql-5.7.9
四、使用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的過程,通常時間不會很長。
五、cmake結束後開始編譯源碼,這一步時間會較長,請耐心等待。
# make
六、安裝編譯好的程序
# make install
注意:若是須要重裝mysql,在/usr/local/src/mysql-5.7.9在執行下make install就能夠了,不須要再cmake和make
七、清除安裝臨時文件
# make clean
八、修改目錄擁有者
# chown -Rf mysql:mysql /usr/local/mysql
# chown -Rf mysql:mysql /home/mysql
九、進入mysql執行程序的安裝路徑
# cd /usr/local/mysql
十、執行初始化配置腳本,建立系統自帶的數據庫和表(注意路徑/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優化的經驗!)
十一、複製服務啓動腳本
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
十二、啓動MySQL服務
# service mysql start
1三、設置開機自動啓動服務
# chkconfig mysql on
1四、登陸並修改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;
1五、檢測下上一步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.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>
1六、(可選)運行安全設置腳本,強烈建議生產服務器使用:
[root@yimiju ~]# /usr/local/mysql/bin/mysql_secure_installationsql

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.vim

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'.mysql優化

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...
1七、重啓服務器,檢測mysql是否能開機自動啓動
[root@yimiju ~]# reboot

相關文章
相關標籤/搜索