MySQL 5.7.19 編譯安裝與配置

背景:本人博客自2014年上線以來,一直使用阿里云ECS最低配的實例,由于最近阿里云ECS進行了升級遷移,原來的低配實例已經不存在了,升級后實例的配置有所提升,當然價格更高了,為了更好的發揮服務器性能,所以就想利用空閑時間對整站進行升級,包含阿里云ecs更換系統盤MySQL 5.7.19 編譯安裝與配置, Nginx 1.12.1 編譯安裝與配置, PHP 7.1.9 編譯安裝與配置等。

服務器環境
CentOS 6.3 64位 全新純凈的系統 / 1核1GB / 經典網絡 1MB

進入MySQL官網下載頁面,地址https://www.mysql.com/downloads/,如果你想使用MySQL 5.7.19的源碼版本,點此處直接下載!
進入MySQL Community Edition下載頁面
點擊進入MySQL Community Edition下載頁面

點擊DOWNLOAD
選擇操作系統為Source Code,選擇操作系統版本為Generic Linux,選擇Compressed TAR Archive, Includes Boost Headers版本或Compressed TAR Archive版本,暫未研究兩個版本的區別,開始以為Includes Boost Headers不用再去下載Boost庫,然而安裝時發現還是需要,所以此處先任意選擇一個版本,選擇點擊 Download 進行下載
選擇源碼下載
點擊下載,此處并不需要注冊
進入/usr/local/src目錄,一般我喜歡把下載的文件放在此目錄,根據自己的喜好設定

[root@iZ2864f6btwZ src]# cd /usr/local/src

下載MySQL文件,如果wget沒有安裝,yum -y install wget即可安裝

[root@iZ2864f6btwZ src]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.19.tar.gz

問題錯誤:
如果此處下載遇到如下問題,說明你沒有安裝openssl,此問題一般只會出現在全新的機器上
[root@iZ2864f6btwZ src]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.19.tar.gz--2017-09-22 16:20:26-- https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.19.tar.gz Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11 Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected. Unable to establish SSL connection.

解決方案:
安裝opensll,yum -y install openssl 再次執行下載命令即可

由于MySQL 5.7需要boost 1.59以及以上版本,所以還需要下載boost庫,根據本人測試1.59版本的最為適合,其它高版本在安裝的時候遇到了一些問題,目前未解決;下載地址,你也可以點擊此處直接下載boost_1_59_0.tar.gz,如果wget無法下載,建議使用迅雷下載后再上傳到服務器目錄

[root@iZ2864f6btwZ src]# wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz

解壓 boost并拷貝 boost 到 /usr/local/boost 目錄
[root@iZ2864f6btwZ src]# tar zxvf boost_1_59_0.tar.gz
[root@iZ2864f6btwZ src]# cp -r boost_1_59_0 /usr/local/boost
安裝編譯所需的常用組件和依賴包 [ 參考于網絡博客 ]

[root@iZ2864f6btwZ src]# yum -y install gcc gcc-c++ ncurses ncurses-devel bison libgcrypt perl make cmake

創建mysql用戶組和用戶,用來運行mysql服務器, -g指定用戶組, -r創建系統用戶
[root@iZ2864f6btwZ src]# groupadd mysql
[root@iZ2864f6btwZ src]# useradd -r -g mysql -s /bin/false -M mysql
解壓MySQL,進入 mysql-5.7.19 目錄
[root@iZ2864f6btwZ src]# tar zxvf mysql-boost-5.7.19.tar.gz 
[root@iZ2864f6btwZ src]# cd mysql-5.7.19/
新建MySQL安裝所需要目錄

[root@iZ2864f6btwZ mysql-5.7.19]# mkdir -p /usr/local/mysql /usr/local/mysql/{data,logs,pids}

修改 /usr/local/mysql 目錄所有者權限

[root@iZ2864f6btwZ mysql-5.7.19]# chown -R mysql:mysql /usr/local/mysql

使用cmake命令進行編譯

[root@iZ2864f6btwZ mysql-5.7.19]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -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 -DENABLE_DOWNLOADS=1 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost

cmake執行完的結果

使用make命令進行編譯,接下來你將經歷漫長的等待~

[root@iZ2864f6btwZ mysql-5.7.19]# make

問題錯誤:
編譯出現錯誤,查詢得知可能是由于內存不足導致的

c++編譯出錯

解決方案:
臨時增加交換空間 ( 虛擬內存 )

[root@iZ2864f6btwZ mysql-5.7.19]# dd if=/dev/zero of=/swapfile bs=1k count=2048000
2048000+0 records in
2048000+0 records out
2097152000 bytes (2.1 GB) copied, 34.6782 s, 60.5 MB/s
[root@iZ2864f6btwZ mysql-5.7.19]# mkswap /swapfile
Setting up swapspace version 1, size = 2047996 KiB
no label, UUID=56026239-26e6-40d9-b080-b95acd9db058
[root@iZ2864f6btwZ mysql-5.7.19]# swapon /swapfile
swapon: /swapfile: insecure permissions 0644, 0600 suggested.
[root@iZ2864f6btwZ mysql-5.7.19]# chmod 600 /swapfile

查看創建的交換空間

[root@iZ2864f6btwZ mysql-5.7.19]# free -m
             total   used  free  shared  buff/cache  available
Mem:         992     51    70    0       869         789
Swap:        1999    0     1999

繼續執行make命令

[root@iZ2864f6btwZ mysql-5.7.19]# make clean
[root@iZ2864f6btwZ mysql-5.7.19]# make

如果你編譯完成后不再想要此交換空間,你可以執行如下命令:

[root@iZ2864f6btwZ mysql-5.7.19]# swapoff /swapfile
[root@iZ2864f6btwZ mysql-5.7.19]# rm /swapfile

溫馨提示:
MySQL編譯過程等待時間會比較久,有時都以為是“卡”住了,你可以使用top命令查看資源狀態,看看cc1plus、make等進程是否在跳動,如果有跳動說明安裝還在繼續,由于我的 ecs 配置較低,此過程大約經歷了幾個小時,特別是在29%和74%的時候,幾乎都要快放棄了, 如果有經濟的能力的話,建議服務器配置還是盡量買高一點。
[root@iZ2864f6btwZ mysql-5.7.19]# top

make編譯完成
編譯完成
編譯完成后執行 make install 進行安裝

[root@iZ2864f6btwZ mysql-5.7.19]# make install

將mysql添加到環境變量,修改/etc/profile文件,在文件最末尾添加export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
[root@iZ2864f6btwZ mysql-5.7.19]# vim /etc/profile
...
unset i
unset -f pathmunge
# mysql執行路徑
export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
更新配置文件

[root@iZ2864f6btwZ mysql-5.7.19]# source /etc/profile

初始化數據庫, –initialize 表示默認生成一個安全的密碼,–initialize-insecure 表示不生成密碼

[root@iZ2864f6btwZ mysql-5.7.19]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

將mysql服務文件拷貝到/etc/init.d/目錄,并給出執行權限
[root@iZ2864f6btwZ mysql-5.7.19]# cp support-files/mysql.server /etc/init.d/mysqld
[root@iZ2864f6btwZ mysql-5.7.19]# chmod a+x /etc/init.d/mysqld
將MySQL并加入開機自動啟動
[root@iZ2864f6btwZ mysql-5.7.19]# chkconfig --add mysqld
[root@iZ2864f6btwZ mysql-5.7.19]# chkconfig mysqld on
[root@iZ2864f6btwZ mysql-5.7.19]# chkconfig --list | grep mysqld

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off
修改/etc/my.cnf文件,編輯配置文件如下,僅供參考
[root@iZ2864f6btwZ mysql-5.7.19]# vim /etc/my.cnf 

[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock

[mysqld_safe]
log-error=/usr/local/mysql/logs/mysqld.log
pid-file=/usr/local/mysql/pids/mysqld.pid

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[client]
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock

[mysql]
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
啟動MySQL
[root@iZ2864f6btwZ local]# service mysqld start
Starting MySQL.2017-09-23T16:13:16.049373Z mysqld_safe error: log-error set to '/usr/local/mysql/logs/mysqld.log', however file don't exists. Create writable for user 'mysql'.
The server quit without updating PID file (/usr/local/mysql[FAILED]2864f6btwZ.pid).

問題錯誤:
由于缺少 mysqld.logmysqld.pid 文件導致無法正常啟動

解決方案:
創建 mysqld.logmysqld.pid 文件

[root@iZ2864f6btwZ mysql-5.7.19]# touch /usr/local/mysql/logs/mysqld.log
[root@iZ2864f6btwZ mysql-5.7.19]# touch /usr/local/mysql/pids/mysqld.pid

修改 /usr/local/mysql 的權限
[root@iZ2864f6btwZ mysql-5.7.19]# chown mysql.mysql -R /usr/local/mysql/

再次啟動MySQL
[root@iZ2864f6btwZ local]# service mysqld start

查看MySQL運行狀態
[root@iZ2864f6btwZ local]# service mysqld status
MySQL is not running, but lock file (/var/lock/subsys/mysql[FAILED]

問題錯誤:
MySQL is not running, but lock file (/var/lock/subsys/mysql[FAILED]

解決方案:
刪除/var/lock/subsys/mysql文件,重新啟動MySQL

[root@iZ2864f6btwZ local]# rm -f /var/lock/subsys/mysql
[root@iZ2864f6btwZ local]# service mysqld start
Starting MySQL.                                            [  OK  ]
連接MySQL
[root@iZ2864f6btwZ mysql-5.7.19]# mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
[root@iZ2864f6btwZ mysql-5.7.19]#

問題錯誤:
/etc/my.cnf 文件配置不正確
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

解決方案:
參考上述步驟 修改 /etc/my.cnf 文件

[root@iZ2864f6btwZ local]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.19 Source distribution

Copyright (c) 2000, 2017, 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>
查看數據庫,如果看到以下幾個數據庫說明數據庫初始化成功
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql>
進入mysql庫,查看用戶表信息
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>
mysql> select host,user,password from user;
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
mysql>

問題錯誤:
ERROR 1054 (42S22): Unknown column 'password' in 'field list'

解決方案:
由于MySQL 5.7版本下的mysql數據庫下已經沒有password這個字段了,password字段改成了authentication_string,查詢時使用authentication_string字段即可

mysql> select host,user,authentication_string from user;
+-----------+---------------+-------------------------------------------+
| host      | user          | authentication_string                     |
+-----------+---------------+-------------------------------------------+
| localhost | root          |                                           |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+-----------+---------------+-------------------------------------------+
3 rows in set (0.00 sec)
設置密碼(推薦),注意此方法必須使用flush privileges命令刷新一下權限才能生效
mysql> UPDATE user SET authentication_string=PASSWORD('newpassword') WHERE user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql>
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>
另外一種方法可以快速密碼,但此方法設置的密碼使用history命令可以看到,所以不太推薦
[root@iZ2864f6btwZ ~]# mysqladmin -u root password 'newpassword'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

結尾:
至此,MySQL 5.7.19 編譯安裝及配置已經全部完成,有疑問的朋友可以給我留言,若有毛病,歡迎指正。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,428評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,024評論 3 413
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,285評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,548評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,328評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,878評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,971評論 3 439
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,098評論 0 286
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,616評論 1 331
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,554評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,725評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,243評論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 43,971評論 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,361評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,613評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,339評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,695評論 2 370

推薦閱讀更多精彩內容